Migrating to Automapper 4.2.1 from Automapper 2.2.1

纵然是瞬间 提交于 2019-12-14 03:44:49

问题


I have a project where I have been using automapper 2.2.1. I upgraded to 4.2.1 and am completely broken. I tried to follow this by changing all references to Mapper.CreateMap<Source, Target>() to Mapper.Initialize(cfg => { cfg.CreateMap<Source, Target>()}). I'm now broken bad and I cant seem to get it done.I'm getting "System.Reflection.ReflectionTypeLoadException was unhandled by user code exception with Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information", message. What am I missing. Code looks like this:

AutomapperTypeAdapterFactory

public class AutomapperTypeAdapterFactory : ITypeAdapterFactory
{
    public AutomapperTypeAdapterFactory()
    {
        //scan all assemblies finding Automapper Profile
        var profiles = AppDomain.CurrentDomain
                                .GetAssemblies()
                                .SelectMany(a => a.GetTypes())
                                .Where(t => t.BaseType == typeof(Profile));

        Mapper.Initialize(cfg =>
        {
            foreach (var item in profiles)
            {
                if (item.FullName != "AutoMapper.SelfProfiler`2")
                    cfg.AddProfile(Activator.CreateInstance(item) as Profile);
            }
        });
    }

    public ITypeAdapter Create() => new AutomapperTypeAdapter();
}

Department Profile

public class DepartmentProfile : Profile
{
    protected override void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Department, DepartmentDto>();
        });
    }
}

UnityContainer

public static class Container
{
    static IUnityContainer _container = new UnityContainer();

    static Container()
    {
        var typeAdapterFactory = _container.Resolve<ITypeAdapterFactory>();
        TypeAdapterFactory.SetCurrent(typeAdapterFactory);
    }
    //other container resolutions....
}

回答1:


In projects where I've moved away from the static configuration I've defined a few mapping profiles (generally a profile per application layer):

public class MappingProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Source, Destination>();
    }
}

I then configure my IoC (SimpleInjector, in my case) to find all profiles and add them to a single, unified configuration. I then register the MapperConfiguration in my container, as well as the IMapper object created from the same:

    public void ConfigureSimpleInjector(IAppBuilder app)
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

        container.RegisterAutoMapper();

        container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

        container.RegisterMvcIntegratedFilterProvider();

        container.Verify();

        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    }


public static Container RegisterAutoMapper(this Container container)
    {
        var profiles = typeof(AutoMapperRegistry).Assembly.GetTypes().Where(t => typeof(Profile).IsAssignableFrom(t)).Select(t => (Profile)Activator.CreateInstance(t));

        var config = new MapperConfiguration(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });

        container.Register<MapperConfiguration>(() => config);
        container.Register<IMapper>(() => container.GetInstance<MapperConfiguration>().CreateMapper());

        return container;
    }
}

I can then use DI for IMapper (or you could store it statically), as well as the MapperConfiguration for use in LINQ projections.



来源:https://stackoverflow.com/questions/36686955/migrating-to-automapper-4-2-1-from-automapper-2-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!