Today I was working through some code refactoring when I came across my implementation of the Castle Windsor container exposed off of the Global.asax file. I had put it there because I needed to get it initialized during Application_Start and I wanted to put it somewhere that was easy to get at from the rest of the code. When I exposed it off of Application_Start, I did it as a property of type IWindsorContainer.
In the end I needed to be able use a different implementation of a container. Since I’m not in control of the IWindsorContainer interface, I didn’t feel comfortable creating a new implementation based on that. Instead I created my own interface and implementation. The implementation for the main production container is just an adapter wrapping the IWindsorContainer. It looked something like this:
public class WindsorDependencContainer : IDependencyContainer
Because I’m using my own interface I could go ahead and implement another simple container and make use of it. Maybe something like this:
public class SimpleDependencyContainer : IDependencyContainer
I went from having my code being tied to the Windsor implementation of an IoC container to having my application be able to implement any container that I desire. I’ve preached and preached that you should wrap everything that you’re implementing from a third party. For some reason I forgot this time and it damn near bit me in the ass.
Why this was important to me is the subject of a future blog post.