Windsor Castle Typified Factory Property Equivalents

Do any other .NET IoC containers provide equivalent functionality for a typed factory object in Castle Windsor?

eg. if I use the abstract factory template in a WPF application:

public class MyViewModel { private IAnotherViewModelFactory factory; public void ShowAnotherViewModel() { viewController.ShowView(factory.GetAnotherViewModel()); } } 

I do not want to create a manual implementation of IAnotherViewModelFactory for each type of ViewModel that I want to show, I want the container to take care of this for me.

+7
abstract-factory unity-container ninject castle-windsor autofac
source share
3 answers

AutoFac has a Delegate Factories function, but as far as I can tell, it only works with delegates, not with interfaces.

I haven't seen anything like the Castle Typed Factory Facility in either StructureMap or Unity, but that doesn't necessarily mean they aren't there ...


The only way to imagine that something like this can be implemented for interfaces is through a dynamic proxy. Since Castle Windsor has a dynamic proxy, but few other containers have something similar, this can go a long way to explain why this feature is not ubiquitous.

Unity also offers interception capabilities, so it should have some kind of dynamic proxy implementation, but I'm sure it has nothing equivalent to Typed Factories. Compared to other containers, Unity is pretty simple.

+6
source share

In Autofac, you can implement typed factories on top of the delegate approach that Mark mentions. For example.

 class AnotherViewModelFactory : IAnotherViewModelFactory { Func<AnotherViewModel> _factory; public AnotherViewModelFactory(Func<AnotherViewModel> factory) { _factory = factory; } public AnotherViewModel GetAnotherViewModel() { return _factory(); } } 

If this class is registered in the container, together with AnotherViewModel Autofac will provide the implementation of Func<AnotherViewModel> implicitly:

 builder.RegisterType<AnotherViewModel>(); builder.RegisterType<AnotherViewModelFactory>() .As<IAnotherViewModelFactory>(); 

Almost any interface that you can implement using the Typed Factory Facility can be implemented in Autofac using this approach. The main difference is that the Windsor implementation configures Factory through the component registration API, and in Autofac Factory it is a component of its choice.

For more complex examples, you can see: http://code.google.com/p/autofac/wiki/RelationshipTypes and http://nblumhardt.com/2010/01/the-relationship-zoo/ .

+3
source share

I recently implemented the equivalent of Castle Windsor Typed Factory for Unity. You can find the project https://github.com/PombeirP/Unity.TypedFactories , and the NuGet package at http://nuget.org/packages/Unity.TypedFactories .

The following is used:

 unityContainer .RegisterTypedFactory<IFooFactory>() .ForConcreteType<Foo>(); 

Coordination of parameters is done by name, which is great for my needs, although the library can be easily expanded to support other needs.

+1
source share

All Articles