Main location in a project with MVC pattern

Asked

Viewed 282 times

4

Within a project guided by the MVC standard where the main() application should be located? Thinking a little I imagined it was the model, because it contains the most "complex" parts of code, but I’m not sure.

  • 1

    The main() is the main(), is not even M, nor V, nor C. The main() does not have to have "complex" parts no, it should only initiate the application.

1 answer

9


You are relying on a false premise, that the whole part of the system has to be either M (model), or C (controller) or V (view).

The case of main() is the most emblematic, he is none of them and is located outside the MVC. In the MVC model, M corresponds to the classes that model your domain, while V and C dictate how the user (or some other external system) can manipulate your domain.

The MVC tells you how you organize your model and how it interacts with the external world (or how the external world interacts with it). In particular, it states that you should not pollute your domain with visualization logic or flow control logic. And if possible, do not pollute the visualization with flow control logic or your flow control with visualization logic. This then says nothing and has no relation to things that do not belong to his domain and do not interact directly with him, such as main() for example (or a library for writing XML documents that your project uses, if you want a different example).

In addition, code complexity is unrelated to whether or not it is part of the model. The MVC proposes to reduce total complexity by clearly dividing the three layers, but it says nothing about the complexity of each layer alone. In fact, the controller tends to be the most complex layer because it is attached to both the model and the view.

Finally, the main() typically has the purpose of climbing everything necessary, which possibly means creating/instantiating the controller and often also the model and view. Although the main() can create the controller, it is not part of it, as it does not manage the data flow of the application. The main() is also not in the model as it is not a fundamental part of your domain (and if you put the main() in the model, it would be subverting MVC, since it also creates the controller). Obviously it is also not view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.