1

when I do MVVM Applications I usually have a central ViewModel locator which works with the service locator pattern. This allows me to inject ViewModel with Services via Ninject

My ViewModel locator usually looks like this:

public class ViewModelLocator
{
    private static IKernel kernel;

    public ViewModelLocator()
    {
        if (kernel == null)
        {
            kernel = new StandardKernel(new ConfigModule());
        }
    }

    public static T Get<T>()
    {
        return kernel.Get<T>();
    }


    public static ProductViewModel ProductViewModel{

        get { return kernel.Get<ProductViewModel>(); }

    }

    public UserViewModel UserViewModel {
        get { return kernel.Get<UserViewModel>();}
    }
}

public class ConfigModule : NinjectModule
{
    public override void Load()
    {

        Bind<ProductViewModel>().ToSelf();
        Bind<UserViewModel>().ToSelf();

    }
}

Right now I am thinking on adding another Module which is called "Orders". So I will have an OrderViewModel (or in real live a couple of them). And I want to have them seperated and imported via MEF.

How could I extend / change this approach to be able to work with a centric viewmodel locator and imported viewmodels and views.

Yeah I know Prism and Caliburn but it would be intressting for me to see the approach...

Thanks for any help....

silverfighter
  • 6,762
  • 10
  • 46
  • 73

1 Answers1

0

Take a look at the MVVM Light samples (specifically the ViewModelLocator) for a great example of the pattern in action.

Note: you don't need to use the MVVM Light framework to apply this pattern - what you're really looking at it the architecture. The framework just makes it easier! FWIW, I do recommend you use a framework... :)

Jess Chadwick
  • 2,373
  • 2
  • 21
  • 24
  • Thanks for your responds. I know the ViewModel Locator pattern as well as the MVVM light implementation. My question was more about... when you do not all your ViewModels by start of the application or maybe you have more viewmodel locators and they get imported basically dynamic viewmodel locator based on composition... – silverfighter Oct 14 '11 at 13:28