Ich habe Probleme, die Dinge nach dem Ersetzen von SimpleIoc mit der IoC-Einheit in Betrieb zu nehmen. Ich folgte these instructions mit Bezug auf die Implementierung der IServiceLocator
für die Einheit.Ersetzen von Mvvm Lights SimpleIoc mit Unity IoC
Allerdings funktioniert meine Anwendung nicht richtig. Zum Beispiel bin ich nicht in der Lage, die dataGrids
_selectedDevice
zu etwas anderem als null zu bekommen. Mit SimpleIoc hat alles gut funktioniert.
Hier ist mein ViewModelLocator:
using System;
using System.Windows;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
namespace FxEditorDatabaseStructure.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
#region Constructor
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
// Register the IOC container as SimpleIoc
//ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
// Register Unity as the IOC container
var unityContainer = new UnityContainer();
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(unityContainer));
//If we wish use another IoC we must implement the IServiceLocator interface
// Create new UnitOfWork for all the views IOC - CREATED AT registration
//SimpleIoc.Default.Register<IUnitOfWork, UnitOfWork>(true);
unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(new FxContext()));
/*
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<DeviceDatabaseViewModel>();
SimpleIoc.Default.Register<ProjectDeviceDatabaseViewModel>();
*/
unityContainer.RegisterType<MainViewModel>();
unityContainer.RegisterType<DeviceDatabaseViewModel>();
}
#endregion
#region ViewModels
[NotNull]
public MainViewModel MainViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
private set { if (value == null) throw new ArgumentNullException(nameof(value)); }
}
[NotNull]
public DeviceDatabaseViewModel DeviceDatabaseViewModel
{
get { return ServiceLocator.Current.GetInstance<DeviceDatabaseViewModel>(); }
private set { if (value == null) throw new ArgumentNullException(nameof(value)); }
}
/// <summary>
/// Retrieves this instance from the application's resources and exposes it to other objects.
/// </summary>
public static ViewModelLocator Instance
{
get
{
return Application.Current.Resources["Locator"] as ViewModelLocator;
}
}
#endregion
#region cleanup
public static void Cleanup()
{
// TODO Clear the ViewModels
}
#endregion
}
}
Alle Viewmodels dieses IoC wie folgt aufrufen:
private readonly IUnitOfWork _context = ServiceLocator.Current.GetInstance<IUnitOfWork>();
Gibt es etwas falsch mit dieser Linie?
unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(new FxContext()));
Ich versuche, eine Instanz von FxContext
und teilen diese auf alle ViewModels
die alle Anruf ServiceLocator.Current.GetInstance<IUnitOfWork>()
EDIT zu initialisieren:
Grundsätzlich ich in der Lage sein wollen, das gleiche zu verwenden UnitOfWork
für zwei verschiedene Datenbanksätze, wie im Folgenden:
var deviceDatabase = new DeviceContext();
var projectDatabase = new ProjectContext();
unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(deviceDatabase));
unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(projectDatabase));