注入使用Autofac接口的特定实例 [英] Injecting a specific instance of an interface using Autofac

查看:747
本文介绍了注入使用Autofac接口的特定实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器和接收接口的特定实例

I have a controller and it receives a specific instance of an interface.

界面看起来是这样的:

public interface IMyInterface
{
   ... implementation goes here
}

然后,我有一个实现这样这个接口的一些类:

And then I have some classes that implement this interface like this:

public class MyClassA : IMyInterface
{
   ... implementation goes here
}

public class MyClassB : IMyInterface
{
   ... implementation goes here
}

在我ControllerA我有下面的构造:

In my ControllerA I have the following constructor:

private ICustomerService customerService;
private IMyInterface myInterface;

puvlic ControllerA(ICustomerService customerService, IMyInterface myInterface)
{
   this.customerService = customerService;
   this.myInterface = myInterface;
}

在我global.ascx:

In 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));
}



我指定Autofac必须提供ICustomerService的实例。我怎么会指定IMyInterface的实例类型?在这种情况下ControllerA我想Autofac注入一个ClassA的实例。而对于ControllerB我想它注入ClassB的。我将如何做到这一点。

I specified that Autofac must supply the instance of ICustomerService. How would I specify the instance type for IMyInterface? In this case for ControllerA I would like Autofac to inject a ClassA instance. And for ControllerB I would like it to inject ClassB. How would I do this?

已更新2011-02-14:

让我给你我的真实生活状况。我有一个NewsController它的构造是这样的:

Let me give you my real life situation. I have a NewsController whose constructor looks like this:

public NewsController(INewsService newsService, IMapper newsMapper)
{
   Check.Argument.IsNotNull(newsService, "newsService");
   Check.Argument.IsNotNull(newsMapper, "newsMapper");

   this.newsService = newsService;
   this.newsMapper = newsMapper;
}



IMapper接口:

IMapper interface:

public interface IMapper
{
   object Map(object source, Type sourceType, Type destinationType);
}



我使用AutoMapper。所以我的NewsMapper看起来是这样的:

I'm using AutoMapper. So my NewsMapper will look like this:

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



所以,你会怎么推荐我现在做到这一点?

So how would you recommend me do this now?

推荐答案

IIRC:

builder.RegisterType<MyClassB>().As<IMyInterface>()



更新



确定。误解你的问题。

Update

Ok. Misread your question.

其实,你永远也不会做你的要求。它势必会给你带来麻烦。为什么?因为没有方法来确定,该控制器不能与相同的接口工作。除其他外,你打破了Liskovs替代原则。它可能不是你的问题了,而是让你的应用程序增长,回来一年,并试图理解为什么它不工作。

Actually, you should never ever do what you are asking. It's bound to give you problems. Why? Since there is no way to determine that the controllers can not work with the same interface. Amongst others, you are breaking the Liskovs Substitution Principle. It might not be a problem for you now, but let your application grow and come back in a year and try to understand why it isn't working.

相反,创建两个新的接口,从`IMyInterface'派生,并要求那些在控制器中。

Instead, create two new interfaces which derive from `IMyInterface´ and request those in the controllers.

Qouting snowbear

Qouting snowbear:

我不同意。 OP不说,他的
控制器无法与其他类型的实例
的工作,他说,他希望
注入不同的
类型的实例。让我们想象一下他有一些
服务来提取数据,他有它封装此数据服务的
,并提供缓存功能的
服务。我
相信他们应该有这样的情况同样
接口,它是DI的
物质注入正确的服务

I disagree. OP doesn't says that his controller cannot work with instance of another type, he says that he wants to inject instances of different types. Let's imagine he has some service to extract data and he has a service which wraps this data service and provides caching functionality. I believe they should have the same interface in such situation and it is matter of DI to inject correct service

OP说就是:?控制器A不能工作同一类控制器B.他为什么要别人想在不同的控制器不同类别相同的接口

OP says just that: Controller A cannot work same class as controller B. Why would he else want to get different classes in different controllers for the same interface?

布伦丹:

我个人将创建一个 INewsMapper 界面让一切清晰的声音。但是,如果你不想这样做:去与聚合根作为类型参数的通用接口

I personally would create a INewsMapper interface to make everything clear and sound. But if you do not want to do that: Go for a generic interface with the aggregate root as type parameter.

public interface IMapper<T> : IMapper where T : class
{

}

public class NewsMapper : IMapper<News>
{
   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 NewsController(INewsService newsService, IMapper<News> newsMapper)
{
}

这篇关于注入使用Autofac接口的特定实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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