IT:AD:Unity

Summary

Process

Preparation

Use Unity (ie, “Tools/Library Package Manager/Manage Packages…“ to search for and install Unity into your App.

Examples

Try the following Console Application to get a feel for dependency injection.

            //Register something using one of the lifetime managers:
            //* TransientLifetimeManager(DEFAULT)  -- each time a new one
            //* ContainerControlledLifetimeManager -- singleton per container
            //* HierarchicalLifetimeManager -- same as Container, but each nested container gets its own
            //* PerResolveLifetimeManager - related to Transient (look it up)
            //* ExternallyControlledLifetimeManager - uses Weaklinks to externally killable resources
            //* PerThreadLifetimeManager - 
            //unityContainer
            //    .RegisterType<ISomeService, SomeService>(new ContainerControlledLifetimeManager())
            //    .RegisterType<ISomeService2, SomeService2>(new PerThreadLifetimeManager());

            ////Get the service:
            //ISomeService someService = unityContainer.Resolve<ISomeService>();

            ////Use it:
            //Console.WriteLine(someService.BePolite("Paul"));

Example:

using System;
using Microsoft.Practices.Unity;

namespace XAct.Studies.UnityDemo01
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a IoC Dictionary/Builder:
            IUnityContainer unityContainer = new UnityContainer();

            //Register something using one of the lifetime managers:
            //* TransientLifetimeManager(DEFAULT)  -- each time a new one
            //* ContainerControlledLifetimeManager -- singleton per container
            //* HierarchicalLifetimeManager -- same as Container, but each nested container gets its own
            //* PerResolveLifetimeManager - related to Transient (look it up)
            //* ExternallyControlledLifetimeManager - uses Weaklinks to externally killable resources
            //* PerThreadLifetimeManager - 
            unityContainer
                .RegisterType<ISomeService, SomeService>(new ContainerControlledLifetimeManager())
                .RegisterType<ISomeService2, SomeService2>(new PerThreadLifetimeManager());

            //Get the service:
            ISomeService someService = unityContainer.Resolve<ISomeService>();

            //Use it:
            Console.WriteLine(someService.BePolite("Paul"));

        }
    }


    //Define interfaces in another assembly (eg: App.Domain.Services.Contracts)
    interface ISomeService
    {
        string BePolite(string name);
    }
    interface ISomeService2 { }


    //Define instances in another assembly (eg: App.Domain.Services)
    public class SomeService : ISomeService
    {
        public string BePolite(string name)
        {
            return string.Format("Hello, {0}.", name);
        }
    }
    public class SomeService2 : ISomeService2{}
}

Registering it in ServiceLocator:

UnityServiceLocator locator = new UnityServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => locator);
var a = ServiceLocator.Current.GetInstance<IFoo>();

Resources