AutoMapper:开闭原则

AutoMapper : Open - Closed Principle

我从给定的 link

中得到了代码

AutoMapper Code for Open Closed Principle Code

我正在尝试在我的项目中使用它,但由于静态 API 已从 AutoMapper 版本 4.2.0 中删除,我被卡住了。供参考see this

任何人都可以帮助我如何在最新版本的 Automapper 中实现以下代码。

  1. Mapper.CreateMap(TSource, TDestination)

    private void RegisterStandardMappings(IEnumerable<Type> types)
    {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapForm<>)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select new
                    {
                        Source = i.GetGenericArguments()[0],
                        Destination = t
                    }).ToArray();
    
        foreach (var map in maps)
        {
            //Need to optimize below line with current version.
            Mapper.CreateMap(map.Source, map.Destination);
        }
    }
    
  2. 正在获取 IConfiguration,因为它已更改为 IConfigurationProvider

    private void ReverseCustomMappings(IEnumerable<Type> types)
    {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where typeof(IHaveCustomMappings).IsAssignableFrom(t)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray();
    
        foreach (var map in maps)
        {
            //Need to optimize below line with current version.
            map.CreateMappings(Mapper.Configuration);
        }
    }
    
    public interface IHaveCustomMappings
    {
        void CreateMappings(IConfiguration configuration);
    }
    

请提供您的建议 - 感谢您的帮助。

class AutoMapperTrail101_Console_OCP_Program
{
    static void Main(string[] args)
    {
        //Initialize Automapper 
        AutoMapperConfiguration.Initialize();

        User user = new User()
        {
            Id = 7,
            Name = "Chandrahas J. Poojari"
        };

        EmployeeViewModel emv = new EmployeeViewModel();

        Mapper.Map(user, emv);

        Console.WriteLine("Employee Id :> " + emv.UserId);
        Console.WriteLine("Employee Name :> " + emv.UserName);
        Console.ReadLine();

    }
}

class AutoMapperConfiguration
{
    public static void Initialize()
    {
        var types = Assembly.GetExecutingAssembly().GetExportedTypes();

        //Mapper Initialize only once
        Mapper.Initialize(cfg =>
        {
            cfg.AllowNullDestinationValues = false;

            RegisterStandardMappings(types, cfg);
            RegisterReverseMappings(types, cfg);
            ReverseCustomMappings(types, cfg);

        });
    }

    private static void RegisterStandardMappings(IEnumerable<Type> types, IMapperConfigurationExpression mce)
    {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select new
                    {
                        Source = i.GetGenericArguments()[0],
                        Destination = t
                    }).ToArray();

        foreach (var map in maps)
        {
            mce.CreateMap(map.Source, map.Destination);
        }
    }

    private static void RegisterReverseMappings(IEnumerable<Type> types, IMapperConfigurationExpression mce)
    {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapTo<>)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select new
                    {
                        Source = t,
                        Destination = i.GetGenericArguments()[0]
                    }).ToArray();

        foreach (var map in maps)
        {
            mce.CreateMap(map.Source, map.Destination);
        }
    }

    private static void ReverseCustomMappings(IEnumerable<Type> types, IMapperConfigurationExpression mce)
    {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where typeof(IHaveCustomMappings).IsAssignableFrom(t)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray();

        foreach (var map in maps)
        {
            map.CreateMappings(mce);
        }
    }     
}`

interface IMapFrom<T> { }

interface IMapTo<T> { }

interface IHaveCustomMappings { void CreateMappings(IMapperConfigurationExpression mce); }

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class EmployeeViewModel : IMapFrom<User>, IHaveCustomMappings
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public void CreateMappings(IMapperConfigurationExpression mce)
    {
        mce.CreateMap<User, EmployeeViewModel>()
            .ForMember("UserId", opt => { opt.MapFrom("Id"); })
            .ForMember("UserName", opt => { opt.MapFrom("Name"); });
    }
}

@AutoMapper 如果需要,请改进代码......