使用Autofac和AutoMapper配置文件进行IoC [英] IoC with AutoMapper Profile using Autofac

查看:309
本文介绍了使用Autofac和AutoMapper配置文件进行IoC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用AutoMapper已有一段时间了.我的个人资料设置如下:

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>());

但是,我现在想使用IoC将依赖项传递给ViewModelAutoMapperConfiguration构造函数.我正在使用Autofac.我一直在这里阅读文章: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/automapper-and-ioc.aspx ,但我看不到它如何与Profiles配合使用.

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.

有什么想法吗? 谢谢

推荐答案

好吧,我发现了使用AddProfile重载的方法.有一个重载需要一个配置文件的实例,因此我可以在将该实例传递给AddProfile方法之前对其进行解析.

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.

这篇关于使用Autofac和AutoMapper配置文件进行IoC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆