From time to time I find there is a need to build a service class. Something I can't really attach inside a model in order to respected GRASP and SOLID.

So I created a class called AccountTransferService.

  • Are service classes good design practice ?
  • Do they represent a Model ?
  • If a Service is not a Model what are they in a MVC arhitecture ?

    • 1
      Everything that is business logic is the model. The Model in MVC may well contain several different things, including business objects and "services". – deceze Mar 30 '13 at 12:57
    • Martin Fowler calls it Layer – Havelock Mar 30 '13 at 13:05

    Are service classes good design practice ?

    As long as they do not contain business logic or state but only orchestrate other components, yes. For additional information, see http://martinfowler.com/eaaCatalog/serviceLayer.html

    Do they represent a Model ?

    The Model in MVC is your application minus the User Interface (V+C). As such, a Service is part of the Model, but it is not a Model in the sense of a Domain Model. They are also not what frameworks like Code Igniter usually (and incorrectly) mean when they talk about models.

    If a Service is not a Model what are they in a MVC arhitecture ?

    See above, they part of the Model

    | improve this answer | |
    • 1
      damn ... i was too late – mefisto Mar 30 '13 at 13:09
    • If my /models folder contains a lot of ORM generated classes, would It be a good idea to place the services in a separate folder /models/services or is there another practice for this ? – nascar Mar 30 '13 at 13:21
    • @danip Where to put stuff in the filesystem is ultimately up to you or your coding convention. For instance, if you follow PSR-2 you will have a close match between class names and folders. – Gordon Mar 30 '13 at 13:24
    • @Gordon - but Models by definition contain business logic and Services are Models. – nascar May 7 '13 at 19:51
    • @danip Services are part of the Model (Layer). That layer contains Domain Models, which contain your business logic. Services usually dont contain any business logic of their own, but only orchestrate other components. But I am repeating myself :) – Gordon May 7 '13 at 20:46

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.