【问题标题】:Replacing Mvvm Lights SimpleIoc with Unity IoC用 Unity IoC 替换 Mvvm 灯 SimpleIoc
【发布时间】:2016-04-05 11:59:24
【问题描述】:

在用统一 IoC 替换 SimpleIoc 后,我在启动和运行时遇到了一些麻烦。我关注了these instructions,指的是如何实现IServiceLocator以实现统一。

但是我的应用程序无法正常运行。例如,我无法将 dataGrids _selectedDevice 设为 null 以外的任何值。使用 SimpleIoc,一切正常。

这是我的 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

    }
}

所有的 ViewModel 都这样称呼这个 IoC:

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

这条线有问题吗?

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

我正在尝试初始化FxContext 的一个实例并将其分享给所有调用ServiceLocator.Current.GetInstance&lt;IUnitOfWork&gt;()ViewModels

编辑:

基本上我希望能够为两组不同的数据库使用相同的UnitOfWork,如下所示:

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

【问题讨论】:

    标签: c# unity-container mvvm-light


    【解决方案1】:

    似乎注册类似于 SimpleIoc 的视图的答案是以下行:

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

    ContainerControlledLifetimeManager() 是使用 RegisterType 方法时的关键。

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      相关资源
      最近更新 更多