我可以使用automapper多个对象映射到目标对象 [英] Can i map multiple objects to a destination object using automapper

查看:1305
本文介绍了我可以使用automapper多个对象映射到目标对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<预类=郎-CS prettyprint-覆盖> UserAccount objUserAccount = NULL;
AutoMapper.Mapper.CreateMap< AccountBO,UserAccount>();
objUserAccount = AutoMapper.Mapper.Map&下; AccountBO,UserAccount>(lstAcc [0]);



到现在为止它映射 AccountBO 性能很好。



现在我必须映射对象 objAddressBO 属性的目的地包括上面映射值。为了这个,我已经写了代码,如下下面的代码行以上。



<预类=郎-CS prettyprint-覆盖> AutoMapper.Mapper。 CreateMap< AddressBO,UserAccount>();
objUserAccount = AutoMapper.Mapper.Map< AddressBO,UserAccount>(objAddressBO);



但它失去了第一次映射值和返回只有最后一次映射值。



请让我知道什么样的变化,我需要做的有两个在我的目标对象的值。


解决方案

您应该只设置一次的映射。要做到这一点,最好的方法是使用配置文件:





<预类=郎-CS prettyprint-覆盖> 公共类我的配置文件:配置文件
{
公众覆盖字符串配置文件名
{
得到
{
返回我的资料;
}
}

保护覆盖无效配置()
{
AutoMapper.Mapper.CreateMap< AccountBO,UserAccount>();
AutoMapper.Mapper.CreateMap< AddressBO,UserAccount>();
}
}

这应该然后在初始化方法初始化(如为 App_Start 对于Web项目)



您也应该创建单元测试来测试映射已正确配置



<预类=郎-CS prettyprint-覆盖> [的TestFixture]
公共类MappingTests
{
[测试]
公共无效AutoMapper_Configuration_IsValid()
{
Mapper.Initialize(M = GT; m.AddProfile<我的资料>());
Mapper.AssertConfigurationIsValid();
}
}

如果这一切工作正常,并假设我已经明白问题正确,要初始化 objUserAccount listAcc [0] ,然后在一些额外的参数填补 objAddressBO 。你可以这样做,如:



<预类=郎-CS prettyprint-覆盖> objUserAccount = Mapper.Map< AccountBO,UserAccount>(lstAcc [ 0]);
objUserAccount = Mapper.Map(objAddressBO,objUserAccount);



第一地图将创建该对象,而第二地图将更新提供目标对象



请注意,为了这个正常工作,你可能需要填写您的映射配置一点点地提供正确的行为。例如,如果你希望避免更新目标属性,你可以使用 UseDestinationValue 指令。如果你想要一个条件适用于更新您可以使用条件指令。如果你想完全忽略的财产,你可以使用忽略指令。



如果需要,更多的文档可以发现这里


UserAccount objUserAccount=null;
AutoMapper.Mapper.CreateMap<AccountBO, UserAccount>();
objUserAccount = AutoMapper.Mapper.Map<AccountBO, UserAccount>(lstAcc[0]);

Up to this point it is mapping AccountBO properties fine.

Now I have to map object objAddressBO properties to destination including above mapped values. for this I have written code as below following to above lines of code.

AutoMapper.Mapper.CreateMap<AddressBO,UserAccount>();
objUserAccount=AutoMapper.Mapper.Map<AddressBO,UserAccount>(objAddressBO);

But it's losing first time mapped values and returning only the last time mapped values.

Please let me know what changes I need to do to have both the values in my destination object.

解决方案

You should only configure the mapping once. The best way to do this is by using profiles:

public class MyProfile : Profile
{
    public override string ProfileName
    {
        get
        {
            return "MyProfile";
        }
    }

    protected override void Configure()
    {
        AutoMapper.Mapper.CreateMap<AccountBO, UserAccount>();
        AutoMapper.Mapper.CreateMap<AddressBO,UserAccount>();
    }
}

This should then be initialised in an initialisation method (such as App_Start for web projects)

You should also create a unit test to test the mapping has been configured correctly

[TestFixture]
public class MappingTests
{
    [Test]
    public void AutoMapper_Configuration_IsValid()
    {
        Mapper.Initialize(m => m.AddProfile<MyProfile>());
        Mapper.AssertConfigurationIsValid();
    }
}

If that all works fine, and assuming I have understood the question correctly, you want to initialise objUserAccount from listAcc[0], then fill in some additional parameters from objAddressBO. You can do this like:

objUserAccount = Mapper.Map<AccountBO, UserAccount>(lstAcc[0]);
objUserAccount= Mapper.Map(objAddressBO, objUserAccount);

The first map will create the object, and the second map will update the provided destination object.

Note that for this to work correctly you may need to fill out your mapping configuration a little to provide the correct behaviour. For example, if you wish to avoid updating a destination property you can use the UseDestinationValue directive. If you want to apply a condition to the update you can use the Condition directive. If you wish to ignore the property completely, you can use the Ignore directive.

If required, more documentation can be found here.

这篇关于我可以使用automapper多个对象映射到目标对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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