C# Automapper - 为什么使用 Mapper.Initialize?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12977273/
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-10 01:11:05  来源:igfitidea点击:

Automapper - why use Mapper.Initialize?

c#automapper

提问by Jez

I wouldn't normally ask this kind of question on here, but unfortunately whilst AutoMapperseems to be a good mapping library, its documentation is woefully bad - there is no XML documentation for the library's methods, and the most official online documentation I could find was this, which is very brisk. If anyone has any better documentation, please let me know.

我通常不会在这里问这种问题,但不幸的是,虽然AutoMapper似乎是一个很好的映射库,但它的文档非常糟糕 - 没有该库方法的 XML 文档,以及我能找到的最官方的在线文档是这个,非常轻快。如果有人有更好的文档,请告诉我。

That said, here's the question: why use Mapper.Initialize? It doesn't seem to be required as you can just use Mapper.CreateMapimmediately, and as there is no documentation I have no clue what Initializeis meant to do.

也就是说,问题来了:为什么要使用Mapper.Initialize?它似乎不是必需的,因为您可以Mapper.CreateMap立即使用,并且由于没有文档,我不知道要做什么Initialize

采纳答案by Jez

I asked on the AutoMapper users list, and this answer basically says why:

我在 AutoMapper 用户列表上问过,这个答案基本上说明了原因:

https://groups.google.com/forum/?fromgroups=#!topic/automapper-users/0RgIjrKi28U

https://groups.google.com/forum/?fromgroups=#!topic/automapper-users/0RgIjrKi28U

It's something to do with allowing AutoMapper to do deterministic (stochastic) optimization. Performance-wise, it's better to get all your mappings created in the Initializecall.

这与允许 AutoMapper 进行确定性(随机)优化有关。在性能方面,最好在Initialize调用中创建所有映射。

回答by Rob West

The initialization runs all the map creation once so it is then done when you come to do your mapping. You can create a map whenever you want, but this will slow your code down as the mapping creation involves reflection.

初始化运行所有地图创建一次,以便在您进行映射时完成。您可以随时创建映射,但这会降低代码速度,因为映射创建涉及反射。

I find it best to use profiles for my mapping code and use something like the following to get this all setup:

我发现最好为我的映射代码使用配置文件并使用类似以下内容来完成所有设置:

public class AutoMapperConfiguration : IRequiresConfigurationOnStartUp
{
    private readonly IContainer _container;

    public AutoMapperConfiguration(IContainer container)
    {
        _container = container;
    }

    public void Configure()
    {
        Mapper.Initialize(x => GetAutoMapperConfiguration(Mapper.Configuration));
    }

    private void GetAutoMapperConfiguration(IConfiguration configuration)
    {
        var profiles = GetProfiles();
        foreach (var profile in profiles)
        {
            configuration.AddProfile(_container.GetInstance(profile) as Profile);
        }
    }

    private static IEnumerable<Type> GetProfiles()
    {
        return typeof(AutoMapperConfiguration).Assembly.GetTypes()
            .Where(type => !type.IsAbstract && typeof(Profile).IsAssignableFrom(type));
    }
}