Technical Insights: Azure, .NET, Dynamics 365 & EV Charging Architecture

Dependency Injection using Simple Injector Tutorial

This is a simple tutorial in how to use Dependency Injection using SimpleInjector (You can get this package from NuGet)

In this case, I use SimpleInjector to manage my Data Context – I want my Data Context to be per request (Unit of Work per request). The concept of this dependency Injection is to have a global container where you can resolve your object from

1. Create an extension method to the Simple Injector

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using SimpleInjector;
  6. using System.Diagnostics;
  7. using System.Linq.Expressions;
  8. ///<summary>
  9. /// Extension methods for registering types on a per web request basis.
  10. ///</summary>
  11. public static partial class SimpleInjectorPerWebRequestExtensions
  12. {
  13.     [DebuggerStepThrough]
  14.     public static void RegisterPerWebRequest<TService, TImplementation>(
  15.         this Container container)
  16.         where TService : class
  17.         where TImplementation : class, TService
  18.     {
  19.         Func<TService> instanceCreator =
  20.             () => container.GetInstance<TImplementation>();
  21.         container.RegisterPerWebRequest<TService>(instanceCreator);
  22.     }
  23.     [DebuggerStepThrough]
  24.     public static void RegisterPerWebRequest<TService>(
  25.         this Container container,
  26.         Func<TService> instanceCreator) where TService : class
  27.     {
  28.         var creator =
  29.             new PerWebRequestInstanceCreator<TService>(instanceCreator);
  30.         container.Register<TService>(creator.GetInstance);
  31.     }
  32.     [DebuggerStepThrough]
  33.     public static void RegisterPerWebRequest<TConcrete>(this Container container)
  34.         where TConcrete : class
  35.     {
  36.         container.Register<TConcrete>();
  37.         container.ExpressionBuilt += (sender, e) =>
  38.         {
  39.             if (e.RegisteredServiceType == typeof(TConcrete))
  40.             {
  41.                 var transientInstanceCreator = Expression.Lambda<Func<TConcrete>>(
  42.                     e.Expression, new ParameterExpression[0]).Compile();
  43.                 var creator = new PerWebRequestInstanceCreator<TConcrete>(
  44.                     transientInstanceCreator);
  45.                 e.Expression = Expression.Call(Expression.Constant(creator),
  46.                     creator.GetType().GetMethod(“GetInstance”));
  47.             }
  48.         };
  49.     }
  50.     [DebuggerStepThrough]
  51.     public static void DisposeInstance<TService>() where TService : class
  52.     {
  53.         object key = typeof(PerWebRequestInstanceCreator<TService>);
  54.         var instance = HttpContext.Current.Items[key] as IDisposable;
  55.         if (instance != null)
  56.         {
  57.             instance.Dispose();
  58.         }
  59.     }
  60.     private sealed class PerWebRequestInstanceCreator<T> where T : class
  61.     {
  62.         private readonly Func<T> instanceCreator;
  63.         internal PerWebRequestInstanceCreator(Func<T> instanceCreator)
  64.         {
  65.             this.instanceCreator = instanceCreator;
  66.         }
  67.         [DebuggerStepThrough]
  68.         public T GetInstance()
  69.         {
  70.             var context = HttpContext.Current;
  71.             if (context == null)
  72.             {
  73.                 // No HttpContext: Let’s create a transient object.
  74.                 return this.instanceCreator();
  75.             }
  76.             object key = this.GetType();
  77.             T instance = (T)context.Items[key];
  78.             if (instance == null)
  79.             {
  80.                 context.Items[key] = instance = this.instanceCreator();
  81.             }
  82.             return instance;
  83.         }
  84.     }
  85. }

2. Modify Global.asax – The class name will be MvcApplication in MVC Project

Code Snippet
  1. #region “Dependency Injection”
  2.         private static Container Container;
  3.         public static T GetInstance<T>() where T : class
  4.         {
  5.             return Container.GetInstance<T>();
  6.         }
  7.         protected void RegisterDependency()
  8.         {
  9.             //Create a main containers
  10.             var container = new Container();
  11.             // 2. Configure the container (register)
  12.             container.RegisterPerWebRequest<IUnitOfWork>(() => new UnitOfWork(new PosDataContext()));
  13.             container.Register<ITableRepository, TableRepository>();
  14.             container.Verify();
  15.             Container = container;
  16.         }
  17.         #endregion
  18.         protected void Application_Start()
  19.         {
  20.             AreaRegistration.RegisterAllAreas();
  21.             RegisterGlobalFilters(GlobalFilters.Filters);
  22.             RegisterRoutes(RouteTable.Routes);
  23.             BundleTable.Bundles.RegisterTemplateBundles();
  24.             RegisterDependency();
  25.         }
  26.         protected void Application_EndRequest(object src, EventArgs e)
  27.         {
  28.             ServiceStack.MiniProfiler.Profiler.Stop();
  29.             SimpleInjectorPerWebRequestExtensions.DisposeInstance<IUnitOfWork>();
  30.         }

3. Consume it from the controller – Call the container in the Global.asax to resolve the object (GetInstance function)

Code Snippet
  1. public ActionResult Index()
  2.         {
  3.             ViewBag.Title = “Tables”;
  4.             return View(MvcApplication.GetInstance<IUnitOfWork>().TableRepository.Get(e => e.Active));
  5.         }

Previous

IoC Container Benchmarks

Next

SlideRocket – Cloud

5 Comments

  1. These extension methods are now redundant with Simple Injector v2. You can simply call container.Register(new WebRequestLifestyle()) and you’re done.

  2. Hi Fransiscus

    This appears to be a Service Locator pattern. Some would argue this is misusing an IoC container. Personally I don’t like to argue so I will leave it to readers and author to hash out 🙂 I do recommend:
    http://www.manning.com/seemann/
    or the short version: http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/

    • Hi Devon,

      thank you for your feedback. CMIIW, what I did was querying the repository (Service Locator) to get the interface while ideally it should be auto wired ?Do you mind to elaborate more what’s the difference between those Service Locator and IoC?I thought somehow both are inter-related

      • Yes I suppose Service Locator(SL) is IoC. I am a bit vague on whether SL counts as DI, as usually this is done with the autowiring for constructor injection or property injection.
        I prefer constructor injection as it revels a classes dependencies. With a SL a class will look like it has no dependencies, and only at runtime will you see that they have not been configured. So in this case you have inverted control of creation of your UoW but your controller is dependent on the SL. I have not thought it through entirely but I would imagine having the SL as a static could lead to some complications if you tried to unit test your controllers.
        I know better developers than myself who use SL but I personally don’t like it adn I just thought I would point out that there are those that consider it an anti-pattern rather than a design pattern as there are ways to achieve the same decoupling without the drawbacks. Just my 2 cents…

Leave a Reply to Steven Cancel reply

Your email address will not be published. Required fields are marked *

Powered by WordPress & Theme by Anders Norén