C# 在 Automapper 中使用 Profiles 来映射具有不同逻辑的相同类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:10:01  来源:igfitidea点击:

Using Profiles in Automapper to map the same types with different logic

c#automapper

提问by sebd

I am using AutoMapper in my ASP.NET MVC website to map my database objects to ViewModel objects and I am trying to use several profiles to map the same types, but using another logic. I had the idea of doing so by reading Matt's blog post where he says:

我在我的 ASP.NET MVC 网站中使用 AutoMapper 将我的数据库对象映射到 ViewModel 对象,我试图使用多个配置文件来映射相同的类型,但使用另一种逻辑。通过阅读马特的博客文章,我有了这样做的想法,他说:

The really key part is the AutoMapper configuration profile. You can group configurations with profiles. Maybe in one profile you format dates in one way, in another profile you format dates in another way. I'm just using one profile here.

真正关键的部分是 AutoMapper 配置文件。您可以使用配置文件对配置进行分组。也许在一个配置文件中您以一种方式格式化日期,在另一个配置文件中您以另一种方式格式化日期。我在这里只使用一个配置文件。

So I created a profile for one case:

所以我为一个案例创建了一个配置文件:

public class MyProfile : Profile
{
    protected override string ProfileName
    {
        get
        {
            return "MyProfile";
        }
    }

    protected override void Configure()
    {
        CreateMap<DateTime, String>().ConvertUsing<StringFromDateTimeTypeConverter>();
    }
}

public class StringFromDateTimeTypeConverter : ITypeConverter<DateTime, String>
{
    public string Convert(DateTime source)
    {
        return source.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
    }
}

And another one for another case:

另一个案例:

public class MyProfile2 : Profile
{
    protected override string ProfileName
    {
        get
        {
            return "MyProfile2";
        }
    }

    protected override void Configure()
    {
        CreateMap<DateTime, String>().ConvertUsing<AnotherStringFromDateTimeTypeConverter>();
    }
}

public class AnotherStringFromDateTimeTypeConverter : ITypeConverter<DateTime, String>
{
    public string Convert(DateTime source)
    {
        return source.ToString("mm - yyyy", CultureInfo.InvariantCulture);
    }
}

However, I cannot find any overload of the Mapper.Map<>()method to specify a profile. I also had a look at the Configurationobject with no luck.
The last registered profile always takes precedence.

但是,我找不到Mapper.Map<>()指定配置文件的方法的任何重载。我也看了看这个Configuration对象,但没有运气。
最后注册的配置文件始终优先。

Is there a way to use profiles for this purpose?

有没有办法为此目的使用配置文件?

采纳答案by Jimmy Bogard

Profiles are for segregating common configuration applied across several type maps, like formatting. However, type maps are still global. You're better off creating separate Configuration objects, and creating a separate MappingEngine for each. The Mapper class is merely a static facade over each of those, with some lifecycle management.

配置文件用于隔离跨多个类型映射应用的通用配置,如格式。但是,类型映射仍然是全局的。您最好创建单独的 Configuration 对象,并为每个对象创建一个单独的 MappingEngine。Mapper 类只是它们中的每一个的静态外观,具有一些生命周期管理。