Question

I have views with constructors like this:

MyView(MyViewModel viewModel)

and viewmodels with constructors like this:

MyViewModel(RuntimeParameter runtimeParam, <design-time resolvable parameters>)

I would like to create a View Factory using Castle Windsor's Typed Factory Facility feature so that I can create Views by calling

MyView view = factory.Create(runtimeParam);

I've been able to make a ViewModel factory using the Castle Windsor documentation here, but I can't make the View Factory work. The run-time parameter passed to the View factory needs to be handed down to the ViewModel. Is there a way to tell Windsor to do that?

I suppose I could use two factories - one for the view and one for the viewmodel, but that seems ugly...

Was it helpful?

Solution

It is not possible to pass parameters through multiple layers of typed factories in the way you are attempting. I think your best bet is to have your View depend on your ViewModel typed factory, and request the ViewModel from the factory in its constructor (which can be invoked from another typed factory for Views, if you like).

As an alternative, consider whether your ViewModels really need to be resolved via a typed factory. Can that RuntimeParameter be fetched from some service that is registered with the container? Generally, I try to avoid typed factories if I can find a way to make the "default" behavior work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top