简体   繁体   English

最佳实践实体框架模型与ViewModels

[英]Best Practice Entity Framework Models vs ViewModels

From what I read, I understand that we should always pass a viewmodel to the view. 根据我的阅读,我理解我们应该始终将视图模型传递给视图。 However, sometimes this viewmodel is exactly or almost the same as the EF-model. 但是,有时这个视图模型与EF模型完全相同或几乎相同。 Is it acceptable or is there any workaround to this problem (repetitive code) ? 是否可接受或是否有解决此问题的方法(重复代码)?

For example, if I have this EF-Model : 例如,如果我有这个EF模型:

class UserModel
{
    string id {get;set}
    string name {get;set}
    string address {get;set}
    string phone {get;set}
    string website {get;set}
}

How should the viewmodel be ... like this : viewmodel应该如何...像这样:

class UserViewModel
{
    string name {get;set}
    string address {get;set}
    string otherobject {get;set}
}

Or Like this : 或者像这样:

class UserViewModel
{
    UserModel user;
    string otherobject {get;set}
}

With option #1, properties are repeated... and in another viewmodel they will be repeated again. 使用选项#1,属性会重复...而在另一个视图模型中,它们将再次重复。 And I will need to repeat all data annotations on each viewmodels. 我需要在每个视图模型上重复所有数据注释。 However, I send only the properties that I need. 但是,我只发送了我需要的属性。

With option #2, nothing is repeated, but I pass a lot of properties that I don't need. 使用选项#2,没有重复,但我传递了许多我不需要的属性。

The last option would be to mix option #1 and option #2 according to the needs... but I don't like this option because of the lack of a common standard. 最后一个选项是根据需要混合选项#1和选项#2 ......但由于缺乏通用标准,我不喜欢这个选项。 Sometimes the properties will be defined and datannoted in the viewmodels and sometimes in the EF-model. 有时,属性将在视图模型中定义和数据注释,有时在EF模型中定义。

I wish there is an option #4 that I don't see...? 我希望有一个选项#4我看不到......?

Thank you. 谢谢。

The difference of those classes is how your application interacts with. 这些类的不同之处在于应用程序与之交互的方式。 Model and ViewModel has different audience. ModelViewModel有不同的受众。

Your Models should be interacting with your application, and sometimes many people prefer to use those models as entities in EF Code First. 您的模型应该与您的应用程序进行交互,有时很多人更喜欢将这些模型用作EF Code First中的实体 They are what we call Domain Objects. 它们就是我们所说的Domain Objects。

On the other hand, ViewModels should be interacting with your Views. 另一方面, ViewModels应该与您的视图进行交互。 In your service layer, you populate your ViewModel with some data, and you can access them from your controllers. 在服务层中,使用某些数据填充ViewModel ,然后可以从控制器访问它们。

However, sometimes this viewmodel is exactly or almost the same as the EF-model. 但是,有时这个视图模型与EF模型完全相同或几乎相同。

The keyword here is "sometimes". 这里的关键字是“有时”。 You are right, for a very simple application, you don't even need to think about ViewModels , where your models can simply be used in most cases. 你是对的,对于一个非常简单的应用程序,你甚至不需要考虑ViewModels ,在大多数情况下你的模型可以简单地使用。 However, think about some cases for example where you want to display list of latest posts , latest comments , and let's say some related posts on a single view. 但是,请考虑一些情况,例如您要显示最新帖子列表, 最新评论 ,以及在单个视图中说一些相关帖子 What you are going to do? 你要做什么? This is where the ViewModels come. 这就是ViewModels的用武之地 You pass a specific ViewModel to your view, that contains all the necessary data, posts, comments, and related posts. 您将特定的ViewModel传递给您的视图,其中包含所有必要的数据,帖子,评论和相关帖子。

In most cases, you ViewModel should be build up from multiple Models , and sometimes, the porperties of a ViewModel are type of Models 在大多数情况下,你的ViewModel应该从多个模型建立起来,有时,在porperties一个视图模型模型的类型

I know it has been quite sometime since this question was asked. 我知道自问这个问题以来已经有一段时间了。 But, this may help someone who is looking for the answer. 但是,这可能有助于寻找答案的人。

Eventhough you have ViewModels which are almost same as models, it is recommended to create ViewModels with same code. 尽管ViewModel与模型几乎相同,但建议使用相同的代码创建ViewModels。

Possible reasons are 可能的原因是

  1. You may want to add validation to properties using Data Annotations. 您可能希望使用数据注释向属性添加验证。 It is not advised to have validation which are more specific to screen in your models which are supposed to be reflections of your DB structure. 建议不要对模型中的屏幕进行更具体的验证,这些验证应该是数据库结构的反映。

  2. Your ViewModels may change in future. 您的ViewModel将来可能会发生变化。 It may not make sense now, but there is always a possibility. 现在可能没有意义,但总有可能。

If you are worried about mapping code which looks obvious in most of the cases, you can make use of Automapper. 如果您担心映射代码在大多数情况下看起来很明显,您可以使用Automapper。

Cheers! 干杯!

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

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