本文介绍了我怎么AutoMapper处理自定义命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个项目我的工作,我们正在映射自动生成的DTO到业务对象。该数据库有一个啊哈不寻常的(但基本一致)的命名规则,这意味着它可能最DTO属性名称转换为等效的业务对象属性的名称,这样就节省了很多行代码。

In the project I'm working on, we are mapping auto-generated DTOs to business objects. 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(和数据库),我们有一个名为 account_ID__created 属性,将映射到名为BO属性 CreatedAccountId 。这是 MemberNameTransformer.GetBoMemberName发生()的那种转变,所以它不是与一个不同的分隔符略有不同的约定一样简单。

For example, in the DTO (and database) we have a property called account_ID__created that will map to a BO property called CreatedAccountId. 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.

随着我在AutoMapper源代码可用,我这是我最好的猜测:

Following what I have available in the AutoMapper source code, I have this as my best guess:

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

现在,我怎么告诉映射器使用映射到SomeDto时SomeBusinessClass这些选项?我意识到我可能在IMappingOptions错接口。什么我想要实现真正的肉是在 MemeberNameTransformer.GetBoMemberName()

Now, how do I tell the Mapper to use these options when mapping SomeDto to SomeBusinessClass? I realize I may have the wrong interface in IMappingOptions. The real meat of what I'm trying to accomplish is in MemeberNameTransformer.GetBoMemberName().

附加题:如何我告诉映射器映射时使用这些选项的任何的IDto到IBusinessObject

Extra credit: How do I tell the Mapper to use these options when mapping any IDto to IBusinessObject?

推荐答案

如果事情真的是一致的,就像textFirstName,你可以使用一些内置的功能。

If things are really consistent, like textFirstName, you can use some built in functions.

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



否则,你需要编写自己的 INamingConvention 类,它看起来是这样的..

Otherwise, you'll need to write your own INamingConvention class that looks something like this..

class DTONaming : INamingConvention
{

    #region INamingConvention Members

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

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

    #endregion
}



然后你可以用automapper寄存器

Then you can register that with automapper.

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

和AutoMapper将用任何映射,所以如果你需要限制这些前缀的注册或自定义的命名对象则可能​​需要初始化并重新初始化或东西。我怀疑命名方案将有后果,但。

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. I doubt the naming scheme would have consequences though.

修改

通过您的您将使用最近增加一个 SourceMemberNameTransformer 来代替。这使您可以编写自己转换的名称功能

With your recent additions you will be using a SourceMemberNameTransformer instead. 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;
}

这篇关于我怎么AutoMapper处理自定义命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 05:56