I think everyone who has used Unity for inversion of control and dependency injection will be very happy to see the new InjectionFactory in Unity 2. Before when you wanted to register a factory ( function / delegate ) with Unity to create an object you had to use the StaticFactoryExtension. Using static factories was way behind the times in terms of what other containers, like Autofac, Windsor, Ninject, etc., could do using simple inline lambda expressions.
InjectionFactory in Unity 2
Microsoft Patterns & Practices has been working diligently on Unity 2 and Enterprise Library 5 and we are seeing all kinds of usability being added to these products. With InjectionFactory one can now use a simple lambda expression when specifying a factory for creating an object. Here is some code using Unity 2 and InjectionFactory to create an instance of IPaymentProcessor using a simple lambda expression:
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType<IPaymentProcessor>(
new InjectionFactory ((c) => new PaymentProcessor())
);
var processor = container.Resolve<IPaymentProcessor>();
}
}
public interface IPaymentProcessor { }
public class PaymentProcessor : IPaymentProcessor { }
I think we can all agree that this is much, much better than the previous StaticFactoryExtension. Of course, you can still specify a static method on a class as a factory to create the object:
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType<IPaymentProcessor>(
new InjectionFactory ((c) => PaymentProcessorFactory.Create())
);
var processor = container.Resolve<IPaymentProcessor>();
}
}
public static class PaymentProcessorFactory
{
public static IPaymentProcessor Create()
{
// Pretend Some Algortithm Here...
return new PaymentProcessor();
}
}
public interface IPaymentProcessor { }
public class PaymentProcessor : IPaymentProcessor { }
Note that you can also pass in the instance of the IUnityContainer into the factory method as well. In fact, you probably will want to use the UnityContainer to create PaymentProcessor as opposed to using the new keyword. Let’s change the code above to use Unity to resolve PaymentProcessor:
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType<IPaymentProcessor>(
new InjectionFactory ((c) => PaymentProcessorFactory.Create(c))
);
var processor = container.Resolve<IPaymentProcessor>();
}
}
public static class PaymentProcessorFactory
{
public static IPaymentProcessor Create(IUnityContainer container)
{
// Pretend Some Algortithm Here...
return container.Resolve<PaymentProcessor>();
}
}
public interface IPaymentProcessor { }
public class PaymentProcessor : IPaymentProcessor { }
Conclusion
Lots of interesting changes coming to Unity 2 and Enterprise 5. Hopefully they will be released around mid-to-late April along with Visual Studio 2010. Check my presentations for upcoming Enterprise Library and Unity Presentations in Florida. As soon as the code stabilizes I will create screencasts on Patterns & Practices Guidance.
Hope this helps.

Comments