Design Time Data Interface Implementation

181 Views Asked by At

I have a ViewModel class like this

public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        if (!IsInDesignMode)
        {
            throw new InvalidOperationException();
        }
    }

    public MyViewModel(IDataProvider dataProvider)
    {
        Data = new ObservableCollection<IData>(dataProvider.GetData());
    }

    public ObservableCollection<IData> Data { get; private set; }
}

Now I want to create some design time data. In my unit tests I am using a mocking framework (Moq) to do this. I don't like the fact that I need to create some Mock implementation of IData in my App project or referencing and using the Mocking framework.

What is the most elegant way to achieve having design time data in such a scenario?

Edit: Not sure if it is relevant but I am using Visual Studio 2012.

1

There are 1 best solutions below

2
On BEST ANSWER

Basically you create a stub, not a mock for design time data. The stub can't have any dependencies injected.

public class MyDesignViewModel : ViewModelBase
{
    public MyViewModel()
    {
        Data = new ObservableCollection<IData>(new List<IData>() 
        {
             new MyData() { Value1 = 1, Value2 = "Test" },
             ...
        });
    }

    public ObservableCollection<IData> Data { get; private set; }
}

Then use it in XAML like this:

<UserControl x:Class="MyView"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DataContext="{d:DesignInstance Type=vm:MyDesignViewModel, IsDesignTimeCreatable=True}">