1

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

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

任何意见将不胜感激。

更新

我发现以下解决方法有效:我只是替换了 used this -> to replace 'underscore' with nothing (Mapper.Initialize(c => 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;
    }
4

2 回答 2

1

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

这将处理 Customer_Id、CUSTOMER_ID、customer_id

{L} unicode 类别包括此处提到的 Lu、Lt、Ll、Lm 和 Lo 字符。

于 2015-06-29T21:31:53.070 回答
0

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

于 2015-06-30T07:55:53.013 回答