So how I would initially lay out this as a quick example.
ViewModel
public class EpisodeViewModel
{
private readonly IEpisodeModel episodeModel;
private readonly IViewFinder viewFinder;
public EpisodeViewModel(IEpisodeModel episodeModel, IViewFinder viewFnder)
{
this.episodeModel = episodeModel;
this.viewFinder = viewFinder;
CheckLoginPassed(this.episodeModel.LoginPassed);
}
private CheckLoginPassed(bool loginPassed)
{
if (!loginPassed)
{
this.viewFinder.LoadView<ILoginView>();
}
}
}
IView Interface
public interface IView
{
void Show();
}
Model Interface
public interface IEpisodeModel
{
bool LoginPassed
{
get;
}
}
Model
public class EpisodeModel : IEpisodeModel
{
private bool loginPassed;
public EpisodeModel()
{
if (Registry.GetValue("HKEY_CURRENT_USER", "URL", "") == null)
{
loginPassed = false;
}
}
public bool LoginPassed
{
get
{
return this.loginPassed;
}
}
}
IViewFinder Interface
public interface IViewFinder
{
void LoadView<T>();
}
ViewFinder
public class ViewFinder : IViewFinder
{
private readonly IEnumerable<IView> availableViews;
public ViewFinder(IEnumerable<IView> availableViews)
{
this.availableViews = availableViews;
}
public void LoadView<T>()
{
var type = typeof(T);
foreach (var view in this.availableViews)
{
if (view.GetType().IsAssignableFrom(type))
{
view.Show();
}
}
}
I've written this with using an IoC in mind, if you don't have one I'd really look to getting one as it will be a massive help when resolving dependencies. This is just a basic example and I'd probably have a different object that was only for checking the registry that I provided the results to the Model
from, but this should provide a start.
So in referring to the ILoginView
this is an interface that simply inherits from IView
, it doesn't actually provide any details. The IView
interface is slightly weird as your views already implements a Show
method whenever they also inherit from 'Window' so in effect you don't have to do anything, this simply provides an easier way to call show without having to actually know that what you are calling is a Window
.
Another point is that my ViewFinder
is relatively simple here although it would do for a small project I would probably look at something like MVVM Light to manage my MVVM handling as this comes with View handling and a simple Service Locator as standard.