views:

55

answers:

0

I have a ViewModelLocator class that I am defining in app.xaml which is used by my views to databind to the correct ViewModel.

DataContext="{Binding HomeViewModel, Source={StaticResource Locator}}"

I am using Prism and Unity and my ViewModelLocator class needs a reference to an application level unity container.

I wanted inject the IUnityContainer into the ctor of the ViewModelLocator but the ViewModelLocator gets instanciated from app.xaml using a parameterless ctor.

Is there a preferred way to get access to an application level container - for all other classes in the app I just use ctor injection to get the global container.

What I am currenlty doing for the ViewModelLocator is defining a static variable in my BootStrapper class to store the container. I create the container by overriding the CreateContainer method on UnityBootStrapper.

protected override IUnityContainer CreateContainer()
{
    BootStrapper.DIContainer = base.CreateContainer();
    return BootStrapper.DIContainer;
}

Then in the ViewModelLocator class I just reference the BootStrapper.DIContainer property to register my viewmodels

BootStrapper.DIContainer.RegisterType<IShellViewModel, DesignShellViewModel>();

This works fine but it is the only place in the application that needs to reference this static property on the bootstrapper - and would like to get rid of it if possible.

thanks Michael