简体   繁体   English

如何让AutoMapper处理自定义命名约定?

[英]How do I get AutoMapper to deal with a custom naming convention?

In the project I'm working on, we are mapping auto-generated DTOs to business objects. 在我正在开发的项目中,我们将自动生成的DTO映射到业务对象。 The database has an ahem unusual (but largely consistent) naming convention, which means that it's possible to transform most DTO property names to their equivalent business object property names, thus saving many lines of code. 该数据库有一个不寻常的啊哈 (但基本一致)的命名约定,这意味着它可能最DTO属性名称转换为等效的业务对象属性的名称,这样就节省了很多行代码。

For example, in the DTO (and database) we have a property called account_ID__created that will map to a BO property called CreatedAccountId . 例如,在DTO(和数据库)中,我们有一个名为account_ID__created的属性,该属性将映射到名为CreatedAccountId的BO属性。 This is the kind of transformation happening in MemberNameTransformer.GetBoMemberName() , so it's not as simple as a slightly different convention with a different separator. 这是在MemberNameTransformer.GetBoMemberName()发生的一种转换,因此它不像使用不同分隔符的略微不同的约定那样简单。

Following what I have available in the AutoMapper source code, I have this as my best guess: 按照我在AutoMapper源代码中提供的内容,我将此作为我的最佳猜测:

public class DtoBoMappingOptions : IMappingOptions
{
    public INamingConvention SourceMemberNamingConvention
    {
        get { return new PascalCaseNamingConvention(); }
        set { throw new NotImplementedException(); }
    }

    public INamingConvention DestinationMemberNamingConvention
    {
        get { return new PascalCaseNamingConvention(); }
        set { throw new NotImplementedException(); }
    }

    public Func<string, string> SourceMemberNameTransformer
    {
        get { return s => s; }
        set { throw new NotImplementedException(); }
    }

    public Func<string, string> DestinationMemberNameTransformer
    {
        get { return MemberNameTransformer.GetBoMemberName; }
        set { throw new NotImplementedException(); }
    }
}

Now, how do I tell the Mapper to use these options when mapping SomeDto to SomeBusinessClass? 现在,在将SomeDto映射到SomeBusinessClass时,如何告诉Mapper使用这些选项? I realize I may have the wrong interface in IMappingOptions. 我意识到我可能在IMappingOptions中有错误的界面。 The real meat of what I'm trying to accomplish is in MemeberNameTransformer.GetBoMemberName() . 我想要完成的真正的东西是在MemeberNameTransformer.GetBoMemberName()

Extra credit: How do I tell the Mapper to use these options when mapping any IDto to IBusinessObject? 额外功劳:在将任何 ID映射到IBusinessObject时,如何告诉Mapper使用这些选项?

If things are really consistent, like textFirstName, you can use some built in functions. 如果事情真的一致,比如textFirstName,你可以使用一些内置函数。

Mapper.Initialize(cfg => cfg.RecognizePrefixes(new[] { "text" }));

Otherwise, you'll need to write your own INamingConvention class that looks something like this.. 否则,您需要编写自己的INamingConvention类,看起来像这样。

class DTONaming : INamingConvention
{

    #region INamingConvention Members

    public string SeparatorCharacter
    {
        get { return string.Empty; }
    }

    public Regex SplittingExpression
    {
        get { return new Regex(""); }
    }

    #endregion
}

Then you can register that with automapper. 然后你可以用automapper注册它。

Mapper.Initialize(cfg => cfg.SourceMemberNamingConvention = new DTONaming());

And AutoMapper will use this for any mappings, so if you need to restrict the registration of these prefixes or custom naming objects you may need to initialize and re-initialize it or something. AutoMapper会将此用于任何映射,因此如果您需要限制这些前缀或自定义命名对象的注册,您可能需要初始化并重新初始化它或其他东西。 I doubt the naming scheme would have consequences though. 我怀疑命名方案会产生影响。

Edit 编辑

With your recent additions you will be using a SourceMemberNameTransformer instead. 在您最近添加的内容中,您将使用SourceMemberNameTransformer This allows you to write a function that converts the names yourself. 这允许您编写一个自己转换名称的函数。

Mapper.Initialize(cfg => cfg.SourceMemberNameTransformer = ConvertNames);
private static string ConvertNames(string inputString)
{
    var sections = inputString.Split('_');
    // transform the sections into w/e you need
    return inputString;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM