AutoMapper with prefix

岁酱吖の 提交于 2019-12-01 15:44:55
Mapper.Initialize(cfg =>
{
   cfg.RecognizeDestinationPrefixes("Cust_");
   cfg.CreateMap<A, B>();
});

A a = new A() {FirstName = "Cliff", LastName = "Mayson"};
B b = Mapper.Map<A, B>(a);

//b.Cust_FirstName is "Cliff"
//b.Cust_LastName is "Mayson"

Or alternatively:

Mapper.Configuration.RecognizeDestinationPrefixes("Cust_");
Mapper.CreateMap<A, B>();
...
B b = Mapper.Map<A, B>(a);
...

The documentation has an article on Recognizing pre/postfixes

Sometimes your source/destination properties will have common pre/postfixes that cause you to have to do a bunch of custom member mappings because the names don't match up. To address this, you can recognize pre/postfixes:

public class Source {
    public int frmValue { get; set; }
    public int frmValue2 { get; set; }
}
public class Dest {
    public int Value { get; set; }
    public int Value2 { get; set; }
}
Mapper.Initialize(cfg => {
    cfg.RecognizePrefix("frm");
    cfg.CreateMap<Source, Dest>();
});

Mapper.AssertConfigurationIsValid(); By default AutoMapper recognizes the prefix "Get", if you need to clear the prefix:

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