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

本文是小编为大家收集整理的关于MVC中的业务逻辑的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有2个问题:

Q1. "业务逻辑"到底在哪里?我在模型和控制器之间感到困惑.

Q2. "业务逻辑"与"业务规则"相同吗?如果没有,有什么区别?

如果您可以用一个小例子解释,那就太好了.

推荐答案

业务规则进入模型.

说您正在显示邮件列表的电子邮件.用户单击其中一封电子邮件旁边的"删除"按钮,控制器通知模型要删除条目n,然后通知视图该模型已更改.

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

该模型最终是您数据的网守.您应该能够在不触摸UI的情况下测试您的业务逻辑.

其他推荐答案

所有的拳头:
我相信您正在混合MVC模式和基于N层的设计原理.

使用MVC方法并不意味着您不应该分层应用程序.
如果您看到MVC更像是演示层的扩展.

可能会有所帮助. 如果您在MVC模式中放置非呈递代码,您可能很快就会出现复杂的设计.
因此,我建议您将业务逻辑放入单独的业务层.

只要看看这个: Wikipedia wikipedia关于多层建筑的文章br> 它说:

今天,MVC和类似的模型视图总计(MVP)是关注设计模式的分离,这些模式仅适用于较大系统的演示层.

无论如何...在谈论企业Web应用程序时,UI对业务逻辑层的调用应放置在(演示)控制器内.



这是因为控制器实际上将调用调用到特定资源,通过对业务逻辑进行调用并将数据(模型)链接到适当的视图来查询数据.

Mud告诉您,业务规则进入了模型.
也是如此,但他混合了(演示)模型(MVC中的" M")和基于层的应用程序设计的数据层模型.
因此,将与
数据库相关的业务放置在应用程序的模型(数据层)中是有效的.
但是,您不应将它们放在MVC结构的演示层的模型中,因为这仅适用于特定的UI.

该技术独立于您使用域驱动设计还是基于交易脚本的方法.

让我为您想象一下:


演示层:模型 - 查看 - 控制器


业务层:域逻辑 - 应用程序逻辑


数据层:数据存储库 - 数据访问层


您在上面看到的模型意味着您拥有使用MVC,DDD和数据库累人员数据层的应用程序.
这是设计较大企业Web应用程序的常见方法.

但是您还可以将其缩小以使用简单的非DDD业务层(无域逻辑的业务层)和直接写入特定数据库的简单数据层.
您甚至可以删除整个数据层并直接从业务层访问数据库,尽管我不建议它.

多数民众赞成在技巧...我希望这会有所帮助...

[注意:] 您还应该意识到,如今,应用程序中不仅有一个"模型". 通常,应用程序的每一层都有其自己的模型. 演示层的模型是特定的,但通常与使用的控件无关. 业务层也可以具有称为"域模型"的模型.通常,当您决定采用域驱动的方法时,通常是这种情况. 此"域模型"包含数据和业务逻辑(您程序的主要逻辑),通常与演示层无关. 演示层通常在某个"事件"(按钮等)上调用业务层以从或写入数据到数据层的数据. 数据层可能还具有自己的模型,该模型通常与数据库相关.它通常包含一组实体类以及数据访问 - 对象(DAOS).

问题是:这如何适合MVC概念?

答案 - >不是!
好吧 - 有点这样,但不是完全.
这是因为MVC是1970年代后期针对SmallTalk -80编程语言开发的一种方法.那时,Guis和个人计算机并不常见,甚至没有发明万维网! 当今的大多数编程语言和IDE是在1990年代开发的. 当时,计算机和用户界面与1970年代完全不同.
当您谈论MVC时,您应该记住这一点.
Martin Fowler写了一篇非常好的文章,以了解MVC,MVP和今天的GUIS.

其他推荐答案

我认为,业务逻辑一词不是一个精确的定义.埃文斯(Evans)在他的书《域驱动的设计》中谈论了两种类型的商业逻辑:

  • 域逻辑.
  • 应用程序逻辑.

我认为这种分离要清楚得多.随着意识到有不同类型的业务规则的意识也意识到它们并不一定都去同一地点.

域逻辑是对应于实际域的逻辑.因此,如果您要创建会计应用程序,那么域规则将是有关帐户,发布,税收等的规则.等.

对于这两种类型的应用程序,CSV导入/导出可能是相关的,但是CSV导入/导出的规则与实际域无关.这种逻辑是应用程序逻辑.

域逻辑肯定会进入模型层.该模型还将对应于DDD中的域层.

应用程序逻辑不一定必须放置在模型层中.可以直接放置在控制器中,也可以创建一个单独的应用程序层托管这些规则.在这种情况下,最合乎逻辑的是什么取决于实际应用.

本文地址:https://www.itbaoku.cn/post/627554.html

问题描述

I have 2 questions:

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

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.

推荐答案

Business rules go in the model.

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.

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.

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

其他推荐答案

Fist of all:
I believe that you are mixing up the MVC pattern and n-tier-based design principles.

Using an MVC approach does not mean that you shouldn't layer your application.
It might help if you see MVC more like an extension of the presentation layer.

If you put non-presentation code inside the MVC pattern you might very soon end up in a complicated design.
Therefore I would suggest that you put your business logic into a separate business layer.

Just have a look at this: Wikipedia article about multitier architecture

It says:

Today, MVC and similar model-view-presenter (MVP) are Separation of Concerns design patterns that apply exclusively to the presentation layer of a larger system.

Anyway ... when talking about an enterprise web application the calls from the UI to the business logic layer should be placed inside the (presentation) controller.

That is because the controller actually handles the calls to a specific resource, queries the data by making calls to the business logic and links the data (model) to the appropriate view.

Mud told you that the business rules go into the model.
That is also true, but he mixed up the (presentation) model (the 'M' in MVC) and the data layer model of a tier-based application design.
So it is valid to place your database related business rules in the model (data layer) of your application.
But you should not place them in the model of your MVC-structured presentation layer as this only applies to a specific UI.

This technique is independent of whether you use a domain driven design or a transaction script based approach.

Let me visualize that for you:


Presentation layer: Model - View - Controller


Business layer: Domain logic - Application logic


Data layer: Data repositories - Data access layer


The model that you see above means that you have an application that uses MVC, DDD and a database-independed data layer.
This is a common approach to design a larger enterprise web application.

But you can also shrink it down to use a simple non-DDD business layer (a business layer without domain logic) and a simple data layer that writes directly to a specific database.
You could even drop the whole data-layer and access the database directly from the business layer, though I do not recommend it.

Thats' the trick...I hope this helps...

[Note:] You should also be aware of the fact that nowadays there is more than just one "model" in an application. Commonly, each layer of an application has it's own model. The model of the presentation layer is view specific but often independent of the used controls. The business layer can also have a model, called the "domain-model". This is typically the case when you decide to take a domain-driven approach. This "domain-model" contains of data as well as business logic (the main logic of your program) and is usually independent of the presentation layer. The presentation layer usually calls the business layer on a certain "event" (button pressed etc.) to read data from or write data to the data layer. The data layer might also have it's own model, which is typically database related. It often contains a set of entity classes as well as data-access-objects (DAOs).

The question is: how does this fit into the MVC concept?

Answer -> It doesn't!
Well - it kinda does, but not completely.
This is because MVC is an approach that was developed in the late 1970's for the Smalltalk-80 programming language. At that time GUIs and personal computers were quite uncommon and the world wide web was not even invented! Most of today's programming languages and IDEs were developed in the 1990s. At that time computers and user interfaces were completely different from those in the 1970s.
You should keep that in mind when you talk about MVC.
Martin Fowler has written a very good article about MVC, MVP and today's GUIs.

其他推荐答案

The term business logic is in my opinion not a precise definition. Evans talks in his book, Domain Driven Design, about two types of business logic:

  • Domain logic.
  • Application logic.

This separation is in my opinion a lot clearer. And with the realization that there are different types of business rules also comes the realization that they don't all necessarily go the same place.

Domain logic is logic that corresponds to the actual domain. So if you are creating an accounting application, then domain rules would be rules regarding accounts, postings, taxation, etc. In an agile software planning tool, the rules would be stuff like calculating release dates based on velocity and story points in the backlog, etc.

For both these types of application, CSV import/export could be relevant, but the rules of CSV import/export has nothing to do with the actual domain. This kind of logic is application logic.

Domain logic most certainly goes into the model layer. The model would also correspond to the domain layer in DDD.

Application logic however does not necessarily have to be placed in the model layer. That could be placed in the controllers directly, or you could create a separate application layer hosting those rules. What is most logical in this case would depend on the actual application.