AutoMapper是不承认具体的配置文件的前缀 [英] AutoMapper isn't recognizing profile-specific prefixes

查看:285
本文介绍了AutoMapper是不承认具体的配置文件的前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用AutoMapper从该属性名称前有前缀的一类数据并将其映射到没有这些前缀一,二类。但是,我并不希望它的总是的去掉前缀:我只是希望它做的这个特殊的映射

I'm trying to use AutoMapper to take data from a class that has prefixes before property names and map it to a second class that doesn't have those prefixes. However, I don't necessarily want it to always strip out that prefix: I just want it to do it for this particular mapping.

我的源类看起来是这样的:

My source class looks like this:

public class AdvancedSearchFilterDataModel
{
    // ....

    public string ServiceMeterNumber { get; set; }

    // ....
}



我的目标类看起来是这样的:

My destination class looks like this:

[DataContract]
public class ServicesAdvancedSearchFilterData : AdvancedSearchFilterData
{
    // ....

    [DataMember]
    public string MeterNumber { get; set; }

    // ....
}

当我尝试映射这样的价值观,它的工作原理:

When I try to map values like this, it works:

Mapper.Configuration.RecognizePrefixes("Service");
Mapper.CreateMap<AdvancedSearchFilterDataModel, ServicesAdvancedSearchFilterData>();
ServicesAdvancedSearchFilterData servciesFilterData = 
    Mapper.Map<ServicesAdvancedSearchFilterData>(model);



不过,我只是想服务被公认为是某些映射的前缀,因为它也用于在其他映射属性名称的一个正常部分。我试图用一个配置文件来处理这个问题,但这并没有工作 - 没有数据被映射:

But I only want "Service" to be recognized as a prefix for certain mappings, since it's also used as a normal part of property names in other mappings. I tried to handle this with a profile, but this didn't work -- no data was mapped:

Mapper.CreateProfile("ServicePrefix").RecognizePrefixes("Service");
Mapper.CreateMap<AdvancedSearchFilterDataModel, ServicesAdvancedSearchFilterData>()
    .WithProfile("ServicePrefix");
ServicesAdvancedSearchFilterData servciesFilterData = 
    Mapper.Map<ServicesAdvancedSearchFilterData>(model);



我怎样才能使它只认前缀时,我想它,或者使用配置文件或其他技术? (我也有,我要去给需要它承认在以同样的方式其他映射其他前缀)。

How can I make it recognize the prefix only when I want it to, either using profiles or some other technique? (I also have other prefixes that I'm going to need it to recognize for other mappings in the same way.)

推荐答案

我通过创建以下结构来实现此功能:

I achieved this functionality by creating following structure:

我是从PersonCombined压扁了我的看法Person模型

I have Person model for my view which is flattened from PersonCombined

public class PersonCombined
{
    public Person Person { get; set; }
    public Address DefaultAddress { get; set; }
    public Contact EmailContact { get; set; }
    public Contact PhoneContact { get; set; }
    public Contact WebsiteContact { get; set; }
}

public class Person : IWebServiceModel
{
    public int ID { get; set; }

    public string PersonFirstName { get; set; }
    public string PersonSurname { get; set; }
    public string PersonDescription { get; set; }
    public Nullable<bool> PersonIsActive { get; set; }
}



然后,我有单独的类,这种映射只是看起来像这样:

Then I have separate class for this mapping only that looks like this:

public class PersonCustomMapping : ICustomMapping
{
    const string separator = " ";

    private static IMappingEngine _MappingEngine;
    public IMappingEngine MappingEngine
    {
        get
        {
            if (_MappingEngine == null)
            {
                var configuration = new ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers);
                configuration.RecognizePrefixes("Person");
                configuration.RecognizeDestinationPrefixes("Person");

                configuration.CreateMap<Person, MCIACRM.Model.Combine.PersonCombined>();
                configuration.CreateMap<MCIACRM.Model.Combine.PersonCombined, Person>();


                _MappingEngine = new MappingEngine(configuration);
            }

            return _MappingEngine;
        }
    }
}

在我一般认为我有引擎(MappingEngine)属性是这样的:

In my generic view I have mappingEngine property like this:

    private IMappingEngine mappingEngine
    {
        get
        {
            if (_mappingEngine == null)
            {
                _mappingEngine = AutoMapper.Mapper.Engine;
            }

            return _mappingEngine;
        }
    }



最后,在我的通用视图构造我有:

Finally in my generic view constructor i have:

    public GenericEntityController(IGenericLogic<S> logic, ICustomMapping customMapping)
        : base()
    {
        this._mappingEngine = customMapping.MappingEngine;
        this.logic = logic;
    }



这就是我该怎么做映射:
结果= items.Project(引擎(MappingEngine))。为了< R>();

logic.Update(mappingEngine.Map< S>( wsItem));

由于我使用按次1实体我可以定义每个实体定制映射配置。
希望这有助于

Because I use 1 entity per view I can define custom mapping configuration per entity. Hope this helps

这篇关于AutoMapper是不承认具体的配置文件的前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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