[英]Unity: pass parameters to custom lifetime constructor, in xml configuration file
[英]Inject complex objects in constructor with Unity XML configuration file
我有以下类声明:Logger Service、FacadeReaderService 和 BusinessManager。 我想使用 Unity XML 配置在 BusinessManager 中注入 Logger Service 和 FacadeReaderService。
记录器服务
public class LoggerService : ILoggerService
{
}
门面阅读器服务
public class FacadeReaderService : IFacadeReaderService
{
}
业务经理
public class BusinessManager : IBal
{
IFacadeReaderService _facadeReaderService;
ILoggerService _loggerService;
public BusinessManager(IFacadeReaderService facadeReaderService, ILoggerService loggerService)
{
this._facadeReaderService = facadeReaderService;
this._loggerService = loggerService;
}
}
我的问题是如何在我的 BusinessManager 类中注入这个复杂的对象? 以下是我到目前为止在我的 Unity 配置文件中所做的:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<typeAliases>
<typeAlias alias="IFacadeReaderService" type="Interfaces.Services.IFacadeReaderService, Interfaces" />
<typeAlias alias="FacadeReaderService" type="Services.FacadeReader.FacadeReaderService, Services" />
<typeAlias alias="ILoggerService" type="Interfaces.Services.ILoggerService, Interfaces" />
<typeAlias alias="LoggerService" type="Services.Log.LoggerService, Services" />
<typeAlias alias="IBal" type="Interfaces.Bal.IBal, Interfaces" />
<typeAlias alias="BusinessManager" type="Bal.BusinessManager, Bal" />
</typeAliases>
<container>
<register type="IFacadeReaderService" mapTo="FacadeReaderService" name="FRS"/>
<register type="ILoggerService" mapTo="LoggerService" name="LS"/>
<register type="IBal" mapTo="BusinessManager" name="BMS">
<constructor>
<param name="facadeReaderService" value="????????" />
<param name="loggerService" value="??????" />
</constructor>
</register>
</container>
</unity>
好吧,我通过查看 Microsoft 文档找到了我的问题的答案: https : //msdn.microsoft.com/en-us/library/ff660914(v=pandp.20).aspx#config_value
我将发布统一配置,以防万一有人需要它:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<typeAliases>
<typeAlias alias="IFacadeReaderService" type="Interfaces.Services.IFacadeReaderService, Interfaces" />
<typeAlias alias="FacadeReaderService" type="Services.FacadeReader.FacadeReaderService, Services" />
<typeAlias alias="ILoggerService" type="Interfaces.Services.ILoggerService, Interfaces" />
<typeAlias alias="LoggerService" type="Services.Log.LoggerService, Services" />
<typeAlias alias="IBal" type="Interfaces.Bal.IBal, Interfaces" />
<typeAlias alias="BusinessManager" type="Bal.BusinessManager, Bal" />
</typeAliases>
<container>
<register type="IFacadeReaderService" mapTo="FacadeReaderService" name="FRS"/>
<register type="ILoggerService" mapTo="LoggerService" name="LS"/>
<register type="IBal" mapTo="BusinessManager" name="BMS">
<constructor>
<param name="facadeReaderService">
<dependency name="FRS" />
</param>
<param name="loggerService">
<dependency name="LS" />
</param>
</constructor>
</register>
</container>
</unity>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.