How do I get AutoMapper to deal with a custom naming convention?

让人想犯罪 __ 提交于 2019-11-30 21:28:23

If things are really consistent, like textFirstName, you can use some built in functions.

Mapper.Initialize(cfg => cfg.RecognizePrefixes(new[] { "text" }));

Otherwise, you'll need to write your own INamingConvention class that looks something like this..

class DTONaming : INamingConvention
{

    #region INamingConvention Members

    public string SeparatorCharacter
    {
        get { return string.Empty; }
    }

    public Regex SplittingExpression
    {
        get { return new Regex(""); }
    }

    #endregion
}

Then you can register that with automapper.

Mapper.Initialize(cfg => cfg.SourceMemberNamingConvention = new DTONaming());

And AutoMapper will use this for any mappings, so if you need to restrict the registration of these prefixes or custom naming objects you may need to initialize and re-initialize it or something. I doubt the naming scheme would have consequences though.

Edit

With your recent additions you will be using a SourceMemberNameTransformer instead. This allows you to write a function that converts the names yourself.

Mapper.Initialize(cfg => cfg.SourceMemberNameTransformer = ConvertNames);
private static string ConvertNames(string inputString)
{
    var sections = inputString.Split('_');
    // transform the sections into w/e you need
    return inputString;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!