MVC中的业务逻辑 [英] Business logic in MVC

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

问题描述

我有两个问题:

Q1。 业务逻辑究竟在于MVC模式?我在模型和控制器之间感到困惑。

Q1. Where exactly does "business logic" lie in the MVC pattern? I am confused between Model and Controller.

Q2。 商业逻辑是否与业务规则相同?如果没有,有什么区别?

Q2. Is "business logic" the same as "business rules"? If not, what is the difference?

如果您可以用一个小例子解释,这将是很棒的。

It would be great if you could explain with a small example.

推荐答案

业务规则进入模型。

说你正在显示邮件列表的电子邮件。用户单击其中一个电子邮件旁边的删除按钮,控制器通知模型删除条目N,然后通知模型已更改的视图。

Say you were displaying emails for a mailing list. The user clicks the "delete" button next to one of the emails, the controller notifies the model to delete entry N, then notifies the view the model has changed.

也许管理员的电子邮件不应该从列表中删除。这是一个商业规则,知识属于模型。该视图可能最终表示此规则 - 也许该模型公开了一个IsDeletable属性,该属性是业务规则的函数,因此视图中的删除按钮对某些条目禁用,但是该规则本身不包含在视图中。

Perhaps the admin's email should never be removed from the list. That's a business rule, that knowledge belongs in the model. The view may ultimately represent this rule somehow -- perhaps the model exposes an "IsDeletable" property which is a function of the business rule, so that the delete button in the view is disabled for certain entries - but the rule itself isn't contained in the view.

该模型最终是您的数据的守门人。您应该可以测试业务逻辑,而不用触摸UI。

The model is ultimately gatekeeper for your data. You should be able to test your business logic without touching the UI at all.

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

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