views:

32

answers:

1

I need to register open generic of a type in my Windsor Container. How does this thing works?

container.Register(Component.For<IRepository<??>>().UsingFactoryMethod(x => x.Resolve<IDataContext>().GetRepository<??>()));

Basically, I want to register IRepository as open generic and then create the repository from the IDataContext based on the passed generic type. Is this supported?

A: 

When registering open generic use non-generic method:

Component.For(typeof(IRepository<>))...

When resolving via UsingFactoryMethod you can have access to the type that is requested, so you can pass the type to your GetRepository and resolve the repository based on the type (System.Type object) you pass.

As a sidenote I must say I find your design rather unusual. It's more common that a repository holds a reference to data context, not the other way around. Perhaps you'd want to take another look at the design instead?

Krzysztof Koźmic
Thanks for your answer...I know about the design, the problem is I'm working with a legacy API which things seemingly are the other way around!
Hadi Eskandari
well then I'm afraid unless you have a non-generic overload of `GetRepository` you're in for `methodInfo.MakeGenericMethod().Invoke()`
Krzysztof Koźmic