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

落爺英雄遲暮 提交于 2019-11-29 19:54:06

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?"

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.

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.

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/

FastAl

By all means, 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.

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)."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!