Implementing MVVM Light toolkit WPF Unity
The way I use Unity on WPF (MVVM Light) is like this:
I create a bootstrapper class on the application root, something like:
public class Bootstrapper{ public IUnityContainer Container { get; set; } public Bootstrapper() { Container = new UnityContainer(); ConfigureContainer(); } private void ConfigureContainer() { Container.RegisterType<IMyRepo, MyRepo>(); Container.RegisterType<MainViewModel>(); }}
This is my bootstrapper. I register the ViewModels too because is easy create them in the Locator.
Next, I create the boostrapper on the ViewModelLocator's constructor and I resolve every ViewModel here, like:
public class ViewModelLocator{ private static Bootstrapper _bootStrapper; static ViewModelLocator() { if (_bootStrapper == null) _bootStrapper = new Bootstrapper(); } public MainViewModel Main { get { return _bootStrapper.Container.Resolve<MainViewModel>(); } }}
As you see, my ViewModelLocator is simple, it just create the bootstrapper and resolve the ViewModel, and these VM will resolve their dependencies through the container too :)
Maybe there is a best way to archieve this, but this is a good start indeed.
I would advise to use Managed Extensibility Framework. It's in .NET 4 and I switched myself from unity to MEF. I works very great when your app is growing. You can find lots of info on it by search using google.Good luck!