This is a mirror of official site: http://jasper-net.blogspot.com/

MVVM, Design Time vs IoC usage

| Sunday, July 24, 2011
The problems

Lets start with the problems we want to solve with our solution

    Blend Design time support
    Resharper binding validation support
    No using ServiceLocator, DI only
    ViewModels should be testable

Frameworks like Magellan solve these sort of issues in other ways, but if you don't want to take on a framework then maybe an approach like this may help you get started.
My approach

I find that once you progress past a basic application with simple interactions that often you need views to interact with each other, this could be returning data from a modal dialogue or anything.

One thing we are often told is we should develop against interfaces, not implementations. I think this applies to views as well, so lets create an IView interface.

public interface IView
{
}

Now I want to be able to tell the calling viewmodel if I was cancelled, or possibly return data, so we will create three more interfaces and a few classes:

public interface IDialogueView : IView
{
    void DialogueDisplayed();
    void DialogueClosed();
}

public interface IDialogueView<TResult> : IDialogueView
{
    event EventHandler<DialogueResultEventArgs<TResult>> Finished;
}

public interface IDialogueViewWithoutResult : IDialogueView
{
    event EventHandler<DialogueResultEventArgs> Finished;
}

public class DialogueResultEventArgs : EventArgs
{
    public DialogueResultEventArgs(bool cancelled)
    {
        Cancelled = cancelled;
    }

    public bool Cancelled { get; private set; }

    public static DialogueResultEventArgs EmptyResult = new DialogueResultEventArgs(false);
    public static DialogueResultEventArgs CancelledResult = new DialogueResultEventArgs(true);
}


Read more: Jake Ginnivan
QR: wpf-mvvm-thoughs

Posted via email from Jasper-net

0 comments: