2
votes

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...

1
Hello Jana, In generally I tend to use only default constructors for views. It seems you are using the constructor to bind the view and the viewModel. If you are using WPF/Silverlight you might want to consider using Caliburn.Micro (or an other library) for this. For me this simplified the design alot.Marwijn
Yes I am using the constructor to bind the View and ViewModel. I haven't decided on an MVVM framework yet and I'm pushing that off for a couple weeks while I figure out what all I can get out of Windsor. But thanks for reminding me that I don't have to solve everything with one framework! :)Jana Mandic

1 Answers

1
votes

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.