Unity resolved by name

I have the following constructor for the Service class:

public class Service : IService
{
    private readonly IRepository _repository;

    public Service(IRepository repository)
    {
        _repository = repository;
    }

    // ...
}

IRepository has two named implementations. I want to enable IService, but you need to establish which IRepository implementation should be used (the service must be flexible, and I cannot put the Dependency attribute in the IRepository constructor parameter).

Is there any way to implement its Unity?

+3
source share
1 answer

To achieve this, you can use the unit configuration section. Check this link.

In the configuration section, you can specify the display as follows.

 <type type="IMyService" mapTo="MyDataService" name="DataService">
      <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                 Microsoft.Practices.Unity.Configuration">
        <constructor>
          <param name="connectionString" parameterType="string">
            <value value="AdventureWorks"/>
          </param>
          <param name="logger" parameterType="ILogger">
            <dependency />
          </param>
        </constructor> 
        <property name="Logger" propertyType="ILogger" />
        <method name="Initialize">
          <param name="connectionString" parameterType="string">
            <value value="contoso"/>
          </param>
          <param name="dataService" parameterType="IMyService">
            <dependency />
          </param>
        </method>
      </typeConfig>
    </type>
+3
source

Source: https://habr.com/ru/post/1782004/


All Articles