comment 0

Typed Factories a la Castle Windsor for Unity

I recently finished an implementation of the equivalent of Castle Windsor’s Typed Factories facility, for Unity 2.

I decided on the creative name of Unity.TypedFactories :-). As far as I know, there isn’t anything like it out there for Unity, and since I didn’t feel like creating and testing dozens of small factories, I decided to create a simple implementation of Typed Factories. All you need is to declare the factory’s interface:

public interface IFooFactory
    #region Public Methods and Operators

    IFoo Create(int id);


And register it with Unity, through my library’s extension methods:

public void Main()
    var unityContainer = new UnityContainer();

Upon resolution of the factory interface, Unity.TypedFactories will use Castle.Core to generate a proxy for your interface and inject code into each method of the interface. That code will try to match the factory methods argument names to the concrete type’s argument names and rely upon Unity to do the heavy lifting of instantiating the object.

The source code is as always on my GitHub page, and the NuGet package is published here. Enjoy!