通用Automapper功能,具有用于下划线属性的自定义对流

时间:2015-06-29 02:45:41

标签: automapper auto-generate convention automapper-3

我只需要将一些自动生成的类从数据库映射到domain / viewmodels类。自动生成的类可能具有customer_id这样的名称,我想用CustomerId映射。不知何故,我想用自动映射器注册我自己的约定。我已经开始使用以下代码,但是未填充映射的对象属性:

  // Generic method that should map source to target
  public static TTarget MapWithUnderScoreConvension(TSource source, TTarget target)
    {
        Mapper.Initialize(cfg=> cfg.AddProfile<AutoMapperUnderScoreProfile>());
        Mapper.CreateMap<TSource, TTarget>();
        var mapped = Mapper.Map(source, target);
        return mapped;
    } 

 // New underscore profile
public class AutoMapperUnderScoreProfile : Profile
{
    public AutoMapperUnderScoreProfile()
    {
        Mapper.Initialize(configuration => configuration.CreateProfile("UnderScoreProfile", UnderScoreProfile));
        Mapper.AssertConfigurationIsValid();
    }

    private void UnderScoreProfile(IProfileExpression profile)
    {
        profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
        profile.DestinationMemberNamingConvention = new SourceUnderScoreNamingConvension();
    }
}

// Convention class
public class SourceUnderScoreNamingConvension : INamingConvention
{
    private readonly string _separatorCharacter="_";
    private readonly Regex _splittingExpression = new Regex(@"[\p{Lu}0-9]+(?=_?)");

    public Regex SplittingExpression { get { return _splittingExpression;} private set{} }
    public string SeparatorCharacter { get { return _separatorCharacter; } private set{} }
}

// Test cases
 [TestMethod()]
    public void Test_Map_Db_Generated_Class_To_Model()
    {
        var dbGenerated = new QuestionTypeFromDb()
        {
            QuestionType_Description = "1",
            QuestionType_Id = 1,
            QuestionType_Is_Default = true,
            QuestionType_Is_TimeBased = true,
            QuestionType_Sequence = 1,
            QuestionType_Time_In_Seconds = 1
        };

        var mappedObject = AutoMapperHelper<QuestionTypeFromDb, QuestionType>
            .MapWithUnderScoreConvension(dbGenerated, new QuestionType());

        mappedObject.QuestionTypeId.Should().Be(dbGenerated.QuestionType_Id);
        mappedObject.QuestionTypeDescription.Should().Be(dbGenerated.QuestionType_Description);
        mappedObject.TimeInSeconds.Should().Be(dbGenerated.QuestionType_Time_In_Seconds);
        mappedObject.QuestionTypeSequence.Should().Be(dbGenerated.QuestionType_Sequence);
    }

 public class TestQuestionWithAnswerType 
{
    public int QuestionTypeId { get; set; }
    public string QuestionTypeDescription { get; set; }
    public int QuestionTypeSequence { get; set; }
    public bool QuestionTypeIsTimeBased { get; set; }
    public int? QuestionTypeTimeInSeconds { get; set; }
    public bool QuestionTypeIsDefault { get; set; }
}

任何评论都将不胜感激。

更新

我发现以下解决方法有效: 我只是替换使用了这个 - &gt;用任何东西替换'下划线'(Mapper.Initialize(c =&gt; c.ReplaceMemberName(“_”,“”));

 public static TTarget MapWithUnderScoreConvension(TSource source, TTarget target)
    {
        Mapper.Initialize(c => c.ReplaceMemberName("_", ""));
        //Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperUnderScoreProfile>());
        Mapper.CreateMap<TSource, TTarget>();
        var mapped = Mapper.Map(source, target);
        return mapped;
    }

2 个答案:

答案 0 :(得分:0)

您的正则表达式需要更改为:[\p{L}}0-9]+(?=_?)

这将照顾Customer_Id,CUSTOMER_ID,customer_id

{L} unicode类别包括提及here的Lu,Lt,Ll,Lm和Lo字符。

答案 1 :(得分:0)

在问题的“更新”部分添加了答案。基本上我的解决方案非常简单 - &gt; Mapper.Initialize(c =&gt; c.ReplaceMemberName(&#34; _&#34;,&#34;&#34;));