MVC:凡把业务逻辑? [英] MVC: Where to put business logic?

查看:119
本文介绍了MVC:凡把业务逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我见过很多这样的问题,但没有足够的理由落后。如果我的问题是不够好,应予删除我就明白了。

我已经采取了看,例如,<一个href=\"http://stackoverflow.com/questions/534233/in-mvc-mvp-mvpc-where-do-you-put-your-business-logic\">this和45+投了回答说,他建议你把业务逻辑模型,这听起来pretty逻辑。

I have taken a look at, for example, this and a 45+ voted up answer says he advises you to put the business logic in the model, which sounds pretty logical.

不过,我的第一个大型项目我已经用我所有的BL完全控制器做的,因为我没有质疑这些东西,看着它是如何在的AccountController 这是,如果你选择使用MVC形式验证自动添加。所有方法都看在配备BL酿pretty。或者,也许这是无以复加,这是可能的code最少的,我俯瞰的东西呢?

However, my first large project I have done with all my BL fully in the controllers, because I didn't question these things and looked how it is done in the AccountController which is the automatically added if you choose MVC with form authentication. All the methods look pretty stuffed with BL. Or maybe it is the least amount of code that was possible to be added and I am overlooking things?

在YouTube上的人问我,他是对通过将所有业务逻辑,他的模型,并在第一次我没有!然后我就开始想,也许他是对的!?

A person on youtube asked me whether he is right by putting all the logic into his models and at first I was no! Then I started thinking that maybe he was right!?

所以,毕竟我在哪里把我的商业逻辑?如果它是在模型类,那么,多少code应该算是这是一个方法,一个健康的金额控制器?一号线在大多数呼叫从模型的一些方法在控制器中,然后返回到视图?

So, after all, where do I put my business logic? If it is in models classes, then, how much code should be considered a healthy amount in a method which is in controller? One line to call some method from the model in a controller at most and then a return to the view?

推荐答案

我preFER把域逻辑模型中的一对夫妇的原因。

I prefer to put domain logic in the model for a couple of reasons.


  1. 该模型应该有它没有UI code,因而更容易进行测试。只要有可能,我喜欢写任何UI code之前必须有一个充分的工作(即完整的测试覆盖率)模型。该控制器可以相信该模型是在做正确的事情,只是处理UI的关注。

  1. The model should have no UI code in it and thus be easier to test. Whenever possible, I like to have a fully working (meaning complete test coverage) model before writing any UI code. The controller can trust that the model is doing the right thing and just deal with UI concerns.

如果你把域逻辑控制器,它不是那么容易不同的应用程序之间共享,甚至是不同的控制器之间。

If you put domain logic in a controller, it's not as easy to share between different apps, or even between different controllers.

这篇关于MVC:凡把业务逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆