使用AutoMapper展开DTO [英] Using AutoMapper to unflatten a DTO

查看:106
本文介绍了使用AutoMapper展开DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用AutoMapper来节省从DTO到域对象的时间,但是我在配置地图以使其起作用时遇到了麻烦,并且我开始怀疑AutoMapper是否可能是错误的工作工具.

考虑以下域对象示例(一个实体和一个值):

public class Person
{
    public string Name { get; set; }
    public StreetAddress Address { get; set; }
}

public class StreetAddress
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

我的DTO(来自Linq-to-SQL对象)看起来像这样:

public class PersonDTO
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

我希望能够在我的存储库中执行此操作:

return Mapper.Map<PersonDTO, Person>(result);

我已经尝试过尽一切可能来配置AutoMapper,但是我一直收到通用的缺少类型映射配置或不受支持的映射错误,没有任何详细信息告诉我失败的地方. /p>

我尝试了许多不同的配置,但其中一些:

Mapper.CreateMap<PersonDTO, Person>()
    .ForMember(dest => dest.Address, opt => opt.MapFrom(Mapper.Map<Person, Domain.StreetAddress>));

Mapper.CreateMap<Person, Domain.Person>()
    .ForMember(dest => dest.Address.Address1, opt => opt.MapFrom(src => src.Address))
    .ForMember(dest => dest.Address.City, opt => opt.MapFrom(src => src.City))
    .ForMember(dest => dest.Address.State, opt => opt.MapFrom(src => src.State));

我已经读过,使用AutoMapper进行拉平对象很容易,但是不拉平对象并不容易...甚至可能.谁能告诉我我是否正在尝试做不可能的事情,否则我做错了吗?

请注意,我的实际对象要复杂一些,因此有可能我遗漏了导致错误的关键信息...如果我的操作看起来正确,我可以提供更多信息或开始简化我的操作测试对象.<​​/p>

解决方案

使用 https://github.com/omuleanu/ValueInjecter ,它可以进行拉平不拉平等操作,您还需要一个 asp.net mvc示例应用程序下载 ,其中演示了所有功能(也包括单元测试)

I've been trying to use AutoMapper to save some time going from my DTOs to my domain objects, but I'm having trouble configuring the map so that it works, and I'm beginning to wonder if AutoMapper might be the wrong tool for the job.

Consider this example of domain objects (one entity and one value):

public class Person
{
    public string Name { get; set; }
    public StreetAddress Address { get; set; }
}

public class StreetAddress
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

My DTO (from a Linq-to-SQL object) is coming out looking roughly like this:

public class PersonDTO
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

I'd like to be able to do this in my repository:

return Mapper.Map<PersonDTO, Person>(result);

I've tried configuring AutoMapper every way I can figure, but I keep getting the generic Missing type map configuration or unsupported mapping error, with no details to tell me where I'm failing.

I've tried a number of different configurations, but here are a few:

Mapper.CreateMap<PersonDTO, Person>()
    .ForMember(dest => dest.Address, opt => opt.MapFrom(Mapper.Map<Person, Domain.StreetAddress>));

and

Mapper.CreateMap<Person, Domain.Person>()
    .ForMember(dest => dest.Address.Address1, opt => opt.MapFrom(src => src.Address))
    .ForMember(dest => dest.Address.City, opt => opt.MapFrom(src => src.City))
    .ForMember(dest => dest.Address.State, opt => opt.MapFrom(src => src.State));

I've read that flattening objects with AutoMapper is easy, but unflattening them isn't easy...or even possible. Can anyone tell me whether I'm trying to do the impossible, and if not what I'm doing wrong?

Note that my actual objects are a little more complicated, so it's possible I'm leaving out info that is the key to the error...if what I'm doing looks right I can provide more info or start simplifying my objects for testing.

解决方案

use https://github.com/omuleanu/ValueInjecter, it does flattening and unflattening, and anything else you need, there is an asp.net mvc sample application in the download where all the features are demonstrated (also unit tests)

这篇关于使用AutoMapper展开DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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