Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellabyte/1524434 to your computer and use it in GitHub Desktop.
Save kellabyte/1524434 to your computer and use it in GitHub Desktop.
public abstract class ViewModelFactory
{
protected static IViewModelResolver resolver = null;
public static void InitializeResolver(IViewModelResolver resolver)
{
// This is where we set the resolver for the ViewModelFactory.
// This should get set in the Bootstrapper (Composition Root)
// to the resolver implementation using the
// IoC container that has been chosen for the application.
ViewModelFactory.resolver = resolver;
}
}
public abstract class ViewModelFactory<TViewModel, TDesignViewModel>
: ViewModelFactory
where TViewModel : IViewModel
where TDesignViewModel : TViewModel, new()
{
public ViewModelFactory()
{
}
public TViewModel ViewModel
{
get
{
TViewModel viewModel;
bool designMode = DetectDesignMode();
if (designMode)
{
viewModel = new TDesignViewModel();
}
else
{
if (resolver == null)
{
throw new InvalidOperationException();
}
// Use the resolver implementation to resolve
// the view model requested.
viewModel = (TViewModel)resolver.Resolve(typeof(TViewModel));
}
viewModel.OnLocated();
return viewModel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment