2016-04-19 88 views
0

我想註冊一個映射約定來處理從具有Pascal Case名稱的類映射到帶有後綴和前綴的下劃線名稱的類,然後再返回。我試圖遵循例子,但無法理解它應該如何工作。AutoMapper屬性名稱轉換

這其中的很多事情我已經試過,看起來像它應該工作(在我看來:)),但它似乎並沒有做任何事情:

​​

我是什麼在這裏失蹤?

回答

0

我終於找到了一個可行的解決方案。我創建了兩個配置文件,每個「方向」一個配置文件,並向它們添加映射。

我不太滿意,因爲我寧願在同一個文件中的映射(將它們分組在業務領域)。但至少它的工作原理...... :)

我也嘗試把註冊在同一個配置文件,並使用.WithProfile(「ToUnderscoreWithPrefix」)方法,但我沒有得到那個工作。

Mapper.Initialize(cfg => 
{ 
    cfg.AddProfile(new ToUnderscoreWithPrefixMappings()); 
    cfg.AddProfile(new FromUnderscoreWithPrefixMappings()); 
}); 

public class ToUnderscoreWithPrefixMappings : Profile 
{ 
    protected override void Configure() 
    { 
     RecognizeDestinationPrefixes("P", "p"); 

     SourceMemberNamingConvention = new PascalCaseNamingConvention(); 
     DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention(); 

     CreateMap<PascalCaseEntity, UnderscoreWithPrefixAndPostfixEntity>(); 
    } 

    public override string ProfileName { get; } = "ToUnderscoreWithPrefix"; 
} 

public class FromUnderscoreWithPrefixMappings : Profile 
{ 
    protected override void Configure() 
    { 
     RecognizePrefixes("P_", "p_"); 
     RecognizePostfixes("_"); 

     SourceMemberNamingConvention = new LowerUnderscoreNamingConvention(); 
     DestinationMemberNamingConvention = new PascalCaseNamingConvention(); 

     CreateMap<UnderscoreWithPrefixAndPostfixEntity, PascalCaseEntity>(); 
    } 

    public override string ProfileName { get; } = "FromUnderscoreWithPrefix"; 
} 
相關問題