Reputation: 6882
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....
Upvotes: 1
Views: 820
Reputation: 2373
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... :)
Upvotes: 0