在LightInject IoC中注册多接口实现 [英] Register Multiple Interface Implementation In LightInject IoC

查看:125
本文介绍了在LightInject IoC中注册多接口实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两个实现的interface.

I have an interface with two implementations.

public interface ILogger
{
   void Log(string message);
}

public class FileLogger : ILogger
{
   public void Log(string message) {}
}

public class SQLiteLogger : ILogger
{
  public void Log(string message) {}
}

我尝试使用此代码,但不起作用.

I try using this code, but doesn't work.

ServiceContainer service = new ServiceContainer();
service.Register<ILogger, FileLogger>();
service.Register<ILogger, SQLiteLogger>();

LightInject将省略第一个注册,而仅注册SQLiteLogger. 那么如何在LightInject中为多个实现注册同一接口?

LightInject will omit the first registration and only register SQLiteLogger. So how to register same interface with multiple implementation in LightInject ?

推荐答案

这称为命名服务-这是我所见过的大多数IOC/DI容器的标准配置(请参阅有关命名服务的部分).

This is called Named Services - which is quite standard across most IOC/DI containers that I've seen (see section on named services).

文档提供了您需要的所有详细信息,但这是从其网站上粘贴下来的内容:

The documentation provides all the detail you need but here is a cut-and-paste from their website:

container.Register<IFoo, Foo>();
container.Register<IFoo, AnotherFoo>("AnotherFoo");
var instance = container.GetInstance<IFoo>("AnotherFoo");
Assert.IsInstanceOfType(instance, typeof(AnotherFoo));

这篇关于在LightInject IoC中注册多接口实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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