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

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

问题描述

我有一个接口。

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将自动注入到服务1,和SomeImpl2将自动注入到服务2。

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支持服务的鉴定名称。利用这一点,你可以用一个名称(使用命名扩展方法),注册您的实现。然后,您可以按名称IServiceX注册代表解决这些问题,使用 ResolveNamed 扩展方法。 。下面的代码演示了这种

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 (而不是注册

Alternative using RegisterType (as opposed to Register)

您可以使用达到相同的结果 RegisterType 组合扩展方法以 WithParameter ResolvedParameter 。这是有用的,如果构造采取命名的参数也需要你不小心在注册委托指定其他非命名参数:

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天全站免登陆