1

I am wondering what a service layer is in MVC. I like to understand concepts through a game of monopoly as it's relatable to a lot of people and fairly easy to explain whilst allowing for some good OOP and SOLID principles to be implemented . Can someone tell me if this is the job of a service layer in an example described here:

1- The controller responds to a user rolling a dice by calling a controller funciton named roll Dice.

2- Now when we roll the dice in monopoly a handful of things could occur. First the current square count of the players turn that it is needs to be updated. We then need to check if the player is in jail or not , we then need to see what square the player has landed on and give the user option etc etc. I have multiple classes for Dice, Board, Square, Player etc which all have very specific methods and as a result of these methods data is changed. I often pass dependancies via dependancy injection as either arguments to other methods or as properties when instantiating a new class to achieve polymorphism without needing if/else statements on multiple possibilities.

3- Although I have various methods which do one thing e.g. updateCount() or isPlayerInJail(), at some point these methods need to be put in a bigger piece of logic to all interact with each other. Originally I did this in the controller but found achieving polymorphism is impossible unless making new classes which hold data inside the controller ( which I obviously wouldn't want to do due to (SOC) ). So I then decided to do all my logic in the Model, where inside my controller I call a method on a class, and pass in dependancies. Now this method is always a lot bigger than all the other methods which only do one thing. Instead this method controls the logic of what happens sequentially when the player rolls the dice. 4. My final questions , is this larger method an example of a service, and this service should be called directly from my controller , the same as I do now , but instead of calling this method from inside the class directly from the model as a method, I should call it from the service layer , outside the class ? This service method will change data inside the model by calling its smaller methods ?

My game is solely a frontend project written fully in javascript in which two people must be in front of each other to play.

Each implantation works and whereas I accept a MVC pattern with a service layer is probably overkill for such a project, the concepts learnt I am happy will put me in good stead for a larger application where MVC is designed for Law and order.

Am I on the right lines here regarding what a service layer is used for ?

1 Answer 1

3

There is a misconception here. The Model-View-Controller pattern does not include a service layer, nor does it care if one exists. I asked a similar question some number of years ago that was focused on a C# implementation for a web framework. I haven't accepted an answer yet on that question, not because the answers were bad, but because none of the possibilities feel very "clean" to me. You are experiencing the same problem with the MVC design pattern from a different type of application (arguably a more traditional implementation of MVC than a web app).

The problem is that MVC is not an application architecture. It solves user interface stuff, but doesn't describe how to organize collaboration logic. You have some number of objects that need to collaborate to achieve a particular use case. Rather than trying to invent a place to put this logic in MVC, consolidate this logic some place so that:

  • It is easy to reason about the code.
  • It is sufficiently easy to write tests.
  • And you can reduce dependencies down to only those required to execute the use case.
    • Which helps make the code sufficiently easy to test.

You won't find a perfect place for this collaboration logic. Choose a place, and be consistent, while keeping in mind that you can deviate from this structure where it makes sense.

8
  • Fantastic answer. Thanks Greg. This leads directly onto another concept I often struggle with which is SRP. If I have an array of objects each with multiple properties or other classes , if I want to change state of that data from inside the class the array is built in, would you say that it still has a Single Resp to “manage that array” or would you suggest the 20-30 methods which do different things to various properties of the array objects should all be separated into their own class which receive the array as a dependancy? This way each class only has 1/2 methods but is doing 1/2 Nov 17, 2022 at 6:39
  • One thing. And doing that one thing well. But this means I have to have a class which potentially just houses an array with a method of getArray which is then injected into all other classes either as an argument to a method or as a constructor property? This matter always confuses me! Nov 17, 2022 at 6:41
  • 1
    @KevinGreetham: doing one thing is a common misconception about the SRP. Robert Martin has a good write -up: blog.cleancoder.com/uncle-bob/2014/05/08/… Nov 17, 2022 at 12:16
  • Greg. Everywhere I look everyone describes method as having to be roughly 5 lines of code. 90% of my methods are, but some are not. E.g following your advice here, I decided to put my collobartion logic in my rich domain model. Surely this means one of the methods will be a lot longer as to collaborate correctly and calls various other smaller methods in this bigger one? Does this seem correct? Nov 21, 2022 at 14:46
  • 1
    @KevinGreetham: collaboration logic isn't any different than a calculation. My previous comment still stands. Use your best judgement. There is no rule, just a guideline that you can apply where it makes sense. Nov 21, 2022 at 16:56

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.