In MVC / MVP / MVPC, where do you put your business logic?

in the MVC / MVP / MVPC design pattern, where do you put your business logic? No, I do not mean the ASP.NET MVC Framework (aka "Tag Soup").

Some say you should put it in the "Controller" in MVC / MVPC or "Presenter". But others believe that this should be part of the Model.

What do you think and why?

+24
model-view-controller mvp puremvc
Feb 10 '09 at 21:14
source share
6 answers

Here is how I see it:

The controller is designed for application logic; logic that is specific to how your application wants to interact with the area of ​​knowledge to which it relates.

The model is designed for logic that is application independent. that is, logic that is valid in all possible applications of the field of knowledge to which it relates.

Consequently, almost all business rules will be in the model.

I find a useful question to ask myself when I need to decide where to put some kind of logic, "is this always the case, or just for the part of the application that I am currently encoding?"

+64
Feb 10 '09 at 21:17
source share

I have an ASP.NET MVC application structure, the workflow is as follows:

Controller β†’ Services β†’ Repositories

The level of services is higher, where all the business logic takes place. If you place your business logic at your controller level, you will lose the ability to reuse this business logic in other controllers.

+35
Feb 10 '09 at 21:36
source share

I do not believe that it belongs to the controller, because when it is built in there, it cannot exit.

I think that MVC should have another layer that is introduced between them: the service level, which is displayed when used. It contains business logic, knows about units of work and transactions, and deals with model objects and persistence to perform its tasks.

The controller has a link to the service, which should fulfill its use case. He takes care of decoupling requests for objects that the service can work with, calls the service, and sends a response to send back to the view.

With this arrangement, the service can be used on its own, even without a pair of controllers / views. This can be a local or remote object, packaged and deployed in any way that the controller deals with.

The controller is now more closely associated with the view. In the end, the controller that you will use for the desktop is likely to be different from the one used for the web application.

I think this design is more service oriented.

+8
Feb 10 '09 at 21:25
source share

Put your business logic in the domain and save your separte domain. I prefer Presenter -> Command (Message command use NServiceBus) -> Domain (with BC contour limited) -> EventStore -> Event handler -> Repository (reading model). If the logic is not suitable for the domain, then use the service level.

Read the article by Martin Fowler, Eric Evan, Greg Young, and Udi Dahan. They define a very clear picture.

Here is an article written by Mark Nijhof: http://elegantcode.com/2009/11/11/cqrs-la-greg-young/

+3
Sep 17 '10 at 3:52
source share

For the sake of all this, put it in the model !!!!!

Of course, some of the user interactions should be in a view that will be related to your application and business logic, but avoid this as much as possible. Ironically, following the principle as little as possible, since the user "works" in your GUI and so much during the "sending" or requesting actions, provides a better user interface and usability, and not vice versa. At least for business applications.

+2
Aug 17 '10 at 17:49
source share

You can put it in two places. Controller and presentation level. With some logic at the presentation level, you can limit the number of requests back to the architecture, which adds to the load on the system. Yes, you need to enter the code twice, but sometimes this is what you need to quickly respond to the user.

I like what was said here ( http://www.martinhunter.co.nz/articles/MVPC.pdf )

"With MVPC, the presenting component of the MVP model is split into two components: the presenter (view control logic) and the controller (abstract assignment control logic).

+1
Feb 10 '09 at 21:36
source share



All Articles