This project has moved. For the latest updates, please go here.

ViewModel Service Locator

Nov 18, 2010 at 3:25 PM

How do you go about registering a service for your view model via dependency injection and/or mef importing constructor when registering your views to your viewmodel?

[ExportAsViewModel(Constants.MYVIEWMODEL)]
public class MyViewModel : BaseViewModel
{
	private IService _service;
    public MyViewModel(IService service)
    {
        _service = service;
    }
}

[Export] 
public ViewModelRoute Binding 
{ 
  get 
  { 
    return ViewModelRoute.Create(Constants.MYVIEWMODEL, "Shell"); 
  }
} 

 

Coordinator
Nov 18, 2010 at 11:19 PM

So a simple service just becomes

[EXport(typeof(IServer))]

on the service and

[Import] IService (public) on the view model.

For more complex scenarios, I might use a locator like this:

http://csharperimage.jeremylikness.com/2010/07/using-hints-for-generic-mef-exports.html

Nov 19, 2010 at 11:14 AM

ok, thanks