AutoMapper isn't recognizing profile-specific prefixes

大憨熊 提交于 2019-12-04 14:35:43

I achieved this functionality by creating following structure:

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;
        }
    }
}

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;
    }

And that's how I do mapping: result = items.Project(mappingEngine).To<R>(); or logic.Update(mappingEngine.Map<S>(wsItem));

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!