lundi 23 août 2021

In an MVC-type architecture, where should a Builder class go? Model or Controller?

Suppose you have the following Architecture, and the following definitions of MVC (I know this may not be totally conformant to the "original" MVC as described by Martin Fowler):

  • Model: Responsible for direct data access (not especially for actually holding the data, but for accessing it (for example accessing a database or processing HTTP requests). Exposes some kind of interface to the data for the Controller to work with that data.

  • Controller: Contains the actual business logic, processes data coming from the Model and gives it to the views. Also processes data coming from the user and gives it to the Model to store it.

  • View: Responsible for displaying the Data (and other relevant Information) to the user, for example in the form of some GUI.

I know I am probably mixing up stuff here that isn't really supposed to be mixed in the way I do, but I am still interested in your assessment of this situation:

I have a class (in the model part of my application) that is rather complicated (multiple different fields, some are other object types as well, the value of some of those depends on other values) to actually instantiate in a sensible way. Since I figured that the Model isn't supposed to hold any real logic apart from basic getters, setters and maybe some minor input validation (although everything that makes its way to the Model should already be processed by some Controller); it seems logical for me to introduce a Builder class that takes care of the complicated creation of this object I am speaking of.

Intuitively, I would put this Builder in the Controller layer, but there could also be valid points for putting it in the Model layer. My rationale for Controller is the following:

Even though a Builder isn't especially containing actual "business logic", it may still need to access other data which it then uses in the creation process of the object it is supposed to build. For example there might be some value that the user can supply but that will just be set to a default value if the user doesn't supply it. Or the builder may have to get references to other objects for duplication checking some input from the user. I may just have a wrong understanding of the MVC architecture here, but in my mind, the Model shouldn't make ANY requests to other model classes, but should purely communicate to some underlying data storage like a database, and the Controller classes.

Aucun commentaire:

Enregistrer un commentaire