UnityInjectionLookup 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package UnityInjectionLookup --version 1.0.0
NuGet\Install-Package UnityInjectionLookup -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UnityInjectionLookup" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UnityInjectionLookup --version 1.0.0
#r "nuget: UnityInjectionLookup, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UnityInjectionLookup as a Cake Addin
#addin nuget:?package=UnityInjectionLookup&version=1.0.0

// Install UnityInjectionLookup as a Cake Tool
#tool nuget:?package=UnityInjectionLookup&version=1.0.0

The package defines a custom Unity's InjectionMember - InjectionCOnstructorLookup. It resolves a provided type between several inheritors of some basis type, depending on their constructor parameter signatures.

Each inheritor should have a constructor with a certain signature - each parameter should be inherited from a type passed to InjectionCOnstructorLookup's constructor.

Here is an example. Suppose we have following types:

   interface IConfig { ... }
   interface IReport { ... }

   class ConfigA : IConfig { ... }
   class ConfigB : IConfig { ... }

   class ReportA : IReport
   {
       public ReportA (ConfigA config)
       { ... }
   }

   class ReportB : IReport
   {
       public ReportB (ConfigB config)
       { ... }
   }

The problem is - we have a IConfig as an input and we want to decide which report to create.
Suppose then, we have registered both reports:

   var unityContainer = new UnityContainer();
   
   unityContainer.RegisterType<IReport, ReportA>("ReportA");
   unityContainer.RegisterType<IReport, ReportB>("ReportB");

Do make things work we just add another registration of `IReport` with the `InjectionMember`:

   unityContainer.RegisterType<IReport>(new InjectionConstructorLookup<IConfig>());

Here `IConfig` type parameters says that we'll look for `IReport`-inherited objects with a single constructor argument, inherited from `IConfig`.

Then there are two ways of resolving `IReport`.

   var reportA = unityContainer.Resolve<IReport>(new DependencyOverride<IConfig>(new ConfigA())); // Should be ReportA
   var reportB = unityContainer.Resolve<IReport>(new DependencyOverride<IConfig>(new ConfigB())); // Should be ReportB

The second one is a delegate factory:

   var factory = unityContainer.Resolve<Func<IConfig, IReport>>();
   
   var reportA1 = factory(new ConfigA()); // Should be ReportA
   var reportB1 = factory(new ConfigB()); // Should be ReportB

Call to the factory is faster due to result caching.

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 1,901 11/22/2018
1.0.0 1,597 7/19/2016

First one.