Search code examples
castle-windsor

Castle Windsor concrete type resolving and property injection


I'm working with WPF. I have several view models implementing a interface called IPageView.

The MainViewModel also implements IPageView and as 2 properties LiveView and EditView.

public MainViewModel : IPageView
{
    public LiveViewModel LiveView{ get; set; }
    public EditViewModel EditView{ get; set; }
}

I register components implementing IPageView like this:

Classes.FromThisAssembly().BasedOn<IPageView>().WithServiceBase();

And i resolve all IPageView instances like this:

_container.ResolveAll<IPageView>();

I'd expect CastleWindsor to inject a instance of LiveViewModel to LiveView property, and a instance of EditViewModel to EditView.

This is not happening. Properties are null.

I don't like the idea to create specific new interfaces for each of my viewmodel just to correctly register and resolve the components.

In facts, I know i won't ever have more than one and only one viewmodel of type 'ILiveView' or 'IEditView' to choose from, hence i want to use the concrete implementations.

How can i configure the container to resolve my view models in MainViewModel? Thank you.


Solution

  • Register like this:

    Classes.FromThisAssembly().BasedOn<IPageView>()
        .WithService.Base()
        .WithService.Self();