views:

20

answers:

2

Im working with the MVVM pattern + a simple ServiceLocator implementation, now to my problem how am i supposed to setup the services when the views are running in design time?

Iv tryed this but it does not seem to work in VS 2010 or some thing, i know it worked on my old computer but on my new it does not. so does any one know a good alternative?

Edit: (On behalf of Merlyn Morgan-Graham)

Well what im trying to do is this, i have my view, ViewModel and services now the difference here is that i have 2 implementations of each service one for design time and one for run time. for a better explanation look here.

A: 

You usually don't need to access services at design-time... Typically, you don't even use your real ViewModels at design-time, you use dummy design data, as explained here. If you really need to use your real ViewModels, you can implement dummy versions of your services, and use them instead of the real services :

if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
    // Design time
    ServiceLocator.Instance.Register<IService1>(new DummyService1());
    ServiceLocator.Instance.Register<IService2>(new DummyService2());
}
else
{
    // Run time
    ServiceLocator.Instance.Register<IService1>(new RealService1());
    ServiceLocator.Instance.Register<IService2>(new RealService2());
}
Thomas Levesque
well yes my goal is to use dummy services but where do i put this code that registers the services so that it run in design time?
Petoj
Good question... I usually put service initialization in the App's OnStartup method, but this method isn't executed at design time. Are you sure it doesn't work with the DesignTimeBootstrapper attribute mentioned in Josh Smith article ?
Thomas Levesque
@Thomas, Petoj: I Just tried it. not working for me :(
Merlyn Morgan-Graham
+1  A: 

If you want to decouple your view from your viewmodel, and your viewmodel from your model/dal (basically, if you want to use MVVM), then your view model and data model shouldn't know anything about design time. Design time only applies to the view.

This article shows a way to define your design time data via XML/XAML, so your code underneath doesn't have to know anything about it:

http://karlshifflett.wordpress.com/2009/10/21/visual-studio-2010-beta2-sample-data-project-templates/

After Edit: It turns out that you'll still have to use your view model for your existing XAML bindings to work. This will just populate the view model rather than having to create a new data model. I'm not sure, but there might be classes that allow you to use the WPF binding mechanism to take care of this... Views?

Resume Before Edit...: As far as the solution in the article you linked first, the designer doesn't instantiate anything but your class, and the code it references. That means that assembly attributes won't be instantiated unless your view code somehow directly references them.

If you really want to couple your view models to your views during design time, and make it so that design time services are registered, then you have to place the service registration code in your view class, or a class the view class directly references.

To do that, you could use static constructors of your views to register your design time services. You could also write a static method on some other class (application?) to (conditionally) register the design time services. Then, call that method in the constructor of your views.

Or you could simply register them in the constructor for each of your views.

Basically, what you want to do is possible, but that method linked in the first article isn't. If you read farther in the comments, you'll see that his method is broken.

You may also want to question the idea of hooking your view model to your view during design time, because the MVVM pattern was made to avoid that sort of thing.

Merlyn Morgan-Graham