Automapper,INAMingConvention驼峰式字符集属性以带下划线的大写字母表示 [英] Automapper, INamingConvention Camelcase properties to uppercase with underscore

查看:314
本文介绍了Automapper,INAMingConvention驼峰式字符集属性以带下划线的大写字母表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,一个是由Entity Framework提供的,另一个是我到处使用的类.

I have two classes one generetad by Entity Framework, the other is the class I use everywhere.

我的班级:

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的解决方案(属性为大写和下划线分隔符).

I found the solution when the source is PERSON to Person, but I don't find the solution for Person to PERSON (the properties are in uppercase and underscore separator).

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

推荐答案

这适用于7.0.1版的AutoMapper:

This is working for version 7.0.1 of AutoMapper:

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

这篇关于Automapper,INAMingConvention驼峰式字符集属性以带下划线的大写字母表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆