AutoMapper 9 通用扩展静态 class
AutoMapper 9 generic extension static class
我需要一些帮助。
当我让 AutoMappar 9 停止工作时,我有一个通用的 class 使用 Automapper 8 maas。
我寻找解决方案但没有找到可以帮助我的人?
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);
}
}
}
T AutoMapper.Mapper.Map(object source) An object reference is
required for the non-static field, method, or property
'Mapper.Map(object)' (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();
}
要使用它,只需在旧的 Mapper 调用之前添加 MapperWrapper。
MapperWrapper.Mapper.Map<Foo2>(Foo1);
我需要一些帮助。
当我让 AutoMappar 9 停止工作时,我有一个通用的 class 使用 Automapper 8 maas。 我寻找解决方案但没有找到可以帮助我的人?
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);
}
}
}
T AutoMapper.Mapper.Map(object source) An object reference is required for the non-static field, method, or property 'Mapper.Map(object)' (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();
}
要使用它,只需在旧的 Mapper 调用之前添加 MapperWrapper。
MapperWrapper.Mapper.Map<Foo2>(Foo1);