Interface Controller (ViewModel) for multiple UI's
I am currently designing a new .NET application and would like to keep it UI independant.
Whilst I initally would like to use WPF, I would like to have the option of swapping the UI to ASP or WinForms if ne开发者_如何学Gocessary.
In a layered design:
View - Interface Controller (ViewModel) - Model - Persistance
is it possible to design the Interface Controller so that it will work with different view technologies, or will I need to replace the interface controller at the same time as the View?
Take a look at my answer to the question "Silverlight with MVVM Inheritance: ModelView and View matching the Model".
The answer I gave works for your situation too.
In a nutshell, I define the following general interfaces:
public interface IModel
{
}
public interface IViewModel
{
}
public interface IViewModel<M> : IViewModel
where M : IModel
{
void Bind(M model);
}
public interface IView
{
}
public interface IView<VM> : IView
where VM : IViewModel
{
void Bind(VM viewModel);
}
Which allow me to define specific interfaces like this:
public interface IPersonModel : IModel
{
}
public interface IPersonViewModel : IViewModel<IPersonModel>
{
}
public interface IPersonView : IView<IPersonViewModel>
{
}
By implementing your layers against these interfaces you can swap your implementation of the IView<VM>
interface with WPF, Silverlight, Windows Forms or even ASP.Net.
As long as you
1) Are not tempted to put code-behind in your xaml markup
2) and limit all of the relevant functionality to be dictated by your viewmodel
then yes you should be able to swap it out with any view technology.
When you swap it out, though you will obviously have to rewire the way the new ui interacts with the viewmodel. The main interfaces that WPF uses to connect to the viewmodel is INotifyPropertyChanged, INotifyCollectionChanged, and ICommand. So your new ui will basically have to tap into those interfaces to gain the same functionality
精彩评论