ParameterizedAutoFactory.Unity5 0.0.12

dotnet add package ParameterizedAutoFactory.Unity5 --version 0.0.12
NuGet\Install-Package ParameterizedAutoFactory.Unity5 -Version 0.0.12
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="ParameterizedAutoFactory.Unity5" Version="0.0.12" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ParameterizedAutoFactory.Unity5 --version 0.0.12
#r "nuget: ParameterizedAutoFactory.Unity5, 0.0.12"
#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 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 type IDependencyOfDependency.

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!

Product 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. 
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
0.0.12 307 1/24/2024
0.0.11 1,467 8/25/2020
0.0.10 1,239 8/25/2020
0.0.8 1,706 1/16/2019
0.0.7 1,532 11/15/2018
0.0.6 1,712 7/6/2018
0.0.5 1,829 6/7/2018
0.0.4 1,809 6/2/2018
0.0.3 1,863 6/1/2018
0.0.2 1,876 5/30/2018
0.0.1 1,855 5/30/2018

Make sure we're compatible with Unity >= 5.11.10