I am quite new to MVVM pattern so please bear with me. I have seen implemnentations in wpf +mvvm + prism where all the views tend to have an IView as top most interface. Then the views in the respective modules have a view specific interface like IViewA, IViewB etc which implement the IView interface. Even the viewmodel has IViewModel top most interface and the subsequent modules have IViewAViewModel , IViewBViewModel etc that inherit from the IViewmodel. IViewmodel has a reference to Iview and the Iview has a reference to IViewModel.
namespace xxx.xxx.infrastructure
{
public interface IView
{
IViewModel ViewModel {get;set;}
}
public interface IViewModel
{
IView View {get;set;}
}
public abstract class ViewModelBase : IViewModel, INotifyPropertyChanged
{
public IView View {get;set;}
public ViewModelBase(IView view)
{
View = view;
View.ViewModel = this;
}
//INotifyPropertyChanged left out
}
}
namespace xxx.xxx.Modules.Customer
{
public interface ICustomerDetailsView : IView
{
}
public partial Class CustomerDetailsView : UserControl, ICustomerDetailsView
{
public CustomerDetailsView ()
{
InitializeComponent();
}
//Is this implementation acceptable?The view is supposed to have zero code in the code behind.....
public IViewModel ViewModel
{
get
{
return (ICustomerDetailsViewViewModel)DataContext;
}
set
{
DataContext = value;
}
}
}
public interface ICustomerDetailsViewViewModel : IViewModel
{
string Message {get;set;}
}
public class CustomerDetailsViewViewModel : ViewModelBase, ICustomerDetailsViewViewModel
{
//Will be injected by unity as i have set up mappings in module initilize.
public CustomerDetailsViewViewModel(ICustomerDetailsView view)
:base(view)
{
}
public string Message
{
//INotifyPropertyChanged left out for brevity
get;set;
}
}
I have a few questions.
1.)Isnt this violation of MVVM as code behind file is supposed to have zero code?
2.)in MVVM view model should not worry the view or its contract?Doesnt the above implementation break it?
3.)I cannot understand what is the use of this implementation. In fact this borders on MVP and lot of code is required.
4.)If this is an acceptable way to implement, do i need to have interfaces for all the views and viewmodels in all of my modules.?