37
votes

Does Castle Windsor permit registration of an open generic interface or do I need to register each possible typed instance separately?

Example - the below with types T,Z fails upon compilation unless I separately specify T, Z with strong types.

 container.Register(Component
      .For<IAdapterFactory<T,Z>>()
      .ImplementedBy<AdapterFactory<T,Z>>()
      .LifeStyle.PerWebRequest);
1
"Strong types" not an accurate description since templating does use strong types in C#. My point is that Castle Windsor does not seem to accept templates for registration, so it would seem I need to enumerate all possible types within ConstrollerInstaller.cs in order to register the same IAdapterFactory against multiple possible typed invokations. Seems strange.goldfinger
it's not Windsor's limitation, it's how .NET runtime works.Krzysztof Kozmic
Can you say more about this? "it's not Windsor's limitation, it's how .NET runtime works"goldfinger
You cannot close a generic method (like Component.For<>) over a non-closed generic type. This is how .NET generics work. Have a look here for some more insight msdn.microsoft.com/en-us/library/b8ytshk6.aspxKrzysztof Kozmic

1 Answers

75
votes

It's called open generic, and yes, Windsor does support that.

 container.Register(Component
             .For(typeof(IAdapterFactory<,>))
             .ImplementedBy(typeof(AdapterFactory<,>))
             .LifestylePerWebRequest());