0

I am using the Galasoft MVVM Toolkit and ServiceLocator to manage my services in my application. I would like to manage two different implementation of service. A stub implementation should be injected in a debug/design mode and a real implementation should be injected in other cases. A pseudo code could be :

public TmepServiceLocator(){
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    //If DEBUG/DESIGN MODE
    //SimpleIoc.Default.Register<IMyService>(() => new MyServiceStub());
    //ELSE
    SimpleIoc.Default.Register<IMyService>(() => new MyServiceImpl());
}
public IMyService MyService{
    get{return ServiceLocator.Current.GetInstance<IMyService>();}
}

How can I do that ?
Do you have a workaround to work with service stubs when developping (and without using Spring)?

ltu
  • 169
  • 3
  • 16
  • You want to debug using an implementation different than the production one ? Do you wish to go crazy when something breaks in production ? Why are you doing this to yourself ? – thomasb Feb 23 '15 at 16:36
  • Because the real service will use a DB to store/query data and I want to be able to debug with a stub that does not need to connect to a DB. In this stub, data are stored in a List. If the real service is still in development (developed by other guys of your team), it allows you to develop other components of the application without waiting for the full implementation of the service. I typically worked like this with Spring. So maybe Spring is the only one solution... – ltu Feb 23 '15 at 16:59
  • 1
    Ok, then can't you use a regular dependency injection framework (you can use light ones like SimpleInjector or LightInject), and just replace the bootstrapper when you get the final DB module ? – thomasb Feb 23 '15 at 20:41
  • I agréé. Seems to the best solution. Thanks. – ltu Feb 24 '15 at 20:06

1 Answers1

0

In MVVM Light, you normally use an object of type ViewModelLocator where you do the dependcy injection.

You can use the static method ViewModelBase.IsInDesignModeStatic.

if (ViewModelBase.IsInDesignModeStatic)
{
  SimpleIoc.Default.Register<IMyService, StubServiceImplementation>();
}
else
{
SimpleIoc.Default.Register<IMyService, RealServiceImplementation>();
}

other than that you can use conditionals

#if DEBUG
SimpleIoc.Default.Register<IMyService, StubServiceImplementation>();
#else
SimpleIoc.Default.Register<IMyService, RealServiceImplementation>();
#endif
matthes
  • 102
  • 2
  • You use a ViewModelLocator to manage and inject ViewModels but you use ServiceLocator to manage and inject services. Is DEBUG a variable of VisualStudio – ltu Feb 23 '15 at 17:02
  • You can certainly put the service injection wherever you want. See https://msdn.microsoft.com/en-us/magazine/jj991965.aspx from the author of MVVM Light. Quote 'In some MVVM applications (and notably apps based on the MVVM Light toolkit), a class named ViewModelLocator is used to create and expose some of the application’s ViewModels. This is a convenient location in which to register most of the services and service consumers' in standard mvvm light app, the locator is added as a static resource, so ill always leave injection there. DEBUG is a constant, define your own: #define useDesign – matthes Feb 23 '15 at 17:34