Web application architecture

1,395 views

Published on

This set of slides deals with the problem of building robust Web applications by isolating different areas of concern into different layers. The typical four-layer architecture is presented (consisting of the presentation, service and persistence layer, as well as of the domain model) along with an in-depth discussion on the role and responsibilities of each single layer.

Published in: Education
0 Comments
19 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,395
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
0
Comments
0
Likes
19
Embeds 0
No embeds

No notes for slide

Web application architecture

  1. 1. Web applica*on architecture Ilio Catallo - info@iliocatallo.it
  2. 2. Outline • Layers of abstrac.on • Domain model • Service layer • Persistence layer • Presenta.on layer
  3. 3. Layers of abstrac-on
  4. 4. Layers A layer is a discrete, orthogonal area of concern within an applica4on
  5. 5. Applica'ons are made of layers Applica'ons are broken down into a series of layers, so that each layer isolates a different type of concern
  6. 6. Web applica*ons are made of layers For instance, the problem of persis&ng data is isolated in the persistence layer
  7. 7. Web app. layers Typically, Web applica-ons have four layers of abstrac-on ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  8. 8. Communica)on between layers Communica)on between layers is from top to bo5om ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  9. 9. Execu&on flow through layers Layers can help conceptualize the flow through an applica5on ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  10. 10. Layers are not physically isolated Even if layers are conceptually isolated, they are not necessarily physically isolated ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  11. 11. Flexibility & testability Isola&ng concerns (e.g., data persistence) into separate layers increases flexibility and testability
  12. 12. Flexibility Flexibility is increased because implementa0ons of each layer can vary independently public class SQLMovieRepository implements MovieRepository {...} public class CSVMovieRepository implements MovieRepository {...} public class RESTMovieRepository implements MovieRepository {...}
  13. 13. Testability Testability is increased because the low-coupling between areas make it easier to test each part public class MovieServiceTest { @Test public void moviesDirectedByTest() { ... } }
  14. 14. How to design a layer?
  15. 15. Designing layers When designing a layer, we first try to iden3fy a clear API for it public interface FlightService { List<SpecialDeal> getSpecialDeals(); List<Flight> findFlights(String queryString); }
  16. 16. Designing layers We then expose such an API through a Java interface public interface FlightService { List<SpecialDeal> getSpecialDeals(); List<Flight> findFlights(String queryString); }
  17. 17. Designing layers The interface acts as the layer's contract • It enforces correct layer usage • It hides implementa6on details
  18. 18. Interfaces as layer contracts Interfaces are the key enablers to build applica/ons with layers
  19. 19. Domain model
  20. 20. Domain model The domain model is the most important layer in the applica/on, as it contains the business logic of the applica/on ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║┌────────────────────────────────┐ ║ n ║│ │ ║ ║│ Service layer │ ║ m ║│ │ ║ o ║└────────────────────────────────┘ ║ d ║┌────────────────────────────────┐ ║ e ║│ │ ║ l ║│ Persistence layer │ ║ ║│ │ ╚══════╝└────────────────────────────────┘
  21. 21. Domain model In other words, the domain model is the code representa,on of the business problem we are trying to solve ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║┌────────────────────────────────┐ ║ n ║│ │ ║ ║│ Service layer │ ║ m ║│ │ ║ o ║└────────────────────────────────┘ ║ d ║┌────────────────────────────────┐ ║ e ║│ │ ║ l ║│ Persistence layer │ ║ ║│ │ ╚══════╝└────────────────────────────────┘
  22. 22. Domain objects The domain model consists of a network of interconnected domain objects +-------+ +-------+ | | | | | A +------> B | | | | | +---^---+ +---^---+ | | | | +---+---+ | | | | | C +----------+ | | +-------+
  23. 23. Concepts Each class in the domain model corresponds to a concept from the applica2on problem domain +-------+ +-------+ | | | | | A +------> B | | | | | +---^---+ +---^---+ | | | | +---+---+ | | | | | C +----------+ | | +-------+
  24. 24. Problem domain concepts That is, domain objects are specific to the applica4on’s problem domain +-------+ +-------+ | | | | | A +------> B | | | | | +---^---+ +---^---+ | | | | +---+---+ | | | | | C +----------+ | | +-------+
  25. 25. Airline travel Web applica/on For instance, concepts for an Airline travel app may include: • Airport • Flight • FlightLeg • SpecialDeal
  26. 26. Nouns of the applica0on A popular technique to determine the domain model is to use the nouns in use case descrip3ons as domain objects +-------+ +-------+ | | | | | A +------> B | | | | | +---^---+ +---^---+ | | | | +---+---+ | | | | | C +----------+ | | +-------+
  27. 27. Nouns of the applica0on In other words, the nouns that are used when describing the problem domain suggest domain objects +-------+ +-------+ | | | | | A +------> B | | | | | +---^---+ +---^---+ | | | | +---+---+ | | | | | C +----------+ | | +-------+
  28. 28. Airport class public class Airport { private String name; private String airportCode; public Airport(String name, String airportCode) {...} // other methods }
  29. 29. FlightLeg class public class FlightLeg { private Airport departFrom; private Date departOn; private Airport arriveAt; private Date arriveOn; public FlightLeg(Airport departFrom, Date departOn, Airport arriveAt, Date arriveOn) {...} // other methods }
  30. 30. Flight class public class Flight { private List<FlightLeg> legs; private BigDecimal totalCost; public Flight(List<FlightLeg> legs, BigDecimal totalCost) {...} public boolean isNonStop() {...} public long getTotalTravelTime() {...} // other methods }
  31. 31. Airline travel Web applica/on Hence, the resul-ng domain model is as follows: +---------+ +---------+ +----------+ | |////| | | | | | |////+-----> +------> | | |////| | | | | +---------+ +---------+ +----------+ Flight FlightLeg Airport
  32. 32. Domain model ≠ set of rela0onships It is important to note that business logic does not mean just a set of rela,onships between objects +---------+ +---------+ +----------+ | |////| | | | | | |////+-----> +------> | | |////| | | | | +---------+ +---------+ +----------+ Flight FlightLeg Airport
  33. 33. Domain objects have state and behavior As well as storing data, a domain model object should implement the business logic that operates on the data +---------+ +---------+ +----------+ | |////| | | | | | |////+-----> +------> | | |////| | | | | +---------+ +---------+ +----------+ Flight FlightLeg Airport
  34. 34. Domain objects have state and behavior That is, domain objects should have both state and behavior +---------+ | |////| | |////| +---------+ Flight
  35. 35. Assign behavior to domain objects Calculated values and derived answers should be provided by the object you are talking with +---------+ | |////| | |////| +---------+ Flight
  36. 36. Assign behavior to domain objects Examples: • Calculated value: Flight.getTotalTravelTime() • Derived answer: Flight.isNonStop()
  37. 37. Object-oriented features Pu#ng the business logic inside the domain objects makes it possible to harness the full power of OOP
  38. 38. Object-oriented features Think about how to take advantage of object-oriented features to help solve the business problem • Inheritance • Polymorphism
  39. 39. Anemic domain model A domain model that contains only state and rela1onships (but no behavior) is usually called an anemic domain model
  40. 40. Pervasivity The domain model is a layer that largely permeates the other layers ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║┌────────────────────────────────┐ ║ n ║│ │ ║ ║│ Service layer │ ║ m ║│ │ ║ o ║└────────────────────────────────┘ ║ d ║┌────────────────────────────────┐ ║ e ║│ │ ║ l ║│ Persistence layer │ ║ ║│ │ ╚══════╝└────────────────────────────────┘
  41. 41. Ver$cality The domain model is a ver$cal layer, as all the other layers have dependencies on the domain model ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║┌────────────────────────────────┐ ║ n ║│ │ ║ ║│ Service layer │ ║ m ║│ │ ║ o ║└────────────────────────────────┘ ║ d ║┌────────────────────────────────┐ ║ e ║│ │ ║ l ║│ Persistence layer │ ║ ║│ │ ╚══════╝└────────────────────────────────┘
  42. 42. Centrality That is, even though each layer is responsible for its own concern, all the layers live to serve the domain model ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║┌────────────────────────────────┐ ║ n ║│ │ ║ ║│ Service layer │ ║ m ║│ │ ║ o ║└────────────────────────────────┘ ║ d ║┌────────────────────────────────┐ ║ e ║│ │ ║ l ║│ Persistence layer │ ║ ║│ │ ╚══════╝└────────────────────────────────┘
  43. 43. Independence However, the domain model has no dependency on any other layer ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║┌────────────────────────────────┐ ║ n ║│ │ ║ ║│ Service layer │ ║ m ║│ │ ║ o ║└────────────────────────────────┘ ║ d ║┌────────────────────────────────┐ ║ e ║│ │ ║ l ║│ Persistence layer │ ║ ║│ │ ╚══════╝└────────────────────────────────┘
  44. 44. Domain objects do not need injec/on In general, the domain model will not need dependencies injected +---------+ +---------+ +----------+ | |////| | | | | | |////+-----> +------> | | |////| | | | | +---------+ +---------+ +----------+ Flight FlightLeg Airport
  45. 45. Service Layer
  46. 46. Service Layer The service layer exposes the use cases of the applica1on ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │╔════════════════════════════════╗ │ n │║ ║ │ │║ Service layer ║ │ m │║ ║ │ o │╚════════════════════════════════╝ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  47. 47. Airline travel Web applica/on FlightService implements two applica.on use cases public interface FlightService { List<SpecialDeal> getSpecialDeals(); List<Flight> findFlights(String queryString); }
  48. 48. Coordinator The service layer coordinates calls to the domain objects +------------------------------------------+ | | +--+--+ | | | | | S | +-------+ +-------+ | | e | | | | | | | r +-------> A +------> B | | | v | | | | | | | i | +---+---+ +---^---+ | | c | | | | | e | | | | | | | +---+---+ | | l | +----------> | | | a | | C | | | y +----------------------> | | | e | +-------+ | | r | | | | | +--+--+ | | | +------------------------------------------+
  49. 49. Single entry point The service layer is the single entry point for the business logic +------------------------------------------+ | | +--+--+ | | | | | S | +-------+ +-------+ | | e | | | | | | | r +-------> A +------> B | | | v | | | | | | | i | +---+---+ +---^---+ | | c | | | | | e | | | | | | | +---+---+ | | l | +----------> | | | a | | C | | | y +----------------------> | | | e | +-------+ | | r | | | | | +--+--+ | | | +------------------------------------------+
  50. 50. Façade pa(ern That is, the service layer acts as a façade +------------------------------------------+ | | +--+--+ | | | | | S | +-------+ +-------+ | | e | | | | | | | r +-------> A +------> B | | | v | | | | | | | i | +---+---+ +---^---+ | | c | | | | | e | | | | | | | +---+---+ | | l | +----------> | | | a | | C | | | y +----------------------> | | | e | +-------+ | | r | | | | | +--+--+ | | | +------------------------------------------+
  51. 51. Exposing the service layer The business logic can then be exposed in different ways • Web applica+ons • Web service • Desktop applica+on
  52. 52. Exposing the service layer This thanks to the fact that, no ma0er the means, it is always the same business logic to be executed
  53. 53. Transac'onality Ideally, each applica-on’s use case should represent a single transac'onal unit of work that either succeeds or fails
  54. 54. Transac'onality Since the service layer is the single entry-point, we can apply security and transac+onal requirements @Transactional public class FlightServiceImpl implements FlightService { ... }
  55. 55. Coarse grained & stateless Each method in the service layer should be coarse grained and stateless public interface FlightService { List<SpecialDeal> getSpecialDeals(); List<Flight> findFlights(String queryString); }
  56. 56. Coarse grained Coarse grained: a single method call will accomplish the en1re use case public interface FlightService { List<SpecialDeal> getSpecialDeals(); ... }
  57. 57. Stateless Stateless: calls to a service method do not create any state on the object implemen4ng the service interface public interface FlightService { List<SpecialDeal> getSpecialDeals(); ... }
  58. 58. Service layer The service layer should never have a dependency on the presenta3on layer ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │╔════════════════════════════════╗ │ n │║ ║ │ │║ Service layer ║ │ m │║ ║ │ o │╚════════════════════════════════╝ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  59. 59. Service layer However, the service layer depends on the persistence layer ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │╔════════════════════════════════╗ │ n │║ ║ │ │║ Service layer ║ │ m │║ ║ │ o │╚════════════════════════════════╝ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  60. 60. Persistence layer
  61. 61. Persistence layer The persistence layer is responsible for interfacing with the underlying persistence mechanism ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │╔════════════════════════════════╗ │ e │║ ║ │ l │║ Persistence layer ║ │ │║ ║ └──────┘╚════════════════════════════════╝
  62. 62. Persistence layer The persistence layer knows how to store and retrieve domain objects from the data store ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │╔════════════════════════════════╗ │ e │║ ║ │ l │║ Persistence layer ║ │ │║ ║ └──────┘╚════════════════════════════════╝
  63. 63. Persistence layer It does so in such a way that the service layer does not know which underlying data store is used ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │╔════════════════════════════════╗ │ e │║ ║ │ l │║ Persistence layer ║ │ │║ ║ └──────┘╚════════════════════════════════╝
  64. 64. Persistence concerns That is, the persistence layer isolates the applica0on from changes in the persistence mechanisms ┌──────┐┌────────────────────────────────┐ │ D ││ │ │ o ││ Presentation layer │ │ m ││ │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │╔════════════════════════════════╗ │ e │║ ║ │ l │║ Persistence layer ║ │ │║ ║ └──────┘╚════════════════════════════════╝
  65. 65. Airline travel Web applica/on public interface AccountRepository { Account findByUsername(String username); Account findById(long id); Account save(Account account); }
  66. 66. Airline travel Web applica/on public interface AccountRepository { Optional<Account> findByUsername(String username); Optional<Account> findById(long id); Account save(Account account); }
  67. 67. The service layer is a coordina0on layer The service layer coordinates the persistence layer and the domain model ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║╔════════════════════════════════╗ ║ n ║║ ║ ║ ║║ Service layer ║ ║ m ║║ ║ ║ o ║╚════════════════════════════════╝ ║ d ║╔════════════════════════════════╗ ║ e ║║ ║ ║ l ║║ Persistence layer ║ ║ ║║ ║ ╚══════╝╚════════════════════════════════╝
  68. 68. Persis&ng domain objects The service layer ensures that the appropriate domain objects are loaded and persisted in the use case ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║╔════════════════════════════════╗ ║ n ║║ ║ ║ ║║ Service layer ║ ║ m ║║ ║ ║ o ║╚════════════════════════════════╝ ║ d ║╔════════════════════════════════╗ ║ e ║║ ║ ║ l ║║ Persistence layer ║ ║ ║║ ║ ╚══════╝╚════════════════════════════════╝
  69. 69. Domain objects are unaware The domain objects are un unaware of being persisted ╔══════╗┌────────────────────────────────┐ ║ D ║│ │ ║ o ║│ Presentation layer │ ║ m ║│ │ ║ a ║└────────────────────────────────┘ ║ i ║╔════════════════════════════════╗ ║ n ║║ ║ ║ ║║ Service layer ║ ║ m ║║ ║ ║ o ║╚════════════════════════════════╝ ║ d ║╔════════════════════════════════╗ ║ e ║║ ║ ║ l ║║ Persistence layer ║ ║ ║║ ║ ╚══════╝╚════════════════════════════════╝
  70. 70. Presenta(on layer
  71. 71. Presenta(on layer We can further detail the architecture by spli3ng the presenta5on layer into a Web layer and a user interface layer ┌──────┐┌────────────────────────────────┐ │ ││ User interface layer │ │ D │└────────────────────────────────┘ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  72. 72. Web layer The Web layer has two responsibili-es • It acts as the glue between the service layer and HTTP • It drives the user through the correct pages in the order
  73. 73. HTTP oddi(es The HTTP world is populated with strange creatures • Request parameters • Headers • Cookies
  74. 74. Web logic ≠ business logic These aspects are not business logic specific, and thus should be kept isolated from the service layer
  75. 75. Web logic ≠ business logic It's on the Web layer to hide the details of the Web from the business logic ┌──────┐┌────────────────────────────────┐ │ ││ User interface layer │ │ D │└────────────────────────────────┘ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │╔════════════════════════════════╗ │ n │║ ║ HTTP request? │ │║ Service layer ║ I don't know what │ m │║ ║ you are talking │ o │╚════════════════════════════════╝ about... │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  76. 76. The Web layer is an integra0on layer The Web layer converts the incoming HTTP requests to something that can be handled by the service layer... ┌──────┐┌────────────────────────────────┐ │ ││ User interface layer │ │ D │└────────────────────────────────┘ │ o │╔════════════════════════════════╗ Don't worry! I've │ m │║ Web layer ║ got you covered │ a │╚════════════════════════════════╝ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  77. 77. The Web layer is an integra0on layer ...and then transforms the result from the service layer into a response for the user interface ┌──────┐┌────────────────────────────────┐ │ ││ User interface layer │ │ D │└────────────────────────────────┘ │ o │╔════════════════════════════════╗ Here's the data │ m │║ Web layer ║ you need to │ a │╚════════════════════════════════╝ render the UI! │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  78. 78. Exposing the use cases The Web layer is an adapter that connects to the service layer and exposes it to the end user
  79. 79. Exposing the use cases That is, the Web layer exposes the use cases to the Web
  80. 80. Naviga&on logic Apart serving as an adapter, the Web layer is also in charge of handling the naviga&on logic
  81. 81. Naviga&on logic That is, the Web layer drives the user through the correct pages in the correct order
  82. 82. TODO Web applica-on GET /todo +----------------------------+ | | | | +-----v-----+ +-----+------+ | | | | GET /todo | List of | GET /todo/:id | Individual | +-----------> TODOS +----------------> TODO | | | | | +--+-------^+ +------------+ | | | | | | | | +-------+ POST /todo
  83. 83. Naviga&on logic The naviga*on is bound to the Web layer only. There is no naviga*on logic in the service layer or in the domain model
  84. 84. Naviga&on logic By guiding the user through the site, the Web layer determines the user experience
  85. 85. Thin layer The Web layer is a thin layer • It is just a bridge between the Web and the service layer • It does not contain any business logic
  86. 86. Web layer’s dependencies The Web layer depends on the service layer, to which it delegates the actual processing ┌──────┐┌────────────────────────────────┐ │ ││ User interface layer │ │ D │└────────────────────────────────┘ │ o │╔════════════════════════════════╗ │ m │║ Web layer ║ │ a │╚════════════════════════════════╝ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  87. 87. Web layer’s dependencies The Web layer depends on the domain model, in that informa5on from the Web could be converted to domain objects ┌──────┐┌────────────────────────────────┐ │ ││ User interface layer │ │ D │└────────────────────────────────┘ │ o │╔════════════════════════════════╗ │ m │║ Web layer ║ │ a │╚════════════════════════════════╝ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  88. 88. User Interface layer
  89. 89. User Interface layer The user interface layer is responsible for presen&ng the applica3on to the end user ┌──────┐╔════════════════════════════════╗ │ │║ User interface layer ║ │ D │╚════════════════════════════════╝ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  90. 90. User Interface layer The user interface layer renders the response generated by the Web layer into the form requested by the user’s client ┌──────┐╔════════════════════════════════╗ │ │║ User interface layer ║ │ D │╚════════════════════════════════╝ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  91. 91. User Interface layer For instance: • A Web browser will probable request a HTML page • A Web service may want an XML (or JSON) document • Another client could request a PDF document
  92. 92. Many view technologies are available There exists a plethora of of view technologies • JSP pages • JSF pages • Handlebars
  93. 93. Many view technologies are available However, the naviga/on logic and the need of calling the service layer does not change with the specific view technology
  94. 94. Many view technologies are available That is why the user interface and the web layer are split ┌──────┐╔════════════════════════════════╗ │ │║ User interface layer ║ │ D │╚════════════════════════════════╝ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  95. 95. User interface layer’s dependency The user interface layer may depend on the domain model ┌──────┐╔════════════════════════════════╗ │ │║ User interface layer ║ │ D │╚════════════════════════════════╝ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  96. 96. User interface layer’s dependency Indeed, it is o+en convenient to expose and render domain objects ┌──────┐╔════════════════════════════════╗ │ │║ User interface layer ║ │ D │╚════════════════════════════════╝ │ o │┌────────────────────────────────┐ │ m ││ Web layer │ │ a │└────────────────────────────────┘ │ i │┌────────────────────────────────┐ │ n ││ │ │ ││ Service layer │ │ m ││ │ │ o │└────────────────────────────────┘ │ d │┌────────────────────────────────┐ │ e ││ │ │ l ││ Persistence layer │ │ ││ │ └──────┘└────────────────────────────────┘
  97. 97. User interface layer’s dependency This is useful when we start to use forms in our applica2on, as it makes possible to objec*fy form fields directly into domain objects
  98. 98. References
  99. 99. References • S. Ladd, K. Donald, Expert Spring MVC and Web flows, Apress PublicaBons • M. Deinum, K. Serneels, Pro Spring MVC: with Web flows, Apress PublicaBons • Chris Richardson, POJOs in AcBon, Manning PublicaBons
  100. 100. References • Mar%n Fowler, Anemic Domain Model • StackOverflow, What Does it Mean to “Program to an Interface”?

×