IoC with AutoMapper Profile using Autofac

你离开我真会死。 提交于 2019-12-11 01:49:56

问题


I have been using AutoMapper for some time now. I have a profile setup like so:

public class ViewModelAutoMapperConfiguration : Profile
    {
        protected override string ProfileName
        {
            get { return "ViewModel"; }
        }

        protected override void Configure()
        {
            AddFormatter<HtmlEncoderFormatter>();
            CreateMap<IUser, UserViewModel>();

        }
    }

I add this to the mapper using the following call:

Mapper.Initialize(x => x.AddProfile<ViewModelAutoMapperConfiguration>());

However, I now want to pass a dependency into the ViewModelAutoMapperConfiguration constructor using IoC. I am using Autofac. I have been reading through the article here: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/automapper-and-ioc.aspx but I can't see how this would work with Profiles.

Any ideas? Thanks


回答1:


Well, I found a way of doing it by using an overload of AddProfile. There is an overload that takes an instance of a profile, so I can resolve the instance before passing it into the AddProfile method.




回答2:


A customer of mine was wondering the same thing as DownChapel and his answer triggered me in writing some sample application.

What I've done is the following. First retrieve all Profile types from the asseblies and register them in the IoC container (I'm using Autofac).

var loadedProfiles = RetrieveProfiles();
containerBuilder.RegisterTypes(loadedProfiles.ToArray());

While registering the AutoMapper configuration I'm resolving all of the Profile types and resolve an instance from them.

private static void RegisterAutoMapper(IContainer container, IEnumerable<Type> loadedProfiles)
{
    AutoMapper.Mapper.Initialize(cfg =>
    {
        cfg.ConstructServicesUsing(container.Resolve);
        foreach (var profile in loadedProfiles)
        {
             var resolvedProfile = container.Resolve(profile) as Profile;
             cfg.AddProfile(resolvedProfile);
        }
    });
}

This way your IoC-framework (Autofac) will resolve all dependencies of the Profile, so it can have dependencies.

public class MyProfile : Profile
{
    public MyProfile(IConvertor convertor)
    {
        CreateMap<Model, ViewModel>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Identifier))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => convertor.Execute(src.SomeText)))
            ;
    }
}

The complete sample application can be found on GitHub, but most of the important code is shared over here.



来源:https://stackoverflow.com/questions/2403615/ioc-with-automapper-profile-using-autofac

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