I've just begun using the following interfaces to produce a lightweight MVVM framework designed to be introduced control-by-control into an existing application.
public interface IView<C>
where C : class
{
}
public interface IView<C, VM> : IView<C>
where C : class
where VM : IViewModel
{
IDisposable Bind(VM viewModel);
}
public interface IViewModel : INotifyPropertyChanged
{
}
public interface IViewModel<M> : IViewModel
where M : class
{
IView<C> Bind<C>(M model) where C : class;
IView<C> Bind<C>(string key, M model) where C : class;
}
public interface IViewModel<VM, M> : IViewModel<M>
where VM : IViewModel<VM, M>
where M : class
{
}
The following interfaces are an example Person
model and view model.
public interface IPerson : INotifyPropertyChanged
{
string Name { get; set; }
DateTime? Dob { get; set; }
}
public interface IPersonViewModel : IViewModel<IPersonViewModel, IPerson>
{
string Name { get; set; }
DateTime? Dob { get; set; }
int? Age { get; }
string Title { get; }
}
And the view implementation, using data binding only, looks like this:
[Factory(typeof(IView<Control, IPersonViewModel>))]
public partial class PersonView : UserControl, IView<Control, IPersonViewModel>
{
public PersonView()
{
InitializeComponent();
}
public IDisposable Bind(IPersonViewModel viewModel)
{
this.personViewModelBindingSource.DataSource = viewModel;
return Disposable.Create(() => this.personViewModelBindingSource.Dispose());
}
}
Now, to use all this I just have the following code:
var personM = context.Resolve<IPerson>();
var personVM = context.Resolve<IPersonViewModel>();
var personV = personVM.Bind<Control>(personM);
var personC = personV.AsControl();
personC.Dock = DockStyle.Fill;
this.panel1.Controls.Add(personC);
I'm using dependency injection to resolve instances of my model, view model & view. The view model's Bind
method resolves for the view. All of this is strongly-typed and allows us to write view models that can be used with Windows Forms, WPF & Silverlight.
I hope this is enough for you to work with.