Ask*_*uch 1 ioc-container light-inject
我有interface两个实现。
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) {}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用此代码,但不起作用。
ServiceContainer service = new ServiceContainer();
service.Register<ILogger, FileLogger>();
service.Register<ILogger, SQLiteLogger>();
Run Code Online (Sandbox Code Playgroud)
LightInject将省略第一次注册,只注册SQLiteLogger。那么如何在多个实现中注册相同的接口LightInject呢?
这称为命名服务——这是我见过的大多数 IOC/DI 容器的标准(参见命名服务部分)。
该文档提供了您需要的所有详细信息,但这是他们网站上的剪切和粘贴:
container.Register<IFoo, Foo>();
container.Register<IFoo, AnotherFoo>("AnotherFoo");
var instance = container.GetInstance<IFoo>("AnotherFoo");
Assert.IsInstanceOfType(instance, typeof(AnotherFoo));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1150 次 |
| 最近记录: |