使用Autofac和AutoMapper配置文件进行IoC [英] IoC with AutoMapper Profile using Autofac
问题描述
我已经使用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屋!