in MVC/MVP/MVPC where do you put your business log

2019-01-30 07:03发布

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 people say you should put it in the "Controller" in MVC/MVPC or "Presenter". But, others think it should be part of the Model.

What do you think and why?

6条回答
▲ chillily
2楼-- · 2019-01-30 07:30

I don't believe it belongs in the controller, because once it's embedded there it can't get out.

I think MVC should have another layer injected in-between: a service layer that maps to use cases. It contains business logic, knows about units of work and transactions, and deals with model and persistence objects to accomplish its tasks.

The controller has a reference to the service that it needs to fulfill its use case. It worries about unmarshalling requests into objects the service can deal with, calls the service, and marshals the response to send back to the view.

With this arrangement, the service is usable on its own even without the controller/view pair. It can be a local or remote object, packaged and deployed any way you wish, that the controller deals with.

The controller now becomes bound more closely to the view. After all, the controller you'll use for a desktop is likely to be different than the one for a web app.

I think this design is more service-oriented.

查看更多
神经病院院长
3楼-- · 2019-01-30 07:33

For heaven's sake, put it in the model!!!!!

Of course some of the user interaction will have to be in the view, that will be related to your app and business logic, but avoid this as much as possible. Ironically following the principle of doing as little as possible as the user is 'working' in your GUI and as much during 'submit' or action requests makes for a better user experience and usability, not the other way around. At least for line-of-business apps.

查看更多
再贱就再见
4楼-- · 2019-01-30 07:34

The way I have my ASP.NET MVC application structure the workflow looks like this:

Controller -> Services -> Repositories

The Services layer above is where all the business logic takes place. If you put your business logic in your Controller layer, you lose the ability to re-use that business logic in other controllers.

查看更多
ら.Afraid
5楼-- · 2019-01-30 07:34

You can put it in two places. The Controller and in the Presentation layer. By having some of the logic in the Presentation layer, you can limit the number of requests back into the architecture which adds load to the system. Yeah, you have to code twice, but sometimes this is what you need for a responsive user experience.

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

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

查看更多
唯我独甜
6楼-- · 2019-01-30 07:41

This is how I see it:

The controller is for application logic; logic which is specific to how your application wants to interact with the domain of knowledge it pertains to.

The model is for logic that is independent of the application. i.e. logic that is valid in all possible applications of the domain of knowledge it pertains to.

Hence nearly 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 logic is "is this always true, or just for the part of the application I am currently coding?"

查看更多
成全新的幸福
7楼-- · 2019-01-30 07:45

Put your business logic in domain and keep your domain separte. I prefered Presenter -> Command (Message command use NServiceBus) -> Domain (with BC Bounded Context) -> EventStore -> Event handler -> Repository (read model). If logic is not fit in domain then use service layer.

Please read article from Martin fowler, Eric Evan, Greg Young and Udi dahan. They have define very clear picture.

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

查看更多
登录 后发表回答