UnityInjectionLookup 1.0.0
See the version list below for details.
dotnet add package UnityInjectionLookup --version 1.0.0
NuGet\Install-Package UnityInjectionLookup -Version 1.0.0
<PackageReference Include="UnityInjectionLookup" Version="1.0.0" />
paket add UnityInjectionLookup --version 1.0.0
#r "nuget: UnityInjectionLookup, 1.0.0"
// 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 | Versions 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. |
-
- Unity (>= 4.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
First one.