Back to list
Views:   4.4K
Replies:  1

What is the point of a service layer in Patterns in Action 4.5?

As far as I can tell the Service class mostly just wraps DAO calls.  If there is only one consumer of the service (e.g. one win form application), then what is the point of a service layer?  What's the big deal in just calling the DAL directly from the winform application?
Mr. Query, Aug 20, 2013
Reply 1
It is possible to invoke the DAL directly from the code behind and this would work in the Patterns in Action example.
However, this wouldn't be a good idea. 

There is several reasons for having a Service Layer. Two important ones are 
  • Validation, and 
  • Transaction Management 
Validation: The best place for complex business rule validation is the service layer. Here is an example of a complex rule: only approve a loan application if the user is 18 years or older, has a credit history, has a bank account with the financial institution, and has lived in the same house for at least 1 year. 

Transaction Management: When multiple objects are committed to the database, you will want to do this within the context of a transaction (Unit of Work pattern).  The Art Shop has a good example in which the content of the shopping cart are turned into an order. This involves DELETES and INSERTS in 4 tables, and the transaction ensures that the entire process either fails or succeeds, so there are no situations where half of the shopping cart is an order and the rest is still in the cart. 

Hope this helps. 

Jack Poorte, Aug 26, 2013
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.