0
votes

I am having some trouble to get things up and running after replaced SimpleIoc with the unity IoC. I followed these instructions referring how to implement the IServiceLocator for unity.

However my application fails to function properly. For example I'm unable to get the dataGrids _selectedDevice to be anything else than null. With SimpleIoc, everything worked fine.

Here's my 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

    }
}

All the ViewModels call this IoC like this:

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

Is there something wrong with this line?

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

I'm trying to initialise one instance of FxContext and share this to all the ViewModels which all call ServiceLocator.Current.GetInstance<IUnitOfWork>()

EDIT:

Basically I want to be able to use the same UnitOfWork for two different sets of database, like in the following:

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

1 Answers

0
votes

Seems like to the answer to register the views similar to SimpleIoc is by following line:

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

where the ContainerControlledLifetimeManager() was the key when using RegisterType method.