ASP.NET MVC - 我应该使用Repository模式来写的ViewModels到数据库或将其先转换为模型? [英] ASP.NET MVC - Should I use the Repository Pattern to write ViewModels to the database or convert them to Models first?

查看:159
本文介绍了ASP.NET MVC - 我应该使用Repository模式来写的ViewModels到数据库或将其先转换为模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC应用程序,我有一个结合了一些模型到一个视图中一个相当复杂的编辑页面。

In my ASP.NET MVC app, I have a fairly complex edit page which combines a number of models into one view.

我使用的视图模型模式所有这些信息和present的合并一个凝聚力的对象查看。

I'm using the ViewModel pattern to combine all of this information and present one cohesive object to the View.

作为一个例子,我的ViewModel结构是这样的:

As an example, my ViewModel structure is something like this:

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

Employee对象有若干个基本属性,和preferred联系方式。

The Employee object has a number of basic properties, and a preferred contact method.

在编辑页面,用户得到的所有公司的员工,他们有添加和删除(使用JavaScript),以及编辑员工详细信息的能力。该ContactMethods列表用于填充每个员工的下拉。

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. The ContactMethods list is used to populate the dropdown for each employee.

我已经成功地翻译我的模型(从数据库读取),这个视图模型,然后再返回,所以编辑后,我留下了一个ViewModel重新presenting该公司的员工的当前状态。

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.

我使用的是库模式与数据库进行沟通,所以我的问题是,我应该直接打电话到CompanyRepository,路过视图模型,或者我应该转换视图模型回模型对象首先使用前存储库,并写入到数据库?

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?

在短,应存储库知道我的ViewModel对象?

推荐答案

我的视图模型转换回模型第一对象。我喜欢让我的网络层和存储库层之间的依赖关系松散越好。

I would convert the ViewModel back into Model objects first. I like keeping the dependency between my Web layer and Repository layers as loose as possible.

我不认为你的版本库应该知道你的视图模型,因为这是一个网络级的概念。

I don't think your Repository should know about your ViewModel, since that's a web level concept.

这篇关于ASP.NET MVC - 我应该使用Repository模式来写的ViewModels到数据库或将其先转换为模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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