Scoping With Unity IoC

2016/03/07

no comments

Scoping With Unity IoC

This post continue the Microsoft.Composition (Portable MEF): Scoping

and it dealing with scoping on Unity IoC.

If you didn’t read the Microsoft.Composition post it is recommend to take

a look on it before reading this post.

this post will focus on the API (and won’t discuss the concept).

 

Many IoC support scoping, many times it refer for child container.

in this post I will show how, Unity handle this task.

On future post I will discuss other container.

 

Scoping with Unity

this is how you do the scoping with Unity:

Code Snippet
  1. using (var container = new UnityContainer())
  2. {
  3.     container.RegisterType<IProcess, Process>(new ContainerControlledLifetimeManager());
  4.     container.RegisterType<IGlobal, Global>(new ContainerControlledLifetimeManager());
  5.     container.RegisterType<IRequest, Request>(new HierarchicalLifetimeManager());
  6.     container.RegisterType<IItemPerRequest, ItemPerRequest>(new HierarchicalLifetimeManager());
  7.  
  8.     var process = container.Resolve<IProcess>();
  9.     process.Execute();
  10. }

IRequest and IItemPerRequest registered by using HierarchicalLifetimeManager (lines 5,6).

This is how Unity register scope.

 

Code Snippet
  1. public class Process : IProcess
  2. {
  3.     [Dependency]
  4.     public IGlobal Global { get; set; }
  5.  
  6.     private IUnityContainer _container;
  7.     public Process(IUnityContainer container)
  8.     {
  9.         _container = container;
  10.     }
  11.  
  12.     public void Execute()
  13.     {
  14.         Console.WriteLine($"Global = {Global.Id}");
  15.  
  16.         using (var childContainer = _container.CreateChildContainer())
  17.         {
  18.             IRequest request = childContainer.Resolve<IRequest>();
  19.             request.Send();
  20.         }
  21.  
  22.         using (var childContainer = _container.CreateChildContainer())
  23.         {
  24.             IRequest request = childContainer.Resolve<IRequest>();
  25.             request.Send();
  26.         }
  27.  
  28.     }
  29. }

The process create child container which used for resolving the request (lines 16, 22).

 

Code Snippet
  1. public class Request : IRequest
  2. {
  3.     private static int _statId = 0;
  4.     public int Id { get; } = ++_statId;
  5.  
  6.     public void Send()
  7.     {
  8.         Console.WriteLine($"\tRequest Id = {Id}");
  9.         Console.WriteLine($"\t\tGlobal = {Global.Id}");
  10.         Console.WriteLine($"\t\tItem A: {ItemPerRequestA}");
  11.         Console.WriteLine($"\t\tItem B: {ItemPerRequestB}");
  12.     }
  13.  
  14.     [Dependency]
  15.     public IGlobal Global { get; set; }
  16.  
  17.     [Dependency]
  18.     public IItemPerRequest ItemPerRequestA { get; set; }
  19.     [Dependency]
  20.     public IItemPerRequest ItemPerRequestB { get; set; }
  21. }

The Request is using [Dependency] attribute to resolve dependencies (both ItemPerRequestA and ItemPerRequestB

will be the same instance per request and Global which is singleton).

 

Code Snippet
  1. public class ItemPerRequest : IItemPerRequest
  2. {
  3.  
  4.     private IUnityContainer _container;
  5.     public ItemPerRequest(IUnityContainer container)
  6.     {
  7.         _container = container;
  8.     }
  9.  
  10.     private static int _statId = 100;
  11.     public int Id { get; } = _statId++;
  12.  
  13.     public override string ToString()
  14.     {
  15.         // Unity don't support mutual dependency via attribute model
  16.         var request = _container.Resolve<Request>();
  17.         return $"Id = {Id} — of request –> {request.Id}";
  18.     }
  19. }

Because Unity don’t support mutual dependency via the attribute model

ItemPerScope is getting the IUnityContainer container on the constructor (line 4, 5)

and using it for lazy resolving at line 16.

 

The output of the execution should look like:

container, DI, Injection, IoC, Scope, Unity

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*