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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.