Introducing Windsor Dependency Injection container into Umbraco application.
IoC
When I first saw the Dependency Injection pattern used in a source code, I did not understand what I was looking at. For a beginner, it just seemed as an unnecessary complication made be some senior developer, to make my life even harder. Passing some objects into the service just to call their methods. Making a complicated constructor to pass 15 other dependencies. But I was young and some time had to pass before I had the aha moment and finally understood what it is all about. After that, it all made perfect sense. Instead of creating lot of nested code, that is tightly coupled, with static method, you just pass required dependencies into the object. With that, you can create the code in the way that will allow you to focus of writing and testing only one functionality at a time. You can easily mock other dependencies that are not relevant at the moment. It introduces a clarity in the code.
Umbraco and IoC
Umbraco documentation states as following:
We don’t use IoC in the Umbraco source code. This isn’t because we don’t like it or don’t want to use it, it’s because we want you as a developer to be able to use whatever IoC framework that you would like to use without jumping through any hoops. With that said, it means it is possible to implement whatever IoC engine that you’d like!
With that in mind, I would like to show you how to add the Windsor Dependency Injection container into an Umbraco web application as an example.
Adding IoC into Umbraco
First step to add Windsor Container is to create class that extends UmbracoApplication class. This gives as ability to override method OnApplicationStarted. In this method we can replace both default MVC and WebApi Dependency Resolvers.
_lifeTimeScope = new WindsorWebApiDependencyScope( _container, _mvcDependencyResolver, _webApiDependencyResolver); }
public object GetService(Type serviceType) { return _lifeTimeScope.GetService(serviceType); }
public IEnumerable<object> GetServices(Type serviceType) { return _lifeTimeScope.GetServices(serviceType); }
public System.Web.Http.Dependencies.IDependencyScope BeginScope() { return new WindsorWebApiDependencyScope(_container, _mvcDependencyResolver, _webApiDependencyResolver); }
public void Dispose() { } }
Constructor in WindsorDependencyResolver takes three parameters, first is the Windsor container, second one is the default MVC Dependency Resolver and the third is the default WebApi Dependency Resolver. The latter is used to first check if the service is not registered there.
The BeginScope method returns adapter class of Windsor container for WebAPI IDependencyScope. It allows to restrict service lifetime to a scope. It can be useful for situation when you don’t want to create singletons and also you don’t want service to be created every time it is being referenced. For example, we can create a scope for a http request and allow all methods/classes to use same instances during one request.
To register custom services you must call the Register method on the container. The Component class is a static class that serves as a Factory class and allows to connect interface with it’s concrete implementation. Also we need can define the lifetime of the registered class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicstatic IWindsorContainer Initialize() { var container = new WindsorContainer();