Working with Interfaces Part Three – Windsor


Castle Windsor is an Inversion of Control container which uses interfaces as a key concept. When working in the manner I described in my previous articles on interfaces, you get a decoupled application but may end up with a lot of “wiring-up” – instantiating interface implementations then passing them into your classes.

For example, a Controller may often demand an implementation of ILogger, IAuthenticationService, IRepository and IPaymentGateway, which would require a fair bit of code to wire up. Windsor removes the wire up burden from the developer. Using XML configuration, you can easily swap out one implementation for another:

public interface ISport{}<br>public class Football : ISport{}<br><br><component id="default.sport"<br>	service="ISport, WindsorTest"<br>	type="Football, WindsorTest"/>

In this example, we use the component element to wire up the Football implementation for any usage of ISport. For Windsor to provide this ISport implementation to your consuming classes, the consumer needs to be in the container too:

public class Consumer<br>{<br>	// Parameter injection<br>	public ISport Sport{ get; set; }<br>	// Constructor injection<br>	public Consumer(ISport sport){} <br>}<br><br><component id="some.consumer" type="Consumer, WindsorTest"/>

So now Consumer will have the configured ISport passed to its constructor or the Sport parameter, depending on your preference. We could also have an ISport parameter on Consumer and Windsor would be able to set it with the configured implementation of ISport.

Using a system like Windsor is a logical extension of the practises I’ve talked about before. It gives you the flexibility of interfaces without the burden of having to manually instantiate and pass through all of the implementations your classes may depend upon. When thinking about Domain Driven Design, you can use Windsor to provide Services to your application, allowing it to consume independent components and create one awesome whole. Full Windsor documentation can be found on the Castle website, and I’ve also written about Windsor on my personal blog.

Working with Interfaces Part Two – Decoupling