简体   繁体   English

ASP.NET MVC中的ViewModel和域模型

[英]ViewModels and domain models in ASP.NET MVC

I'm developing an ASP.NET MVC application and I came to a pretty awkward situation. 我正在开发ASP.NET MVC应用程序,但遇到了一个非常尴尬的情况。

I have a page where a user can search some items (say, students) by some criteria. 我有一个页面,用户可以在其中根据某些条件搜索某些项目(例如,学生)。 I used to pass a collection of students to my view, but then I've added some settings to search so I've decided to create the ViewModel as follows 我曾经将一组学生传递给我的视图,但是后来我添加了一些要搜索的设置,因此决定按照以下方式创建ViewModel

public class SearchViewModel
{
    public string SearchString { get; set; }
    public bool IsCaseSensitive { get; set; }
    ...
    //other parameters
    ...
    public IEnumerable<Student> Students { get; set; }
}

Then I've thought of the situation when I have to add some additional information to each student, which isn't stored in the database and is generated on controller. 然后我想到了一种情况,当我必须向每个学生添加一些其他信息时,这些信息没有存储在数据库中,而是在控制器上生成的。 My first thought was quite an idiotic - I've added an additional array to save it in the ViewModel like this: 我的第一个想法是一个白痴-我添加了一个额外的数组将其保存在ViewModel中,如下所示:

public class SearchViewModel
{
    public string SearchString { get; set; }
    public bool IsCaseSensitive { get; set; }
    ...
    //other parameters
    ...
    public IEnumerable<Student> Students { get; set; }
    public IEnumerable<int> someData { get; set; }
}

So that to get the data, the client code would have to get the students position in the array and then go to the corresponding position in the someData array. 为了获取数据,客户端代码将必须获取学生在数组中的位置,然后转到someData数组中的相应位置。

I didn't like that idea very much so I've changed my model to consist of search parameters + an additional model to hold both student object and their data. 我不太喜欢这个主意,因此我已经更改了模型,使其包含搜索参数以及一个用于容纳学生对象及其数据的附加模型。

public class SearchViewModel
{
    public string SearchString { get; set; }
    public bool IsCaseSensitive { get; set; }
    ...
    //other parameters
    ...
    public IEnumerable<StudentViewModel> StudentModels { get; set; }
}

public class StudentViewModel
{
    public Student Student { get; set; }
    public int someData { get; set; }
}
  1. Is it a fine idea to create such 'helper' models as StudentViewModel? 创建诸如StudentViewModel之类的“帮助器”模型是一个好主意吗? I would probably not use StudentViewModel anywhere but inside the SearchViewModel. 我可能不会在SearchViewModel内的任何地方使用StudentViewModel。 Given the SearchViewModel is a kind of 'help' model on itself, creating another model to use only inside the SearchViewModel seems a bit strange. 鉴于SearchViewModel本身就是一种“帮助”模型,因此创建另一个仅在SearchViewModel内部使用的模型似乎有些奇怪。 Is it so? 是这样吗?

  2. As far as I've found out, a ViewModel should never contain a domain model. 据我了解,ViewModel绝不应该包含域模型。 Should I divide Student property into smaller properties? 我应该将学生财产分为较小的财产吗? For instance, like this: 例如,像这样:

     public class StudentViewModel { public string Name { get; set; } public int GroupId { get; set; } public int someData { get; set; } } 
  3. In general, as I've understood, a ViewModel should consist only of primitives and their collections (and, probably, other ViewModels as in the first question). 一般而言,据我所知,ViewModel应该由基元及其集合(以及第一个问题中的其他ViewModel)组成。 Is it correct? 这是正确的吗?

I do not pretend that this inviolable truth. 我不假装这个不可侵犯的事实。 But in my opinion: 但我认为:

  1. Very often view model is created only for one view. 通常,视图模型仅针对一个视图创建。
  2. Using domain/data models in views it is normally (if it makes the development process easier/faster/more clear). 通常在视图中使用域/数据模型(如果它使开发过程更容易/更快/更清晰)。 In your case I would not be used Student class in ViewModel. 在您的情况下,将不会在ViewModel中使用Student类。
  3. ViewModel should contains all data that you need for building view (primitives and not primitives). ViewModel应该包含构建视图所需的所有数据(原始而非原始)。 But if you can make the view model easier and more clear by doing some work in the controller then it should be done. 但是,如果您可以通过在控制器中进行一些工作来使视图模型更轻松,更清晰,那么应该做到这一点。
  1. Is it a fine idea to create such 'helper' models as StudentViewModel? 创建诸如StudentViewModel之类的“帮助器”模型是一个好主意吗?

I believe it is. 我相信是。 Your view 'consists' of a list of 'student views'. 您的视图“包含”“学生视图”列表。 Viewmodels exist only to serve your views with data that is structured in an optimal way so that the necessary logic in the view is as minimal as possible. 视图模型仅用于以最佳方式构造的数据为您的视图提供服务,以使视图中的必要逻辑尽可能少。

  1. As far as I've found out, a ViewModel should never contain a domain model. 据我了解,ViewModel绝不应该包含域模型。

I would advise against using your entities in views, especially when dealing with forms or other ways of data entry. 我建议不要在视图中使用您的实体,尤其是在处理表单或其他数据输入方式时。 In this case, it's often interesting to create a special "Form model" that contains only the properties you want your user to submit. 在这种情况下,创建一个仅包含您希望用户提交的属性的特殊“表单模型”通常很有趣。 This also allows you to write validation logic for this specific form. 这也使您可以为此特定形式编写验证逻辑。 Think of it as a model that serves your form, similarly to how a view model serves your view. 可以将其视为为您的表单提供服务的模型,类似于视图模型为您的视图提供服务的方式。 Use AutoMapper to fill up these form models to make your life easier. 使用AutoMapper填写这些表单模型可以使您的生活更轻松。

However, in some scenarios I find it acceptable to use entities in your views: when you use EF and lazy loading, you can use entities when displaying data to profit from the lazy loading mechanism. 但是,在某些情况下,我认为在视图中使用实体是可以接受的:使用EF和延迟加载时,可以在显示数据时使用实体以从延迟加载机制中获利。 Only the data you display will get loaded from the database. 仅显示的数据将从数据库中加载。 However, since it is almost always known beforehand which data you will need, there are usually better data loading strategies. 但是,由于几乎总是事先知道您需要哪些数据,因此通常会有更好的数据加载策略。 Lazy loading is handy during development when lots of changes are happening (loading & showing additional data is fast & easy to implement without having to change much code), but is usually the first thing to tackle during performance reviews. 在开发中发生大量更改时(在无需更改大量代码的情况下快速且易于实现的加载和显示其他数据)在开发过程中,惰性加载非常方便,但这通常是性能检查期间要解决的第一件事。

  1. Should I divide Student property into smaller properties? 我应该将学生财产分为较小的财产吗? In general, as I've understood, a ViewModel should consist only of primitives and their collections (and, probably, other ViewModels as in the first question). 一般而言,据我所知,ViewModel应该仅由基元及其集合(以及第一个问题中的其他ViewModel)组成。 Is it correct? 这是正确的吗?

I disagree. 我不同意。 You designed your domain to represent your business objects, why would you throw that away again in your viewmodels? 您设计了域来表示您的业务对象,为什么还要在视图模型中再次丢弃它呢? Always consider the future possibility that your domain models will need to be extended with extra properties and what you can do today to reduce the cost of such changes. 始终考虑将来可能需要使用其他属性扩展域模型的可能性,以及您今天可以采取的措施以减少此类更改的成本。 If you want to separate your domain layer from your presentation layer, introduce a DTO layer that contains the same objects. 如果要将域层与表示层分开,请引入一个包含相同对象的DTO层。 On these objects, you can apply data annotations and other stuff that is only applicable in the presentation layer. 在这些对象上,您可以应用数据注释和仅适用于表示层的其他内容。 Filling the DTO's from the domain objects can be done swiftly with AutoMapper. 可以使用AutoMapper快速完成从域对象填充DTO。 However, be careful not to write your business logic on/with these DTO's. 但是,请注意不要在这些DTO上/与这些DTO一起编写业务逻辑。

This is my 2 cents. 这是我的2美分。

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

相关问题 在Asp.net MVC中,视图模型是否可以从域模型派生? - In Asp.net MVC, is it advisable for viewmodels to derive from domain models? ASP.NET MVC中的模型与ViewModel之间的交互 - Interaction between Models and ViewModels in ASP.NET MVC View asp.net MVC中的多个ViewModel - Multiple ViewModels in View asp.net MVC 如何在ASP.NET MVC中使用ViewModels? - How to use ViewModels in ASP.NET MVC? 使用多个模型比使用ASP.NET MVC中的ViewModel有更好的方法吗? - Is there a better way to consume multiple Models than through ViewModels in ASP.NET MVC? ASP.NET MVC - 我是否应该使用存储库模式将ViewModel写入数据库或首先将它们转换为模型? - ASP.NET MVC - Should I use the Repository Pattern to write ViewModels to the database or convert them to Models first? MVC将视图模型映射到域模型 - MVC map Viewmodels to domain models 如何在ASP.NET MVC中将Controller对象列表转换为Controller上的viewmodel - How can I convert a list of domain objects to viewmodels on the Controller in ASP.NET MVC 为什么 Display(Name = &quot;&quot;) 属性在我的一个 ViewModel 中被忽略,而它似乎在我的 ASP.NET MVC5 中的其他模型中工作? - Why is the Display(Name = "") attribute ignored in one of my ViewModels while it seems to work in my other models in ASP.NET MVC5? Asp.net 核心 MVC 中 ViewModel 的适配器模式 - Adapter Pattern for ViewModels in Asp.net core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM