.net core AutoMapper 9通用扩展静态类

.net core AutoMapper 9通用扩展静态类,.net-core,automapper,.net Core,Automapper,我想要一些帮助 当我将AutoMappar 9停止工作时,我有一个使用Automapper 8 MAA的泛型类。 我在寻找解决办法,但没有找到能帮助我的人 using System.Collections.Generic; namespace Intranet.Services.Extensions { internal static class AutoMapperExtensions { public static T MapTo<T>(this o

我想要一些帮助

当我将AutoMappar 9停止工作时,我有一个使用Automapper 8 MAA的泛型类。 我在寻找解决办法,但没有找到能帮助我的人

using System.Collections.Generic;

namespace Intranet.Services.Extensions {

    internal static class AutoMapperExtensions {

        public static T MapTo<T>(this object value)
        {
            return AutoMapper.Mapper.Map<T>(value);
        }

        public static IEnumerable<T> EnumerableTo<T>(this object value)
        {
            return AutoMapper.Mapper.Map<IEnumerable<T>>(value);
        }
    }
}
使用System.Collections.Generic;
命名空间Intranet.Services.Extensions{
内部静态类扩展{
公共静态T映射到(此对象值)
{
返回AutoMapper.Mapper.Map(值);
}
公共静态IEnumerable EnumerableTo(此对象值)
{
返回AutoMapper.Mapper.Map(值);
}
}
}
T AutoMapper.Mapper.Map(对象源)对象引用是 非静态字段、方法或属性必需 “映射器映射(对象)”(CS0120)


AutoMapper 9希望您使用依赖性注射。这增加了我的遗留项目的复杂性,我只是不想处理

所以我只是对它之前所做的做了反向工程,并为它编写了一个包装器

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();
}
公共静态类MapperWrapper
{
私有常量字符串InvalidOperationMessage="映射器未初始化。请使用适当的配置调用Initialize。如果您试图通过容器或其他方式使用映射器实例,请确保您没有任何对静态映射器.Map方法的调用,如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保在中传递适当的IConfigurationProvider立场。”;
private const string AlreadyInitialized=“映射程序已初始化。您必须为每个应用程序域/进程调用Initialize一次。”;
专用静态IConfigurationProvider\u配置;
私有静态IMapper_实例;
专用静态IConfigurationProvider配置
{
get=>\u配置??抛出新的InvalidOperationException(InvalidOperationMessage);
set=>\u configuration=(\u configuration==null)?值:抛出新的InvalidOperationException(AlreadyInitialized);
}
公共静态IMapper映射器
{
get=>\u实例??抛出新的InvalidOperationException(InvalidOperationMessage);
私有集=>_实例=值;
}
公共静态无效初始化(操作配置)
{
初始化(新的MapperConfiguration(配置));
}
公共静态无效初始化(MapperConfiguration配置)
{
配置=配置;
Mapper=Configuration.CreateMapper();
}
public static void assertconfigurationsvalid()=>Configuration.assertconfigurationsvalid();
}
要使用它,只需在旧的Mapper调用之前添加MapperWrapper

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

你好,Lucian,谢谢。我已经看过了文档,知道它不能做更多的事情。我想有人会有其他选择。我不想直接映射到控件中,而是想保留一个扩展。我将更改编码。