使用Unity XML配置文件在构造函数中注入复杂对象

时间:2017-10-10 12:12:41

标签: c# xml unity-container

我有以下类声明: 记录器服务,FacadeReaderService和BusinessManager。我想使用Unity XML配置在BusinessManager中注入Logger Service和FacadeReaderService。

记录器服务

public class LoggerService : ILoggerService
{

}

FacadeReaderService

public class FacadeReaderService : IFacadeReaderService
{
}

BusinessManager

public class BusinessManager : IBal
    {
        IFacadeReaderService _facadeReaderService;
        ILoggerService _loggerService;

        public BusinessManager(IFacadeReaderService facadeReaderService, ILoggerService loggerService)
        {
            this._facadeReaderService = facadeReaderService;
            this._loggerService = loggerService;
        }
    }

我的问题是如何在我的BusinessManager类中注入这些复杂的对象?以下是我目前在Unity Config文件中所做的工作:

<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>

1 个答案:

答案 0 :(得分:2)

好吧,我通过查看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>