简体   繁体   English

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?

In my ASP.NET MVC app, I have a fairly complex edit page which combines a number of models into one view. 在我的ASP.NET MVC应用程序中,我有一个相当复杂的编辑页面,它将许多模型组合到一个视图中。

I'm using the ViewModel pattern to combine all of this information and present one cohesive object to the View. 我正在使用ViewModel模式来组合所有这些信息,并向View提供一个有凝聚力的对象。

As an example, my ViewModel structure is something like this: 举个例子,我的ViewModel结构是这样的:

CompanyId
CompanyName
List<Employee> Employees
List<ContactMethod> ContactMethods

The Employee object has a number of basic properties, and a preferred contact method. Employee对象具有许多基本属性和首选联系方法。

On the edit page, the user is given all of the employees of the company and they have the ability to add and remove (using javascript), as well as edit employee details. 在编辑页面上,用户将获得公司的所有员工,他们可以添加和删除(使用javascript),以及编辑员工详细信息。 The ContactMethods list is used to populate the dropdown for each employee. ContactMethods列表用于填充每个员工的下拉列表。

I've successfully translated my Models (read from the database) into this ViewModel and back again, so after an edit, I'm left with a ViewModel representing the current state of that company's employees. 我已成功将我的模型(从数据库中读取)转换为此ViewModel并再次返回,因此在编辑之后,我留下了一个ViewModel,表示该公司员工的当前状态。

I'm using a Repository pattern to communicate with the database, so my question is, should I call directly into the CompanyRepository, passing the ViewModel, or should I convert the ViewModel back into Model objects first before using the Repository to write them to the database? 我正在使用Repository模式与数据库进行通信,所以我的问题是, 我应该直接调用CompanyRepository,传递ViewModel,还是应该先将ViewModel转换回Model对象,然后再使用Repository将它们写入数据库?

In short, should the Repository know about my ViewModel objects? 简而言之, Repository应该知道我的ViewModel对象吗?

I would convert the ViewModel back into Model objects first. 我首先将ViewModel转换回Model对象。 I like keeping the dependency between my Web layer and Repository layers as loose as possible. 我喜欢尽可能地保持Web层和Repository层之间的依赖关系。

I don't think your Repository should know about your ViewModel, since that's a web level concept. 我不认为您的存储库应该知道您的ViewModel,因为这是一个Web级别的概念。

ViewModel is the model to the view (UI), so repository shouldn't know about the view model. ViewModel是视图(UI)的模型,因此存储库不应该知道视图模型。 Separating them will keep repository loosely coupled from the UI. 将它们分开将使存储库与UI松散耦合。

Use another layer like service layer, to encapsulate repository from the UI. 使用服务层之类的其他层来从UI封装存储库。 This layer also does the ViewModel - Model conversation and do respository call. 该层还执行ViewModel - Model对话并执行存储库调用。

public class ServiceLayer
{
   public void SaveModel(ViewModel viewmodel)
   {
      var model = viewModel.ToModel();
      repository.Save(model)
   }
}

I would agree with the previous answer of converting ViewModels back into "plain" Models, but would add that this task should probably be carried out by a separate service layer. 我同意先前将ViewModels转换回“普通”模型的答案,但会补充说,这项任务应该由一个单独的服务层执行。 This layer would be responsible for disassembling your ViewModels and acting appropriately. 该层将负责反汇编您的ViewModel并采取适当的行动。

This is essentially the definition of a service: something whose job is to carry out a logical unit of work that requires multiple models and/or complex logic. 这本质上是服务的定义:其工作是执行需要多个模型和/或复杂逻辑的逻辑工作单元。

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

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