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/1524433 to your computer and use it in GitHub Desktop.
Save kellabyte/1524433 to your computer and use it in GitHub Desktop.
public abstract class ViewModelFactory<TViewModel, TDesignViewModel>
where TViewModel : IViewModel
where TDesignViewModel : TViewModel, new()
{
public ViewModelFactory()
{
}
public TViewModel ViewModel
{
get
{
TViewModel viewModel;
bool designMode = DetectDesignMode();
if (designMode)
{
// At design time instantiate design time version.
// Populates mock data, dependencies don't have to be real.
viewModel = new TDesignViewModel();
}
else
{
// Request IoC container to resolve view model & its deps.
viewModel =
ApplicationHost.Current.Container.Resolve<TViewModel>();
}
viewModel.OnLocated();
return viewModel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment