ParameterizedAutoFactory.Unity5
0.0.12
dotnet add package ParameterizedAutoFactory.Unity5 --version 0.0.12
NuGet\Install-Package ParameterizedAutoFactory.Unity5 -Version 0.0.12
<PackageReference Include="ParameterizedAutoFactory.Unity5" Version="0.0.12" />
paket add ParameterizedAutoFactory.Unity5 --version 0.0.12
#r "nuget: ParameterizedAutoFactory.Unity5, 0.0.12"
// Install ParameterizedAutoFactory.Unity5 as a Cake Addin #addin nuget:?package=ParameterizedAutoFactory.Unity5&version=0.0.12 // Install ParameterizedAutoFactory.Unity5 as a Cake Tool #tool nuget:?package=ParameterizedAutoFactory.Unity5&version=0.0.12
Parameterized Auto Factory for the Unity IoC Container
A UnityContainer extension inspired by Autofac's Parameterized Instantiation.
What is it for?
It lets you supply certain parameters of the target type's constructor by passing them to the auto-generated factory. While taking advantage of the container to resolve and inject all the other parameters that you don't want to supply manually.
In other words. Unity generates a factory Func
for constructor parameters of the form Func<IDependency>
.
public class TargetType
{
// Unity will generate createDependency for us.
public TargetType(Func<IDependency> createDependency) { /* ... */ }
}
This extension takes it one step further. It automatically uses parameter overrides when
- it sees a dependency of the form
Func<IDependencyOfDependency, IDependency>
- and the concrete class implementing
IDependency
needs a parameter of typeIDependencyOfDependency
.
This only kicks in if Func<IDependencyOfDependency, IDependency>
is not explicitely registered.
Here is a quick example presented as an xUnit test.
public interface IDialogBoxService { /* ... */ }
public interface IUserListDataSource { /* ... */ }
public class UsersGridWindow
{
public UsersGridWindow(string windowTitle,
IUserListDataSource userListDataSource,
IDialogBoxService dialogBoxService)
{
WindowTitle = windowTitle;
UserListDataSource = userListDataSource;
DialogBoxService = dialogBoxService;
/* ... */
}
public string WindowTitle { get; }
public IUserListDataSource UserListDataSource { get; }
public IDialogBoxService DialogBoxService { get; }
/* ... */
}
public class CachedUserListDataSource : IUserListDataSource
{
public void WarmUp() { /* ... */ }
}
[Fact]
public void Factory_parameters_overrides_matching_constructor_parameters()
{
// Setup the container
var container = new UnityContainer()
.AddNewExtension<UnityParameterizedAutoFactoryExtension>();
// Here the extension kicks in and generates
// a parameterized factory of type Func<string, IUserListDataSource, UsersGridWindow>.
// Of course, in a real app Func<string, IUserListDataSource, UsersGridWindow> would
// likely have been a constructor parameter.
var createUsersGridWindow = container
.Resolve<Func<string, IUserListDataSource, UsersGridWindow>>();
// Now, let's try to show a scenario which illustrates why
// a parameterized auto-factory can be useful.
// Create and warm up a cached data source.
// We can re-use the warmed up cache for any number of UsersGridWindow instances
// or other windows which depend on IUserListDataSource.
var cachedUserListDataSource = new CachedUserListDataSource();
cachedUserListDataSource.WarmUp();
// Pick window title for this particular window instance.
// We can pick another title for another window instance.
const string windowTitle = "Registered users";
// Create the window.
var usersGridWindow = createUsersGridWindow(windowTitle, cachedUserListDataSource);
// Let's make sure the parameters were overridden as expected.
Assert.Equal(usersGridWindow.WindowTitle, windowTitle); // We overrode this one.
Assert.Same(usersGridWindow.UserListDataSource, cachedUserListDataSource); // And this one too.
Assert.NotNull(usersGridWindow.DialogBoxService); // We didn't override DialogBoxService,
// and so it was resolved from the container.
}
Download and install
To install it run the following command in the NuGet Package Manager Console.
Install-Package ParameterizedAutoFactory.Unity5
This will download all the binaries, and add necessary references to your project.
How to use it?
One way to register the extension in container is to use the Unity.UnityContainerExtensions.AddNewExtension
extension method.
var container = new UnityContainer()
.AddNewExtension<UnityParameterizedAutoFactoryExtension>();
A call to Unity.UnityContainerExtensions.AddNewExtension
parameterized by UnityParameterizedAutoFactoryExtension
is approximately equivalent to executing the snippet below.
var extension = (UnityParameterizedAutoFactoryExtension)container.Resolve(
typeof(UnityParameterizedAutoFactoryExtension));
container.AddExtension(extension);
Thank you!
- This extension is inspired by Autofac's Parameterized Instantiation.
- UnityAutoMoq gave a great example of extension hooking into Unity's dependencies instantiation/resolution pipeline.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.5 is compatible. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
- Unity.Abstractions (>= 5.11.1)
- Unity.Container (>= 5.11.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Make sure we're compatible with Unity >= 5.11.10