Object-Oriented JavaScript MVC – Is This a Good Example of a Service Layer?

javascriptmvcobject-orientedservice

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 ?

Best Answer

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.