Estate Digital – Tech Sitecore blog

Castle Windsor

In the previous post we made some changes to make the use of areas a little easier. In this post we be working on controller constructor dependency injection using Castle Windsor. The problem using this type of dependency injection is that Sitecore uses its own CreateControllerInstance method to instantiate the controller class. This method does not take any constructor parameters into account. In fact is just creates the controller instance passing and new object[0] as parameter. There is a way to avoid Sitecore’s CreateControllerInstance method: Just make sure this method does not receive a fully qualified type name and its assembly as value.

Let’s start writing some basic code to make sure we can even use Castle Windsor.

Now we have created a our own (Windsor)ControllerFactory which should take care of creating the controllers, a controller installer class to register the controllers to the dependency injection container and we made a Sitecore initialize pipeline processor to our ControllerFactory as inner factory of the SitecoreFactoryController.

Now publish and check out the result. Damn we got an error message. As i mentioned earlier the CreateControllerInstance method of Sitecore does not pass parameters. A solution can be changing the controller rendering item value of field “Controller” to “Demo”, but since we are using areas it is not an option because give us the multiple type error as described in the previous post. We can solve this with some new modification to the code we already wrote.