Skip to main content

Do I really need a service layer?


My web application is written using Spring MVC + Hibernate.



  • My model is "Customer" entity POJO.

  • I have got a DAO object "CustomerDAO", its method "saveCustomer(c)" contains the code interacting with Hibernate;

  • Then I created a " CustomerService with a "saveCustomer(c)" method who simply pass the customer object to the dao for saving;

  • Finally there are "CustomerController" and customer.jsp, who are responsible for the view layer, the jsp's form fields are bound to a Customer object on the controller side. The controller calls the service.



I saw a lot of applications follow this (best) practice but I'm wondering why I would need a service layer.



Maybe it's useful for decoupling purpose: I can show a universal facade to the controllers and inject into the service HibernateDAO, GaeDAO, MyDAO, and so on.... But I could do that without the service, too: using an interface.



I also tought: validation. I'll make my Customer validation in the service but.... it's much more convenient to validate in Spring controller.



Help me understand the concept please :)


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You don't need a service layer. However it helps you to


    Decouple your components
    You can enforce specific business rules in your service layer which should be agnostic to your repository
    Let a service facade one or more repositories. Let's consider the following sample




    class Service {
    private DatabaseBarRepo barRepo;
    private DatabaseFooRepo fooRepo;

    @Transactional
    public void serviceRoutine() {
    barRepo.doStuff();
    fooRepo.doStuff();
    }
    }


    Here we let two separate repositories take part in the same transaction. This is specific for databases albeit the principles are valid for other systems as well.

    ReplyDelete
  2. The key is the transactional behavior. If you don't have a service layer, where will you demarcate transactions?


    in the presentation layer: that's not where transaction demarcation belongs, and you wouldn't be able to use declarative transaction demarcation
    in the DAO layer: you would not be able to create a customer and its contact (for example) in a single transaction, because it would use two different DAOs


    Moreover, you want your UI layer as simple as possible, and the business code (which sometimes is much more complex than sinply calling a DAO method) isolated in specific components. This allows


    unit testing the UI layer by mocking the service layer
    unit testing the service layer (the business code) by mocking the DAO layer

    ReplyDelete
  3. Right now, your service layer is trivial because your service is a trivial wrapping around database accesses. Is that all the app is? If not, when you start building the non-trivial parts, your service layer will expand.

    ReplyDelete
  4. You can save the verbosity by having a BaseService which has all the CRUD operations, delegating to a BaseDAO injected in it.

    Apart from CRUD, almost everything else has separate logic for the business logic and for the database-specific operations. And another thing - you can have a transaction span multiple database operations by annotating the service-layer methods with @Transactional

    ReplyDelete
  5. There are other things that you at service layer. Sometime its about applying business rules before passing any action to DAO. Sometimes one service needs to interact with other services, DAO for the business rules requirement.

    Tell us how you'll do without server layer and with an interface..high level idea, will help me to tell you more.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex