Search code examples
c#xmlunity-container

Inject complex objects in constructor with Unity XML configuration file


I have the following classes declaration : Logger Service,FacadeReaderService and BusinessManager. I want to inject Logger Service and FacadeReaderService in BusinessManager using Unity XML configuration.

Logger Service

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

My question is how to inject this complex objects in my BusinessManager class ? Below is what I have done so far in my Unity Config file :

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

Solution

  • Well, I found the answer to my question by looking in Microsoft documentation : https://msdn.microsoft.com/en-us/library/ff660914(v=pandp.20).aspx#config_value

    I will post the unity config, just in case there is someone who will need it :

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