简体   繁体   English

大型ASP.NET MVC项目中的模型怎么样

[英]What about the Model in Large ASP.NET MVC Project

I've been doing some research on using ASP.NET MVC and Entity Framework together for a fairly large project. 我一直在做一些有关将ASP.NET MVC和Entity Framework一起用于一个相当大的项目的研究。 Most examples separate the .edmx file from the MVC project by moving it to a new DAL project. 大多数示例将.edmx文件从MVC项目中分离出来,方法是将其移到新的DAL项目中。 In this project you would also find repositories and interfaces. 在该项目中,您还将找到存储库和接口。

While this approach makes perfect sense to me, there's one thing I can't seem to figure out: what about the Models in MVC? 尽管这种方法对我来说非常有意义,但是我似乎无法弄清一件事:MVC中的模型呢? In most examples the Controllers address the repository interfaces from the DAL project directly, so the MVC Models are no longer used? 在大多数示例中,控制器直接从DAL项目寻址存储库接口,因此不再使用MVC模型? Or is it a good idea to keep using them, but map them in the Controller? 还是继续使用它们,而是在Controller中映射它们是一个好主意吗?

There are 2 types of models: 有两种类型的模型:

  • domain models 领域模型
  • view models 查看模型

The domain models represent your domain entities. 域模型代表您的域实体。 They could be the autogenerated EF classes from your database or coming from somewhere else such as proxies generated from a WCF service that you are consuming. 它们可能是从数据库自动生成的EF类,也可能是来自其他位置(例如,您使用的WCF服务生成的代理)的EF类。 Those should live in your domain layer. 这些应该存在于您的域层中。

View models on the other hand go in the Models folder in the ASP.NET MVC project. 另一方面,视图模型位于ASP.NET MVC项目的“ Models文件夹中。 Those are specific classes that you define for each view. 这些是您为每个视图定义的特定类。 The controller actions will query your DAL layer to fetch one or more domain models and instantiate a view model which you have specifically defined for the given view that you want to render from this controller. 控制器操作将查询您的DAL层,以获取一个或多个域模型,并实例化一个您为要从此控制器呈现的给定视图专门定义的视图模型。 So a view model could hold information from multiple domain models (just because in the given view you need all this information). 因此,视图模型可以保存来自多个域模型的信息(仅因为在给定视图中您需要所有这些信息)。 Then the controller passes the view model to the view for displaying. 然后,控制器将视图模型传递给视图以进行显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM