2016-04-05 13 views
0

SimpleIoc'un birleşim IoC ile değiştirilmesinden sonra işleri halletmek için biraz sıkıntı yaşıyorum. Birlik için IServiceLocator'u nasıl uygulayacağımı gösteren these instructions'u takip ettim. Ancak, uygulamam düzgün çalışmayabilir. Örneğin, sıfırdan başka bir şey olması için dataGrids_selectedDevice'u alamıyorum. SimpleIoc ile her şey iyi çalıştı.Mvvm Işıklarını Değiştirme Unity IoC ile BasitEco

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 

    } 
} 

Tüm ViewModels böyle bu IoC diyoruz: Bu hat ile ilgili bir sorun

private readonly IUnitOfWork _context = ServiceLocator.Current.GetInstance<IUnitOfWork>(); 

var mı

İşte benim ViewModelLocator var?

unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(new FxContext())); 

Ben FxContext bir örneğini başlatmak ve tüm ViewModels bu paylaşmaya çalışıyorum hangi tüm çağrı ServiceLocator.Current.GetInstance<IUnitOfWork>()

DÜZENLEME:

Temelde aynı kullanabilmek istiyorum Aşağıdaki gibi iki farklı veritabanı kümesi için UnitOfWork:

var deviceDatabase = new DeviceContext(); 
var projectDatabase = new ProjectContext(); 
unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(deviceDatabase)); 
unityContainer.RegisterType<IUnitOfWork, UnitOfWork>(new InjectionConstructor(projectDatabase)); 

cevap

0

aşağıdaki hattı ile SimpleIoc benzer görünümleri kayıt cevap gibi görünüyor: RegisterType yöntemi kullanılırken ContainerControlledLifetimeManager() anahtar olduğu

unityContainer.RegisterType<MainViewModel>(new ContainerControlledLifetimeManager()); 

.