【问题标题】:Automapper, INamingConvention Camelcase properties to uppercase with underscoreAutomapper,INamingConvention Camelcase 属性转为大写,带下划线
【发布时间】:2012-08-17 05:53:38
【问题描述】:

我有两个类,一个是由实体框架生成的,另一个是我到处使用的类。

我的班级:

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

当源是PERSONPerson时我找到了解决方案,但是我没有找到PersonPERSON的解决方案(属性为大写和下划线分隔符)。

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

【问题讨论】:

  • 如果您使用的是 EF,它可以将字段映射到数据库字段,我的意思是无论它们在数据库中如何调用,您都可以在 EF 模型中给出好听的名称,我想我会这样做方式。如果您使用的是 EF 的 dbml - 它是纯 xml,您可以编写 xslt 在生成代码之前添加字段映射,我们在 Linq2Sql 中使用类似的方法将一些属性替换为枚举,如果您使用 T4 生成代码,您可以对其进行调整以生成正确的代码。
  • 在数据库中,字段都是这样的“FIRST_NAME”,好的,我可以在 EF 模型中进行更改,但这几乎与使用自动映射器和 .ForMember 逐个字段分配相同的工作
  • 您不需要复制类,正如我所说,它可以自动化,这取决于您使用 EF 的方式(在代码优先场景中可能不是那么多)。
  • 看起来可能是 Automapper 中的一个错误...

标签: c# .net automapper


【解决方案1】:

这适用于 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();
    }
}

【讨论】:

    【解决方案2】:

    名称转换注意事项 Target.AB -> Source.AB 但不是 Source.A_B​​strong> 要解决此问题,请使用修改后的 RegExpression (\p{Lu}(?=$|\p{Lu})|\p{Lu}?[\p{Ll}0 -9]+)

    public class MyPascalCaseNamingConvention : INamingConvention
        {
            private static readonly Regex PascalCase = new Regex(@"(\p{Lu}(?=$|\p{Lu})|\p{Lu}?[\p{Ll}0-9]+)");
            public static readonly MyPascalCaseNamingConvention Instance = new MyPascalCaseNamingConvention();
            public Regex SplittingExpression { get; } = PascalCase;
            public string SeparatorCharacter => string.Empty;
            public string ReplaceValue(Match match) => match.Value[0].ToString().ToUpper() + match.Value.Substring(1);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2010-12-03
      • 1970-01-01
      相关资源
      最近更新 更多