Unity IOC container and how to resolve different instances of the same interface

后端 未结 1 664
孤城傲影
孤城傲影 2021-01-01 08:23

I have a unity container that I am registering types within like so:

    IUnityContainer container = new UnityContainer()
.RegisterType

        
相关标签:
1条回答
  • 2021-01-01 08:43

    Ok I figured it out. Keeping the names the same during registration is correct ("PopulationRate" and "BusinessLicenseRate"). All I had to do was add an attribute to the ITaxAuthorityRateService parameter within the constructor of each service like so:

    Service1 constructor parameter:

    [Dependency( "BusinessLicenseRate" )]
    ITaxAuthorityRateService rateService
    

    Service2 constructor parameter:

    [Dependency( "PopulationRate" )]
    ITaxAuthorityRateService rateService
    

    And then each service received the correct ITaxAuthorityRateService instance.

    0 讨论(0)
提交回复
热议问题