Saturday, April 28, 2012

Creating my own dependency injection system II

This post is about extending my own dependency injection system with binding scopes.

In the previous post I showed a very basic DI system. Several things can be added to it, and today I will show how to add binding scopes. What is a binding scope? A binding scope is an indication of when and how the created instances of the DI system should be re-used.

For example, a singleton binding scope. With the singleton binding scope we indicate that once an instance of an object is created for a specific binding rule, no more instances should be created, and that same instance should be returned.

For now we are going to implement the following binding scopes:
  • Normal – An instance is created every time, is the default scope.
  • Singleton – An instance is created once and re-used after.
  • Thread – An instance is created per thread, and re-used after in that thread.
  • WebRequest – An instance is created per web request, and re-used after for that request.
  • WebSession – An instance is created per web session, and re-used after for that session.
  • WebCache – An instance is created when is not yet available in the caching of the web application.
The last three are obviously only applicable in a web application.

First things first. I updated the code from the previous post:
  1. The Factory.GetInstance<TInterface>() method has been renamed to Factory.ResolveInstance<TInterface>().
  2. Accordingly, the interface IInstanceCreator<TInterface> has been renamed to IInstanceResolver<TInterface>. Also, the Create() method has been renamed to Resolve().
  3. Accordingly, the class InstanceCreator<TInterface, TClass> has been renamed to InstanceResolver<TInterface, TClass>. And, of course, the method has been renamed as well.
Good, that’s done. Onto the binding scopes. To implement the binding scopes we basically need to do two things:
  1. Add the ability to indicate the binding scope on a binding rule using the fluent interface.
  2. Update the binding rule or instance resolver so that the binding scope is incorporated.
I want to update the fluent interface in the following way.
   1: Factory.Bind<ICommunicator>().To<WirelessCommunicator>()
   2:     .WithScope(BindingScope.Singleton);
The first one is simple. First we define a binding scope enumeration.
   1: namespace MyDI {
   2:     public enum BindingScope {
   3:         Normal,
   4:         Singleton,
   5:         Thread,
   6:         WebRequest,
   7:         WebSession,
   8:         WebCache
   9:     }
  10: }
Now we can add the binding scope to the fluent interface.
   1: namespace MyDI {
   2:     public interface IBindingScopeIndicator {
   3:         void WithScope(BindingScope scope);
   4:     }
   5: }
   1: namespace MyDI {
   2:     public interface IBindingRule { }
   3:  
   4:     public interface IBindingRule<TInterface> : IBindingRule {
   5:         IBindingScopeIndicator To<TClass>()
   6:             where TClass : class, TInterface, new();
   7:     }
   8: }
   1: using System;
   2:  
   3: namespace MyDI {
   4:     class BindingRule<TInterface> : IBindingRule<TInterface>, IBindingScopeIndicator {
   5:         private IInstanceResolver<TInterface> _resolver;
   6:  
   7:         public IBindingScopeIndicator To<TClass>() where TClass : class, TInterface, new() {
   8:             _resolver = new InstanceResolver<TInterface, TClass>();
   9:             return this;
  10:         }
  11:  
  12:         public TInterface Create() {
  13:             if (_resolver != null) {
  14:                 return _resolver.Resolve();
  15:             }
  16:             throw new InvalidOperationException();
  17:         }
  18:  
  19:         public void WithScope(BindingScope scope) {
  20:             // TODO
  21:             throw new NotImplementedException();
  22:         }
  23:     }
  24: }
So what did we do? We added an interface with which to indicate the binding scope. This interface has to be returned after the IBindingRule<TInterface>.To<TClass>() method, so this method was updated to return an IBindingScopeIndicator interface.

Last, the binding rule class was updated to reflect the changes in the binding rule interface, and it now also implements the IBindingScopeIndicator interface. The implementation of the method IBindingScopeIndicator.WithScope(BindingScope) will be done later.

We can now update the unittests, and add a failing unittest for the singleton scope.
   1: using Microsoft.VisualStudio.TestTools.UnitTesting;
   2: using MyDI;
   3:  
   4: namespace MyDI_Tests {
   5:     [TestClass]
   6:     public class FactoryTests {
   7:         [TestMethod]
   8:         public void Test_normal_type_correct() {
   9:             Factory.Clear();
  10:             Factory.Bind<ICommunicator>().To<WirelessCommunicator>();
  11:             var test = Factory.ResolveInstance<ICommunicator>();
  12:             Assert.IsNotNull(test);
  13:             Assert.AreEqual(typeof(WirelessCommunicator), test.GetType());
  14:         }
  15:  
  16:         [TestMethod]
  17:         public void Test_normal_scope_check() {
  18:             Factory.Clear();
  19:             Factory.Bind<ICommunicator>().To<WirelessCommunicator>();
  20:             var test1 = Factory.ResolveInstance<ICommunicator>();
  21:             var test2 = Factory.ResolveInstance<ICommunicator>();
  22:             Assert.IsNotNull(test1);
  23:             Assert.AreEqual(typeof(WirelessCommunicator), test1.GetType());
  24:             Assert.IsNotNull(test2);
  25:             Assert.AreEqual(typeof(WirelessCommunicator), test2.GetType());
  26:             Assert.AreNotEqual(test1, test2);
  27:         }
  28:  
  29:         [TestMethod]
  30:         public void Test_singleton_scope_check() {
  31:             Factory.Clear();
  32:             Factory.Bind<ICommunicator>().To<WirelessCommunicator>().WithScope(BindingScope.Singleton);
  33:             var test1 = Factory.ResolveInstance<ICommunicator>();
  34:             var test2 = Factory.ResolveInstance<ICommunicator>();
  35:             Assert.IsNotNull(test1);
  36:             Assert.AreEqual(typeof(WirelessCommunicator), test1.GetType());
  37:             Assert.IsNotNull(test2);
  38:             Assert.AreEqual(typeof(WirelessCommunicator), test2.GetType());
  39:             Assert.AreEqual(test1, test2);
  40:         }
  41:     }
  42: }
The first two unittests pass, the last one fails.

image

We now have to update the InstanceResolver class to do something with the given binding scope.

To prevent our InstanceResolver class from cluttering up, I am going to define an interface called IScopeHandler<TClass>. The purpose of this interface is to, obviously, handle the scope of a binding rule. Therefore we will have implementations for every binding scope, NormaleScopeHandler, SingletonScopeHandler, ThreadScopeHandler, and so forth. In this post I will only show the class for the first three binding scopes, the rest will be in the code that can be downloaded at the bottom of the post.

Lets define the scope handler interface, and create the scope handler classes.
   1: namespace MyDI {
   2:     public interface IScopeHandler<TClass> where TClass : class, new() {
   3:         TClass GetInstance();
   4:     }
   5: }
   1: namespace MyDI.ScopeHandlers {
   2:     public class NormalScopeHandler<TClass> : IScopeHandler<TClass> where TClass : class, new() {
   3:         public TClass GetInstance() {
   4:             return new TClass();    
   5:         }
   6:     }
   7: }
   1: using System;
   2:  
   3: namespace MyDI.ScopeHandlers {
   4:     public class SingletonScopeHandler<TClass> : IScopeHandler<TClass> where TClass : class, new() {
   5:         private Lazy<TClass> _value = new Lazy<TClass>(() => new TClass(), true);
   6:  
   7:         public TClass GetInstance() {
   8:             return _value.Value;
   9:         }
  10:     }
  11: }
   1: using System;
   2:  
   3: namespace MyDI.ScopeHandlers {
   4:     public class ThreadScopeHandler<TClass> : IScopeHandler<TClass> where TClass : class, new() {
   5:         [ThreadStatic]
   6:         private static TClass _value = null;
   7:  
   8:         public TClass GetInstance() {
   9:             if (_value == null)
  10:                 _value = new TClass();
  11:             return _value;
  12:         }
  13:     }
  14: }
So what is exactly happening here?

Interface IScopeHandler<TClass> This interface defines a scope handler. A scope handler has only one method, which is to get an instance of the class-type.

Class NormalScopeHandler<TClass> This class creates an object every time the method GetInstance() is called.

Class SingletonScopeHandler<TClass> This class creates an object once, and uses it every time the method GetInstance() is called.

Class ThreadScopeHandler<TClass> This class has a field with a ThreadStatic attribute, and that field is used to hold an object per thread, and that object is used every time the method GetInstance() is called. Logically this binding scope is very similar to the singleton binding scope. The implementation is a bit different though.

Now that we have scope handlers, we need a handy place to get scope handler instances. For this, a scope handler factory is created.
   1: using System;
   2: using MyDI.ScopeHandlers;
   3:  
   4: namespace MyDI {
   5:     public static class ScopeHandlerFactory {
   6:         private const string SCOPEHANDLERNOTREGISTEREDEXCEPTIONMESSAGE = "The specified scope handler was not registered.";
   7:  
   8:         public static IScopeHandler<TClass> GetScopeHandler<TClass>(BindingScope scope) where TClass : class, new() {
   9:             switch (scope) {
  10:                 case BindingScope.Normal:
  11:                     return new NormalScopeHandler<TClass>();
  12:                 case BindingScope.Singleton:
  13:                     return new SingletonScopeHandler<TClass>();
  14:                 case BindingScope.Thread:
  15:                     return new ThreadScopeHandler<TClass>();
  16:                 case BindingScope.WebRequest:
  17:                     return new WebRequestScopeHandler<TClass>();
  18:                 case BindingScope.WebSession:
  19:                     return new WebSessionScopeHandler<TClass>();
  20:                 case BindingScope.WebCache:
  21:                     return new WebCacheScopeHandler<TClass>();
  22:             }
  23:             throw new InvalidOperationException(SCOPEHANDLERNOTREGISTEREDEXCEPTIONMESSAGE);
  24:         }
  25:     }
  26: }
Now we’re getting somewhere. We have scope handlers, and a class to get them. Now we can update the InstanceResolver class. And we can do that very easily.
   1: namespace MyDI {
   2:     class InstanceResolver<TInterface, TClass> : IInstanceResolver<TInterface> where TClass : class, TInterface, new() {
   3:         private IScopeHandler<TClass> _scopeHandler = ScopeHandlerFactory.GetScopeHandler<TClass>(BindingScope.Normal);
   4:  
   5:         public TInterface Resolve() {
   6:             return _scopeHandler.GetInstance();
   7:         }
   8:  
   9:         public void SetScope(BindingScope scope) {
  10:             _scopeHandler = ScopeHandlerFactory.GetScopeHandler<TClass>(scope);
  11:         }
  12:     }
  13: }
A default normal scope handler is retrieved when the instance resolver is instanced. When necessary, it is replaced with another scope handler based on the given binding scope.

Lastly, we can update the BindingRule class to implement the method SetScope().
   1: using System;
   2:  
   3: namespace MyDI {
   4:     class BindingRule<TInterface> : IBindingRule<TInterface>, IBindingScopeIndicator {
   5:         private IInstanceResolver<TInterface> _resolver;
   6:  
   7:         public IBindingScopeIndicator To<TClass>() where TClass : class, TInterface, new() {
   8:             _resolver = new InstanceResolver<TInterface, TClass>();
   9:             return this;
  10:         }
  11:  
  12:         public TInterface Create() {
  13:             if (_resolver != null) {
  14:                 return _resolver.Resolve();
  15:             }
  16:             throw new InvalidOperationException();
  17:         }
  18:  
  19:         public void WithScope(BindingScope scope) {
  20:             _resolver.SetScope(scope);
  21:         }
  22:     }
  23: }
Now all unittests are passing again. Excellent!

image

To add the other binding scopes, all we have to do is write the specific scope handler class, and add it to the factory.

The code can be downloaded here.

I’m not sure what will be next, maybe constructor-injection, but not everyone thinks this is a good thing.