Design Time ViewModel data

Feb 26, 2013 at 3:58 PM
How can I provide dummy ViewModel data for design time? My data comes (mostly) from a web service, so should I write a dummy web service? How to I get it selected automatically by the DI Container?

Thanks!

R.
Coordinator
Feb 26, 2013 at 5:27 PM
Hi Stony,

There are a number of ways to solve this problem and it really depends. Because the DI container understands design time vs. runtime you can replace different parts of the application at design time. So for example you can use a different ViewModel at design time or a different data model or even a different WCF client proxy.

You can control which components are used at design time by attributing the class with DesignTimeAttribute or DesignTimeOnlyAttribute or NonRunTimeAttribute. When the container is in Design time it will favor classes marked with these Attributes over other classes.

In the simple weather example app I have an example of using a different WCF client proxy called FauxWeatherSoapClient. This class implements the WeatherSoap interface that is used by the WCF client proxy and exports itself as NonRunTime (used for design time and unit test time but not runtime) returning back a faux set of data.

-Ian
Feb 26, 2013 at 6:09 PM
Hi Ian,

How do you get the container into design mode? I can see some trickery going on with TestResource.cs, but I can't make it work in my app. (I did not create the app from a template, so I may well be missing some boilerplate code).

Cheers,

R.

From: ipjohnson123

Hi Stony,

There are a number of ways to solve this problem and it really depends. Because the DI container understands design time vs. runtime you can replace different parts of the application at design time. So for example you can use a different ViewModel at design time or a different data model or even a different WCF client proxy.

You can control which components are used at design time by attributing the class with DesignTimeAttribute or DesignTimeOnlyAttribute or NonRunTimeAttribute. When the container is in Design time it will favor classes marked with these Attributes over other classes.

In the simple weather example app I have an example of using a different WCF client proxy called FauxWeatherSoapClient. This class implements the WeatherSoap interface that is used by the WCF client proxy and exports itself as NonRunTime (used for design time and unit test time but not runtime) returning back a faux set of data.

-Ian

Coordinator
Feb 26, 2013 at 6:32 PM
Hi Stony,

So without going into to much detail the framework detects if it's being hosted in a designer and will setup the container to be in design mode for you. The part about the TestResource.cs class you are correct it is used for design time purposes.

The reason for this class is because at design time Visual studio does not load any of your code into the designer (i.e. your Views, ViewModels, data models etc.) so when it comes time to configure the DI container there are no assemblies loaded into the process space hence no exports to be found. The one exception to this rule is for classes that are directly referenced by the XAML. Since the TestResource class is referenced in the App.xaml the class and assembly get loaded into the process space and the DI container will be configured to scan the assembly.

The one catch to all of this is that it doesn't work (yet) for projects that are split across multiple assemblies (I'm still trying to figure out a way to bootstrap it).

If you want to contact me directly I can take a look at your project if you want.

-Ian