asp.net - 如何在没有依赖注入(inject)的情况下在 Asp.Net Web Api 2 中使用 AutoMapper 9.0.0?

标签 asp.net asp.net-web-api2 automapper

我无法找到任何信息将这段代码放入我的项目中。现在我在每个需要映射器的 Action 中使用它。有没有没有依赖注入(inject)的更好方法来做到这一点?

var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<Source, Dest>();
            });
            IMapper iMapper = config.CreateMapper();


            var destList= iMapper.Map<Dest[]>(sourceList);

最佳答案

依赖注入(inject)给我的遗留项目增加了一个整体的复杂性,我只是不想处理。 9.0 移除了静态调用它的 api。

所以我只是对它在 8.0 中所做的事情进行了逆向工程,并为它编写了一个包装器。

public static class MapperWrapper 
{
    private const string InvalidOperationMessage = "Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.";
    private const string AlreadyInitialized = "Mapper already initialized. You must call Initialize once per application domain/process.";

    private static IConfigurationProvider _configuration;
    private static IMapper _instance;

    private static IConfigurationProvider Configuration
    {
        get => _configuration ?? throw new InvalidOperationException(InvalidOperationMessage);
        set => _configuration = (_configuration == null) ? value : throw new InvalidOperationException(AlreadyInitialized);
    }

    public static IMapper Mapper
    {
        get => _instance ?? throw new InvalidOperationException(InvalidOperationMessage);
        private set => _instance = value;
    }

    public static void Initialize(Action<IMapperConfigurationExpression> config)
    {
        Initialize(new MapperConfiguration(config));
    }

    public static void Initialize(MapperConfiguration config)
    {
        Configuration = config;
        Mapper = Configuration.CreateMapper();
    }

    public static void AssertConfigurationIsValid() => Configuration.AssertConfigurationIsValid();
}

要初始化它有一个配置方法

public static class AutoMapperConfig
{
    public static void Configure()
    {
        MapperWrapper.Initialize(cfg =>
        {
            cfg.CreateMap<Foo1, Foo2>();              
        });

        MapperWrapper.AssertConfigurationIsValid();
    }
}

然后在你的启动中调用它

AutoMapperConfig.Configure();

要使用它,只需在调用 Mapper 之前添加 MapperWrapper 即可。可以在任何地方调用。

 MapperWrapper.Mapper.Map<Foo2>(Foo1);

关于asp.net - 如何在没有依赖注入(inject)的情况下在 Asp.Net Web Api 2 中使用 AutoMapper 9.0.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58190428/

相关文章:

c# - 根据另一个属性在 View 中显示模型属性的最佳实践

asp.net - 如何在使用动态where子句时在mysql中使用execute语句

c# - 在具有大量流量的异步 ASP.NET Web API 中,将 ConfigureAwait 设置为 false 会产生什么影响?

c# - Global.asax 中的多个 AutoMapper.Configure()

c# - 升级到 9 后无法访问自动映射器上下文项

c# - WebRequest/WebResponse 内存泄漏

asp.net - 无法加载类型 'System.Web.Optimization.StyleBundle'

c# - Web Api 使用 DataAnnotations 验证可选 Url

asp.net - 如何知道 oauth 访问 token 是否过期

c# - Automapper嵌套实体配置最佳实践