用于在域模型之间映射数据的模式 [英] Patterns for mapping data between domain models

查看:119
本文介绍了用于在域模型之间映射数据的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我最近一直需要做的一件常见的事情,我正在寻找任何常见的模式,使其更容易一些。

This is a common thing that I have been needing to do recently and I was looking for any common patterns to make this a little easier.

这一切都是我有一些数据模型,它们被建模以满足ORM,并且纯粹对对象进行CRUD操作。这些模型目前通过存储库/工厂(取决于它的C或RUD)是否暴露。

The main gist of it all is that I have some data models, which are modelled to satisfy the ORM and purely do CRUD operations to objects. These models are currently exposed via repositories/factories (dependant upon if its C or RUD).

然后我有一个视图模型,这是一个更可读的,洒上UI的关注点,例如视图之间的验证和映射数据(这是一个ASP.MVC场景,但这种情况可以抽象到大多数情况)。

I then have a view model, which is a bit more readable, and is sprinkled with UI concerns, such as validation and mapping data between the view (this is an ASP.MVC scenario but this situation can be abstracted to most situations).

所以让说我去本地/用户/ 1,应该去,让我的用户在数据库中的ID 1,然后显示在用户界面。最终,这必须从数据域下拉数据,然后将其映射到ui模型以进行显示。

So lets say I go to localhost/user/1, that should go and get me the user with an Id 1 in the DB and then display it on the UI. Ultimately this has to pull data down from the data domain and then map it to a ui model for display purposes.

以下是一个示例场景:

public class OrmUser
{
    public int Id {get;set;}
    public string Name {get;set;}
    public IList<Permission> Permissions {get;set;}
}

public class UiUser
{
    [Required]
    public int Id {get;set;}
    [Required]
    public string Name {get;set;}
    public bool IsUserAdmin {get;set;}
}

public class UserMapper : IMapper<UiUser>
{
    public UiUser Get(int id)
    {
        var ormUser = UserRepository.Get(id);
        var uiUser = new UiUser
        {
            Id = ormUser.Id,
            Name = ormUser.Name,
            IsUserAdmin = IsUserAdmin(ormUser.Permissions)
        }
    }

    private bool IsUserAdmin(IList<Permission> permissions)
    {
        return permissions.SomeLinq(ToFindIfTheyAreAnAdmin);
    }
}

这是一个简单的例子,但显示了一个数据模型,包含了很多相同的信息,但在这个观点,你不关心所有的信息只是一个子集。这样一来,你就可以得到一个映射器,不仅可以对映射进行抽象,而且要与数据域进行通信,但是您需要为每个类型写一个映射器类,而上述假定它是一种单向映射,而不是2路映射将需要一些更多的代码。

This is a simple example but shows how a data model, contains a lot of the same sort of information, but at this view in question you do not care about all the information just a subset of it. This way having a mapper you get to abstract not only the mapping but the communication with the data domain, however you need to write a mapper class for each type, and the above assumes it is a one way mapping, not a 2 way mapping which would require some more code.

那么你们如何进行这个映射呢?正如目前我刚刚写过的抽象映射器,它基本上允许UI层运行一个查询,并返回一个视图模型,将存储库抽象出来,并将数据从一个模型复制到另一个模型,而且只是觉得应该有一个更好的

So how do you all go about carrying out this mapping? As currently I have just been writing abstraction mappers which basically allow the UI layer to run a query, and get back a view model, abstracting the repositories and the copying data from one model to another, and it just feels like there should be a better way to do this.

推荐答案

在.net中,你应该看看Automapper

In .net you should probably take a look at Automapper

https://github.com/AutoMapper/AutoMapper

它本质上允许您执行以下操作:

It essentially allows you to do:

CreateMap<Domain.Customer, ViewModel.Customer>()

然后,您可以通过说:

var vmCustomer = Mapper.Map<Domain.Customer, ViewModel.Customer>(domainCustomer);

可能会为您节省一吨的锅炉板码。

It will likely save you a ton of boiler plate code.

这篇关于用于在域模型之间映射数据的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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