Dela via


Free Code: Windsor Castle Autofactory Facility

So I recently was working on a system that used extensive need of delayed factory support for dependencies on our components. Effectively if ClassDepending needed to use ClassDepended, the constructor parameter was instead Func<ClassDepended>. This solution was built on Windsor container. Having to remember to register all those custom factories in our bootstrap routines was becoming a huge Pain In The Tuchus so I knocked out a quick and dirty facility (one of the core extensibility points for Winsdor) that will monitor registrations and will build a complementary factory delegate registration for the same component automagically! By default it will take the registration key of the core component and register a singleton instance of the Func<YourComponent> under the key originalkey_factory. And before you ask YES it is smart enough to not register factories for factories. Basically anything that is a delegate type is ignored. Try her out and see how you like her.

 

Basic Usage:

var myContainer = new WindsorContainer();
myContainer.Facilities<AutoFactoryFacility>();

// initialize your container registerations

var foo = myContainer.Resolve<IFoo>();
Debug.Assert(foo != null);

var fooFactory = myContainer.Resolve<Func<IFoo>>();
Debug.Assert(fooFactory != null);

var foo 2 = fooFactory();
Debug.Assert(foo2 != null); 

AutoFactoryFacility.cs