对MVC和实体模型的困惑 [英] Confusion over MVC and entity model

查看:111
本文介绍了对MVC和实体模型的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑的原因是我在构建mvc应用程序时使用了两个不同的演练,分别是:史蒂文·桑德森的pro asp.net mvc和在线mvc音乐商店。前者创建一个域模型,将实体模型与存储库一起放置在其中,而音乐商店演示将实体模型放置在mvc模型文件夹中。哪种方法是最好的方法。实体模型和相关的存储库应该存在于单独的域层中还是在MVCs模型文件夹中。

My confusion stems from the fact I am using 2 different walkthroughs on building mvc applications, namely: Steven Sanderson's pro asp.net mvc and the online mvc music store. The former creates a domain model, placing the entity model in there along with repositories, while the music store demo places the entity model in the mvc model folder. Which of these is the best approach. Should the entity model and associated repositories exist in a separate domain layer, or in the MVCs model folder.

推荐答案

关注点分离



Asp.net MVC项目模板中的Model文件夹确实非常令人困惑。大多数不了解MVC模式的开发人员都认为应用程序/域模型=数据模型。

Separation of concerns

Model folder in Asp.net MVC project template is indeed very confusing. Most developers not knowing enough about MVC pattern think that application/domain model = data model. Most of the time, that's not the case.

例如,一个用户实体可能采用几种不同的形式:

Take for instance a user entity that may be in several different forms:


  • NewUser 应用程序模型实体,具有用户的大多数属性,以及两个密码属性,可以以声明方式进行验证

  • User 数据模型实体具有所有通常的用户属性和一个密码属性

  • 用户 应用程序模型实体具有所有常规属性,并且输入密码

  • NewUser is an application model entity that has most properties of a user, plus two password properties that can be declaratively validated
  • User data model entity has all usual user properties and one password property
  • User application model entity has all the usual properties and none for password

因此您可以通过这个简单的示例看到存在多个彼此不同的模型。而且,当您有一个多组装的应用程序时,将应用程序模型放在单独的程序集中是非常明智的,因为所有程序集很可能仅使用这些对象进行通信。不应将任何数据模型实体转移到数据组件/层之外以使用SoC ...

So you can see by this simple example there are multiple models that differ between each other. And when you have a multi-assemblied application, putting application model in a separate assembly is very wise, since all assemblies will most probably communicate using these objects only. No data model entities should be transferred outside data assembly/tier to make use of SoC...

因此最后可以将数据模型放在Model文件夹中构建一个小型的简单应用程序,但是在所有其他情况下,最好使用在所有程序集之间共享的单独的应用程序模型程序集。并有一个仅用于数据层组装的单独数据模型。

So in the end it's ok to put data model in the Model folder when building a small scale simple application, but in all other cases it's probably better to use a separate application model assembly that's shared between all assemblies. And have a separate data model that's only used in data tier assembly.

阅读此答案可以帮助您更清楚地了解情况。

这个

Read this answer that may help you see things a bit clearer.
And this one as well.

我建议您不要使用Model文件夹,而应使用单独的程序集。您将获得更好的分离和更好的可伸缩性。

I would recommend against using the Model folder and use a separate assembly instead. You'll have better separation and improved scalability.

这篇关于对MVC和实体模型的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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