繁体   English   中英

Automapper,INamingConvention Camelcase属性为大写,带下划线

[英]Automapper, INamingConvention Camelcase properties to uppercase with underscore

我有两个类,一个是Entity Framework的generetad,另一个是我到处使用的类。

我的课 :

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

EF课程:

public class PERSON
{
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }
}

当源是PERSON to Person ,我找到了解决方案, 但是我找不到Person to PERSON的解决方案(属性是大写和下划线分隔符)。

The solution for PERSON to Person :

Mapper.Initialize(x => x.AddProfile<Profile1>());
var res = Mapper.Map<PERSON, Person>(person);

public class UpperUnderscoreNamingConvention : INamingConvention
{
    private readonly Regex _splittingExpression = new Regex(@"[p{Lu}0-9]+(?=_?)");
    public Regex SplittingExpression
    {
        get { return _splittingExpression; }
    }

    public string SeparatorCharacter
    {
        get { return "_"; }
    }
}

public class Profile1 : Profile
{
    protected override void Configure()
    {
        SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
        DestinationMemberNamingConvention = new PascalCaseNamingConvention();
        CreateMap<PERSON, Person>();
    }
}

这适用于AutoMapper的7.0.1版:

using AutoMapper;
using System.Text.RegularExpressions;


namespace Data.Service.Mapping
{
    public class UpperUnderscoreNamingConvention: INamingConvention
    {
        public Regex SplittingExpression { get; } = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=_?)");

        public string SeparatorCharacter => "_";

        public string ReplaceValue(Match match) => match.Value.ToUpper();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM