I came across two ways to initialize Views and ViewModels in MVPM CAL WPF.
1 - It seems to be more popular. You must enable the ViewModel to automatically allow the view. ViewModel contains view information.
public interface IView
{
void SetModel(IViewModel model);
}
public interface IViewModel
{
IView View { get; }
}
public class View
{
public void SetModel(IViewModel model)
{
this.DataContext = model;
}
}
public class ViewModel
{
private IView view;
public ViewModel(IView view)
{
this.view = view;
}
public IView View { return this.view; }
}
2 - It seems a lot cleaner and removes the View from the ViewModel. You must enable View to automatically enable ViewModel. Embeds objects in a view (not sure if this is good or not).
public interface IView
{
}
public interface IViewModel
{
}
public class View
{
private IViewModel model;
public View(IUnityContainer unityContainer)
{
this.model = unityContainer.Resolve<IViewModel>();
this.DataContext = this.model;
}
}
public class ViewModel
{
}
What is an acceptable method of initializing representations and models and what are the advantages and disadvantages of each method. Should you include objects in your view?