6

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 ?

  • 2
    • 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, 2013 at 12:57
    • Martin Fowler calls it Layer
      – Havelock
      Mar 30, 2013 at 13:05

    1 Answer 1

    7

    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

    6
    • 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 ?
      – johnlemon
      Mar 30, 2013 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, 2013 at 13:24
    • @Gordon - but Models by definition contain business logic and Services are Models.
      – johnlemon
      May 7, 2013 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, 2013 at 20:46
    • @Gordon - I know about the Fowler's Service Layer but is there any reference about the Model Layer? I found a bit confusing the term "Model" at this point. Also the "Service Layer" seems to be a concept from Domain Driven Development. Could you make a clear distinction between a pure MVC application with Services and a MVC&DDD application? What would be the layers and components in both cases?
      – johnlemon
      May 8, 2013 at 3:51

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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