MVC 范式中的模型、域、存储库和服务之间有什么区别?

发布于 2024-09-26 11:03:42 字数 25 浏览 2 评论 0原文

模型、域和存储库本质上是同一件事吗?

Are Models and Domains and Repositories essentially the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2024-10-03 11:03:42

不。

将领域模型与其包含的存储库区分开来的第一种方法是考虑存储库的作用。

本质上,存储库只是封装了一大块数据访问代码,以公开给定接口的方式,以便程序员可以轻松地互换不同的存储方法,例如,独立于数据库问题来测试其他代码。

相比之下,域模型保存给定应用程序所需的所有存储库。因此,很明显它是一种非常不同的野兽。

一只更大的野兽。

域模型还将包括其他代码和类,例如通过控制器传递到视图的模型(在 MVC 范例中)。

一个学派坚持认为领域模型应该位于它自己的类库中。这种思想流派反对在默认的 MVC 项目中使用 Model 文件夹。我同意这一点并且通常会删除它。

领域模型对领域进行建模。它与传递给每个视图的模型不同,模型实际上只表示视图所需的数据,而不是对给定的业务问题进行建模,包含存储库和服务等。

我认为您的问题是一个很好的问题,因为它解决了如果你不知道它们背后的想法,这个问题很容易被混淆。

先有鸡还是先有蛋?答:都不是。首先是问题。

DDD:

这些概念是领域驱动设计的一部分,与其说是关于给定的技术实现,不如说是关于如何标准化需要转换为软件的复杂领域的理解和建模的一组指南。

MVC 的一大优点是它允许您使用领域驱动设计之类的东西。相比之下,在 ASP.NET Web 表单中,当您尝试应用良好的设计原则时,受到的限制要大得多。

即,MVC 允许您设计应用程序,以便将域的表示封装在 MVC 的 M 位中。

归根结底,M 位能走多远取决于您。它可以仅位于 Models 文件夹中,或者您可以将其重构到自己的类库中,并将其重构为您喜欢的内容、实现 DDD 最佳实践或任何您想要的其他内容。

No.

The first way to distinguish a Domain Model from the repositories it contains is to think what a respository does.

In essence, a repository just encapsulates a chunk of data access code, in such a way that it exposes a given interface so that the programmer can easily interchange different storage methods and, for example, test other code independently of database concerns.

The domain model, by contrast, holds all the required respositories for a given app. It is therefore immediately obvious that it is a very different beast.

A much larger beast.

The domain model will also include other code and classes, such as the Models that are passed to Views via the Controllers (in the MVC paradigm).

A school of thought insists that the domain model should be in its own class library. This school of thought is opposed to using the Model folder in the default MVC project. I agree with this and usually delete it.

A Domain Model models a domain. It is distinct from the Models that are passed to each view, that really just represent the data the view needs, as opposed to modelling a given business concern, containing repositories and services, etc.

I think your question is a great one because it addresses an issue that is very easy to get confused about if you don't know the thinking behind them.

What came first, the chicken or the egg? Answer: Neither. What comes first is the question.

DDD:

These concepts are part of Domain Driven Design, which is not so much about a given technological implementation as about a set of guidelines on how to standardise the understanding and modelling of complex domains that need translating into software.

One of the great things about MVC is that it ALLOWS you to use something like Domain Driven Design. In ASP.NET webforms, by contrast, you are much more limited when trying to apply good design principles.

Ie, MVC allows you to design your app so that the representation of the Domain is encapsulated in the M bit of MVC.

At the end of the day, it is up to you how far you go with the M bit. It could just be in the Models folder, or you could refactor it out into its own class library and refactor it to your heart's content, implement DDD best practices or whatever other you want.

小伙你站住 2024-10-03 11:03:42

他们不是。这些都是领域驱动设计方法的所有元素。首先介绍的是这本免费电子书

呵呵!
托马斯

They're not. These are all elements of the Domain Driven Design methodology. A first introduction would be this free e-book.

HTH!
Thomas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文