公开实体或DTO以在mvc3中查看的最佳做法是什么? [英] which is the best practices for exposing entity or DTO to view in mvc3?

查看:99
本文介绍了公开实体或DTO以在mvc3中查看的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的定制体系结构,包括针对不同技术的n层.

I have created my own customized lots of architecture including n-tier layers for different different technology.

当前正在使用asp.net mvc框架在n层体系结构上工作.问题是我在数据访问层有实体框架.由于实体将拥有所有的关系元数据和导航属性,因此实体变得更重.我感觉直接在mvc视图上公开此实体是不明智的.

Currently working on n-tier architecture with asp.net mvc framework. The problem is I have entity framework at data access layer. As the entities will have all it's relational metadata and navigation properties, it becomes heavier one. I am feeling like it is not wise to expose this entities directly over mvc view.

我更倾向于通过mvc视图公开自己的实体定制模型.

I am more favor in exposing own customized model of entities over mvc view which one be lighter one.

但这也导致我将数据从原始实体转换为自定义模型的开销.

But this also leads me overhead of converting data from my original entities to customized model.

例如,我有从实体框架的edmx文件生成的Employee实体.它包含总共20个具有所有导航属性的字段.

For example I have Employee entity which is as generated from edmx file of entity framework. It contains total 20 fields with all navigation properties.

现在在mvc中,我只需要显示2个字段即可进行编辑.

Now over view in mvc I need to show only 2 fields for edit.

那么我们是否需要公开原始实体以进行查看还是需要创建这两个字段的DTO/定制模型,而不是公开该视图?

So do we need to expose original entity to view or need to create DTO/customized model of that two field and than expose that view?

推荐答案

我将使用视图模型.我学会了不要将域对象公开给视图,而是将域对象映射到视图模型,然后将此视图模型返回到视图.

I would use a view model. I have learnt not to expose my domain objects to the view, I rather map my domain object to the view model and return this view model to the view.

这里是局部视图模型,如果需要更多员工数据来创建/编辑或显示,则可能具有更多属性:

Here is a partial view model, you might have more properties if you need more employee data to create/edit or display:

public class EmployeeViewModel
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

在我的控制器的action方法中,它看起来像这样:

In my action method of my controller it would look something like this:

public ActionResult Edit(int id)
{
     Employee employee = employeeRepository.GetById(id);

     // Mapping can be done here by using something like Auto Mapper, but I am
     // manually mapping it here for display purposes

     EmployeeViewModel viewModel = new EmployeeViewModel();
     viewModel.FirstName = employee.FirstName;
     viewModel.LastName = employee.LastName;

     return View(viewModel);
}

然后您的视图可能看起来像这样:

And then your view might look something like this:

<td>First Name:</td>
<td>@Html.TextBoxFor(x => x.FirstName, new { maxlength = "15" })
    @Html.ValidationMessageFor(x => x.FirstName)
</td>

我更喜欢有一个视图模型,该模型仅包含视图上所需的employee值.假设您的员工有20个属性,而您只需要更新2个字段,然后为什么将全部20个都传递给视图? 仅使用所需的内容.

I prefer to have a view model that has only the values of employee that is needed on the view. Let say your employee has 20 properties, and you only need to update 2 fields, why then pass all 20 to the view? Use only what you need.

这篇关于公开实体或DTO以在mvc3中查看的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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