在哪里将业务模型转换为视图模型? [英] Where to convert business model to view model?

查看:97
本文介绍了在哪里将业务模型转换为视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC应用程序中,我正在使用工作单元和存储库模式进行数据访问.

In my ASP.NET MVC application, I am using unit of work and repository patterns for data access.

使用工作单元类和在其中定义的存储库,我正在控制器中获取相关的实体集.以我的初学者知识,我可以想到两种获取业务模型并将其转换为视图模型的方法.

Using the unit of work class and the repository defined inside it I am fetching the related set of entities in my controller. With my beginner knowledge, I can think of two ways to fetch the business model and convert it to view model.

  • 存储库将业务模型返回给控制器,该模型比映射到视图模型还多,或者
  • 存储库本身将业务模型转换为视图模型,然后将其返回给控制器.

当前我正在使用第一种方法,但是对于具有很多属性的视图模型,我的控制器代码开始显得难看且冗长.

Currently I am using first approach, but my controller code started to look ugly and long for view models with lots of properties.

另一方面,我在考虑,因为我的存储库被称为UserRepository(例如),它应该直接返回业务模型,而不是仅对单个视图有用的某些模型.

On the other hand, I am thinking, since my repository is called UserRepository (for example), it should be returning the business model directly, instead of some model that is useful only for single view.

您认为其中哪一项是大型项目的最佳实践?有替代的方法吗?

Which one of these do you think is better practice for large projects ? Is there an alternative way ?

推荐答案

存储库应返回域模型,而不是视图模型.至于模型和视图模型之间的映射,我个人使用 AutoMapper ,所以我有一个单独的映射层但是该层是从控制器调用的.

Repositories should return domain models, not view models. As far as the mapping between the models and the view models is concerned, personally I use AutoMapper so I have a separate mapping layer but this layer is called from the controller.

这是典型的GET控制器动作的样子:

Here's how a typical GET controller action might look like:

public ActionResult Foo(int id)
{
    // the controller queries the repository to retrieve a domain model
    Bar domainModel = Repository.Get(id);

    // The controller converts the domain model to a view model
    // In this example I use AutoMapper, so the controller actually delegates
    // this mapping to AutoMapper but if you don't have a separate mapping layer
    // you could do the mapping here as well.
    BarViewModel viewModel = Mapper.Map<Bar, BarViewModel>(domainModel);

    // The controller passes a view model to the view
    return View(viewModel);
}

当然可以使用自定义操作过滤器将其缩短以避免重复的映射逻辑:

which of course could be shortened with a custom action filter to avoid the repetitive mapping logic:

[AutoMap(typeof(Bar), typeof(BarViewModel))]
public ActionResult Foo(int id)
{
    Bar domainModel = Repository.Get(id);
    return View(domainModel);
}

AutoMap自定义操作过滤器订阅OnActionExecuted事件,拦截传递给视图结果的模型,调用映射层(在我的情况下为AutoMapper)将其转换为视图模型,并将其替换为视图.当然,视图是根据视图模型强类型化的.

The AutoMap custom action filter subscribes to the OnActionExecuted event, intercepts the model passed to the view result, invokes the mapping layer (AutoMapper in my case) to convert it to a view model and substitutes it for the view. The view is of course strongly typed to the view model.

这篇关于在哪里将业务模型转换为视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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