如何根据传递到的服务解析接口 [英] How to resolve interface based on service where it's passed to

查看:19
本文介绍了如何根据传递到的服务解析接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面.

public interface ISomeInterface {...}

和两个实现(SomeImpl1和SomeImpl2):

and two implementations (SomeImpl1 and SomeImpl2):

public class SomeImpl1 : ISomeInterface {...}
public class SomeImpl2 : ISomeInterface {...}

我还有两个服务,我在其中注入了 ISomeInterface(通过构造函数):

I also have two services where I inject ISomeInterface (via contructor):

public class Service1 : IService1 
{
   public Service1(ISomeInterface someInterface)
   {
   }
...
}

public class Service2 : IService2 
{
   public Service2(ISomeInterface someInterface)
   {
   }
...
}

我使用 Autofac 作为我的 IoC 工具.问题.如何配置 Autofac 注册,以便 SomeImpl1 将自动注入 Service1,SomeImpl2 将自动注入 Service2.

I'm using Autofac as my IoC tool. The question. How can I configure Autofac registrations so SomeImpl1 will be automatically injected into Service1, and SomeImpl2 will be automatically injected into Service2.

谢谢!

推荐答案

Autofac 支持 按名称识别服务.使用它,您可以使用名称注册您的实现(使用 Named 扩展方法).然后,您可以使用 ResolveNamed 扩展方法在 IServiceX 注册委托中按名称解析它们.下面的代码演示了这一点.

Autofac supports identification of services by name. Using this, you can register your implementations with a name (using the Named extension method). You can then resolve them by name in the IServiceX registration delegates, using the ResolveNamed extension method. The following code demonstrates this.

var cb = new ContainerBuilder();
cb.Register(c => new SomeImpl1()).Named<ISomeInterface>("impl1");
cb.Register(c => new SomeImpl2()).Named<ISomeInterface>("impl2");
cb.Register(c => new Service1(c.ResolveNamed<ISomeInterface>("impl1"))).As<IService1>();
cb.Register(c => new Service2(c.ResolveNamed<ISomeInterface>("impl2"))).As<IService2>();
var container = cb.Build();

var s1 = container.Resolve<IService1>();//Contains impl1
var s2 = container.Resolve<IService2>();//Contains impl2

替代使用 RegisterType(相对于 Register)

Alternative using RegisterType (as opposed to Register)

您可以将 RegisterType 扩展方法与 WithParameterResolvedParameter 结合使用来获得相同的结果.如果采用命名参数的构造函数也采用您不想在注册委托中指定的其他非命名参数,这将非常有用:

You can achieve the same result using the RegisterType extension method in combination with WithParameter and ResolvedParameter. This is useful if the constructor taking a named parameter also takes other non-named parameters that you don't care to specify in a registration delegate:

var cb = new ContainerBuilder();
cb.RegisterType<SomeImpl1>().Named<ISomeInterface>("impl1");
cb.RegisterType<SomeImpl2>().Named<ISomeInterface>("impl2");
cb.RegisterType<Service1>().As<IService1>().WithParameter(ResolvedParameter.ForNamed<ISomeInterface>("impl1"));
cb.RegisterType<Service2>().As<IService2>().WithParameter(ResolvedParameter.ForNamed<ISomeInterface>("impl2"));
var container = cb.Build();

var s1 = container.Resolve<IService1>();//Contains impl1
var s2 = container.Resolve<IService2>();//Contains impl2

这篇关于如何根据传递到的服务解析接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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