Created
December 27, 2011 17:19
-
-
Save kellabyte/1524434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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