views:

44

answers:

2

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 necessary.

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?

+2  A: 

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.

Enigmativity
@Enigmativity; I like your interfaces but I would think that reuse of everything beyond the interfaces and the model would be limited (ie, you need different implementations of both the view and VM for each technology you want to support). Would love to hear if you are in fact using them with multiple technologies and have a different opinion though. Cheers
Berryl
@Berryl - I do indeed use them with different UI technologies. The VM is independent to the UI technology so you only have to re-implement the view interface, not the view model interface.
Enigmativity
A: 

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

Jose