|
Pros and cons of Service Facades
Just like is the case with DTO's, there are cases when Service Facades make a lot of sense and there are cases when they just add meaningless overhead. Let's have a look at some pros and cons:
* Con: Service Facades add an extra layer that does not do much apart from delegate to the actual service. This argument appeals especially to Java EE developers that have bad memories of the EJB 1.0 based multi-tier architectures we set up in the beginning of this century. ;-)
* Pro: Service Facades can (should!) be made responsible for mapping from DTO's to domain objects and back. Service Facades are invoked with DTO's as arguments, map them to domain objects, invoke the actual service (or services), map the result back to DTO's and return those DTO's to the client. Actually, to make sure your Service Facades only adhere to Single Responsibility Principle, you should factor the mapping logic out to separate DTO2DOMapper and DO2DTOMapper classes.
* Pro: Service Facades can function as the transaction boundary of your application, i.e. a transaction is started for the duration of a request to the Service Facade. Instead of having to define the transaction attributes of all your services, you can assume that all service invocation arrive through the Service Facade. Actually, setting your Service Facade to be the transaction boundary is obligatory when you want to invoke more than one service during the handling of one request or when you want the Service Facade to map lazily loaded domain objects to DTO's. In that case you end with something akin to the open EntityManager in view pattern; a transaction is started that lasts from the moment the incoming DTO's are translated to domain objects to the moment the resulting DTO's are returned to the client. (BTW, if you want to enforce that all services invocations go through the Service Facade you can set their transaction attribute to MANDATORY. If a transaction hasn't already been started when such a service is invoked, an exception is thrown)
* Pro: The Service Facade pattern forces you to think about the interface of your application. Instead of letting the client invoke all your services, you get to decide which ones to actually expose.
Hmm, I can't seem to think of many disadvantages to the Service Facade pattern. ;-) Apart from the "overhead" argument and that is a subjective thing. Feel free to add more cons of this pattern to the comment section! |
|