我有一个使用 Unity 的场景,这有点令人困惑......假设我有一个 AuthenticationService 对用户进行身份验证,并且当成功登录到文本文件或数据库时,分别使用 TextLogger 类或 DbLogger 类。通常,对于我将在我的项目中使用的任何类,我会在我的模块类中适当地注册,如下所示:
public class LoggingModule
{
IUnityContainer _iocContainer;
public LoggingModule(IUnityContainer container)
{
_iocContainer = container;
}
public void Init()
{
//Add any logic here to look in a config file, check a property
//or any other condition to decide which implementation is registered.
//register the database logger to the ILogger interface
_iocContainer.RegisterType(typeof(ILogger), typeof(DBLogger));
}
}
这将被注入到我的身份验证服务的构造函数中。但是,如果我想在我的应用程序的不同点使用这两个记录器,首先我要在我的 Init 方法中注册这两种类型,即 TextLogger 和 DBLogger?其次,我的容器如何知道要解析哪种类型?
请帮忙....