unitycontainer / configuration

Unity.Configuration package
Apache License 2.0
13 stars 13 forks source link

Resolving 2 Objects of same type but different version #22

Open Ghislain1 opened 5 years ago

Ghislain1 commented 5 years ago

Description

We have a scenario where we can registry in config file 2 Singleton of the same type but different dll version. In the background we have 2dll classes (Only different version) which all implement the desired interface. We would like to use Unity to resolve 2 instances for this interface.

To Reproduce

  1. Create config file

    <unity>
    <aliases>
      <add alias="DtmParameter1" type="FullFrameworkLibLogic.DtmParameter,  FullFrameworkLibLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <add alias="DtmParameter2" type="FullFrameworkLibLogic.DtmParameter, FullFrameworkLibLogic, Version=5.5.5.5, Culture=neutral, PublicKeyToken=null" />
      <add alias="my" type="FullFrameworkLibLogic.DtmParameter, FullFrameworkLibLogic" />
    
    </aliases>
    <container>
    
      <register name="1" type="IDtmParameter" mapTo="DtmParameter1">
    <lifetime type="singleton" />
      </register>
    
      <register name="2" type="IDtmParameter" mapTo="DtmParameter2">
        <lifetime type="singleton" />
      </register>
    </container>
    </unity>
    </configuration>
  2. Test like this


[TestMethod]
public void SomeDescriptiveName()
{
        IDtmParameter dtmParameter1 = this.container.Resolve<IDtmParameter>("1");
            var msg1 = dtmParameter1.GetVersion();
            Console.WriteLine(msg1);

            IDtmParameter dtmParameter2 = this.container.Resolve<IDtmParameter>("2");
            var msg2 = dtmParameter2.GetVersion();
            Console.WriteLine(msg2);
}

image