Can Castle.Windsor TypedFactoryFacility build a type containing only some ref arguments passed inline?

I ran into a problem using the Castle.Windsor TypedFactoryFacility concept and am not sure how to resolve it (or, if possible).

I have a ViewModel defined like this:

public class MyViewModel : IMyViewModel { // constructor which I aim to access through the factory public MyViewModel( ISomeContainerResolvableDependency a, ISomePassedInDependency b) { ... } } 

And the corresponding factory interface, defined as;

 public interface IMyViewModelFactory { IMyViewModel Create(ISomePassedInDependency a); void Release(IMyViewModel vm); } 

However, when I try to call factory with a valid, non-null instance of ISomePassedInDependency as follows:

 ISomePassedInDependency o = new SomePassedInDependency(); IMyViewModelFactory factory = IoC.Get<IMyViewModelFactory>(); IMyViewModel viewModel = factory.Create(o); 

I get an exception stating that Castle Windsor was unable to resolve the dependency on IMyViewModel on ISomePassedInDependency . In this case, I supply an instance of ISomePassedInDependency and want the object to allow me ISomeContainerResolvableDependency .

If I change the definition of the constructor to accept the type of value that I pass (for example, t28>) instead of the ISomePassedInDependency instance, the factory call works. So, it seems that inside Windsor Castle there is an assumption that all reference types will be resolved by the container, but something else will not?

Is there any way to make this work?

+3
c # castle-windsor factory
source share
1 answer

OK, I re-read the documentation and noticed a key piece of information that I missed; The argument names in the factory must match the argument names in the object instance.

So, in my example above, if the ViewModel constructor is changed to;

 public MyViewModel( ISomeContainerResolvableDependency x, ISomePassedInDependency a) // <-- NOTE this argument is now named 'a' to match the factory definition { ... } 

He works.

Magic.

+5
source share

All Articles