Friday, 8 June 2012

MVC:Difference between a service(gateway) class and a Utility( Helper class) in MVC


  • A Service class/interface provides a way of a client to interact with some functionality in the application. This is typically public, with some business meaning. For example, a TicketingService interface might allow you to methods  buyTicket,  sellTicket and so on.
    Whereas A helper class tends to be hidden from the client and is used internally to provide some boiler plate work that has no business domain meaning. For example, let's say you wanted to convert a date into a timestamp in order to save it to your particular datastore. You might have a utility class calledDateConvertor with a convertDateToTimestamp method that performs this processing.
    2. service class has some context within the application whereas helpers are more generic and don't care what app they are helping.
    3. Usage
    Generally, in well designed system, most classes have quite clear responsibility or function in that they deal with a specific entity or set of entities in the model.
    • Account, Account Factory, Account Repository, etc
    • Customer, Customer Factory, Customer Repository, etc
    When you have functionality that does not belong with any particular entity it can be difficult to find a correct place for it to sit. I.e something that encapsulates a process that involves both an Account AND aCustomer .
    So, that's where a service comes in. Its where you put code that does not naturally belong to one entity/component or another.
    helper as a kind of strategy class. To me its a place to put code that needs to be re-used by various classes 

    In short

    Service Class : Contain Business logic. 
    Helper Class : this class is one type of reusable component.

No comments:

Post a Comment