ASP.NET MVC - 业务逻辑是否应该存在于控制器中?
Derik Whitaker 发布了 文章 几天前,它触及了一些我一直好奇的点time:业务逻辑应该存在于控制器中吗?
到目前为止,我见过的所有 ASP.NET MVC 演示都将存储库访问和业务逻辑放在控制器中。 有些甚至还在那里进行验证。 这会导致控制器相当大、臃肿。 这真的是MVC框架的使用方式吗? 看起来这最终会导致大量重复的代码和逻辑分布在不同的控制器上。
Derik Whitaker posted an article a couple of days ago that hit a point that I've been curious about for some time: should business logic exist in controllers?
So far all the ASP.NET MVC demos I've seen put repository access and business logic in the controller. Some even throw validation in there as well. This results in fairly large, bloated controllers. Is this really the way to use the MVC framework? It seems that this is just going to end up with a lot of duplicated code and logic spread out across different controllers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
业务逻辑确实应该在模型中。 你应该瞄准胖模型、瘦控制器。
例如,不要有:
我宁愿有:
这假设税收是由外部服务计算的,并且要求您的模型了解外部服务的接口。
这会让你的控制器看起来像:
或者类似的东西。
Business logic should really be in the model. You should be aiming for fat models, skinny controllers.
For example, instead of having:
I would rather have:
This assumes that tax is calculate by an external service, and requires your model to know about interfaces to your external services.
This would make your controller look something like:
Or something like that.
我喜欢 Microsoft Patterns & 提供的图表。 实践。 我相信“一图胜千言”这句格言。
I like the diagram presented by Microsoft Patterns & Practices. And I believe in the adage 'A picture is worth a thousand words'.
这是一个令人着迷的问题。
我认为有趣的是,大量示例 MVC 应用程序实际上未能遵循 MVC 范例,即真正将“业务逻辑”完全放置在模型中。 Martin Fowler 指出,MVC 并不是四人帮意义上的模式。 相反,如果程序员要创建超出玩具应用程序的内容,则必须添加模式,这是范例。
因此,简短的回答是“业务逻辑”确实不应该存在于控制器中,因为控制器具有处理视图和用户交互的附加功能,并且我们希望创建仅具有一个目的的对象。
更长的答案是,在将逻辑从控制器移动到模型之前,您需要对模型层的设计进行一些思考。 也许您可以使用 REST 处理所有应用程序逻辑,在这种情况下,模型的设计应该相当清晰。 如果没有,您应该知道将使用什么方法来防止模型变得臃肿。
This is a fascinating question.
I think that its interesting that a large number of sample MVC applications actually fail to follow the MVC paradigm in the sense of truly placing the "business logic" entirely in the model. Martin Fowler has pointed out that MVC is not a pattern in the sense of the Gang Of Four. Rather, it is paradigm that the programmer must add patterns to if they are creating something beyond a toy app.
So, the short answer is that "business logic" should indeed not live in the controller, since the controller has the added function of dealing with the view and user interactions and we want to create objects with only one purpose.
A longer answer is that you need to put some thought into the design of your model layer before just moving logic from controller to model. Perhaps you can handle all of app logic using REST, in which case the model's design should be fairly clear. If not, you should know what approach you are going to use to keep your model from becoming bloated.
您可以查看 Stephen Walther 撰写的这个很棒的教程,其中显示 验证服务层。
You can check this awesome tutorial by Stephen Walther that shows Validating with a Service Layer.
业务逻辑不应包含在控制器中。 控制器应尽可能精简,最好遵循以下模式:
另外,控制器可以包含一些应用程序逻辑。
那么我的业务逻辑应该放在哪里呢? 在模型中。
什么是模型? 这是一个好问题。 请参阅Microsoft 模式和实践文章(感谢 AlejandroR 的出色发现)。 这里有三类模型:
当然,MVC 是一种有不同变体的范例。 我这里描述的是MVC仅占据顶层,参见维基百科上的这篇文章
Business Logic should not be contained in controllers. Controllers should be as skinny as possible, ideally follow the patter:
Additionally controllers can contain some application logic.
So where do I put my business logic? In Model.
What is Model? Now that's a good question. Please see Microsoft Patterns and Practices article (kudos to AlejandroR for excellent find). In here there are three categories of models:
Of course, MVC is a paradigm that comes in different varieties. What I describe here is MVC occupying top layer only, vide this article on Wikipedia
如果您使用依赖注入器,您的业务逻辑将转到它们,因此您将获得整洁干净的控制器。
If u use Dependency Injectors your business logic will go to them and hence you will get neat and clean controllers.