C# 使用Autofac注入接口的特定实例
我有一个控制器,它接收一个接口的特定实例 界面如下所示:C# 使用Autofac注入接口的特定实例,c#,dependency-injection,inversion-of-control,automapper,autofac,C#,Dependency Injection,Inversion Of Control,Automapper,Autofac,我有一个控制器,它接收一个接口的特定实例 界面如下所示: public interface IMyInterface { ... implementation goes here } public class MyClassA : IMyInterface { ... implementation goes here } public class MyClassB : IMyInterface { ... implementation goes here } public
public interface IMyInterface
{
... implementation goes here
}
public class MyClassA : IMyInterface
{
... implementation goes here
}
public class MyClassB : IMyInterface
{
... implementation goes here
}
public NewsController(INewsService newsService, IMapper newsMapper)
{
Check.Argument.IsNotNull(newsService, "newsService");
Check.Argument.IsNotNull(newsMapper, "newsMapper");
this.newsService = newsService;
this.newsMapper = newsMapper;
}
public class NewsMapper : IMapper
{
static NewsMapper()
{
Mapper.CreateMap<News, NewsEditViewData>();
Mapper.CreateMap<NewsEditViewData, News>();
}
public object Map(object source, Type sourceType, Type destinationType)
{
return Mapper.Map(source, sourceType, destinationType);
}
}
然后我有一些实现这个接口的类,如下所示:
public interface IMyInterface
{
... implementation goes here
}
public class MyClassA : IMyInterface
{
... implementation goes here
}
public class MyClassB : IMyInterface
{
... implementation goes here
}
public NewsController(INewsService newsService, IMapper newsMapper)
{
Check.Argument.IsNotNull(newsService, "newsService");
Check.Argument.IsNotNull(newsMapper, "newsMapper");
this.newsService = newsService;
this.newsMapper = newsMapper;
}
public class NewsMapper : IMapper
{
static NewsMapper()
{
Mapper.CreateMap<News, NewsEditViewData>();
Mapper.CreateMap<NewsEditViewData, News>();
}
public object Map(object source, Type sourceType, Type destinationType)
{
return Mapper.Map(source, sourceType, destinationType);
}
}
在我的ControllerA中,我有以下构造函数:
private ICustomerService customerService;
private IMyInterface myInterface;
puvlic ControllerA(ICustomerService customerService, IMyInterface myInterface)
{
this.customerService = customerService;
this.myInterface = myInterface;
}
在my global.ascx中:
protected void Application_Start()
{
// Autofac
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<NewsService>().As<INewsService>();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
IMapper接口:
public interface IMapper
{
object Map(object source, Type sourceType, Type destinationType);
}
我用的是AutoMapper。因此,我的新闻映射器将如下所示:
public interface IMyInterface
{
... implementation goes here
}
public class MyClassA : IMyInterface
{
... implementation goes here
}
public class MyClassB : IMyInterface
{
... implementation goes here
}
public NewsController(INewsService newsService, IMapper newsMapper)
{
Check.Argument.IsNotNull(newsService, "newsService");
Check.Argument.IsNotNull(newsMapper, "newsMapper");
this.newsService = newsService;
this.newsMapper = newsMapper;
}
public class NewsMapper : IMapper
{
static NewsMapper()
{
Mapper.CreateMap<News, NewsEditViewData>();
Mapper.CreateMap<NewsEditViewData, News>();
}
public object Map(object source, Type sourceType, Type destinationType)
{
return Mapper.Map(source, sourceType, destinationType);
}
}
公共类新闻映射器:IMapper
{
静态新闻映射器()
{
CreateMap();
CreateMap();
}
公共对象映射(对象源,类型sourceType,类型destinationType)
{
返回Mapper.Map(source、sourceType、destinationType);
}
}
那么你建议我现在怎么做呢?IIRC:
builder.RegisterType<MyClassB>().As<IMyInterface>()
它怎么知道这是给Controllerb的?ControllerA和ControllerB看起来是一样的,但它们只是在IMyInterface上得到了不同的实现。@jgauffin,我不同意。OP并没有说他的控制器不能处理其他类型的实例,他说他想注入不同类型的实例。让我们想象一下,他有一些提取数据的服务,他有一个包装这个数据服务并提供缓存功能的服务。我相信他们在这种情况下应该有相同的界面,注入正确的服务是DI的问题。@jgauffin+@Snowbear:请查看我的最新帖子,并请告诉我现在该怎么做?谢谢。@jgauffin:我要创建一个INewsMapper。谢谢你的建议。然后我需要为每个mapper?@jgauffin,+1为泛型解决方案创建一个接口,这与我使用的相同。但总的来说,我仍然相信,在某些情况下,您可能会为同一接口使用多个实现,并且希望在不创建标记接口的情况下注入它们。