While it's main purpose is to provide an Inversion of Control container, and allow you to develop applications that are highly cohesive yet loosely coupled, a secondary benefit of using the Castle Windsor container is how it simplifies dealing with configuration files.
Typically if you need to have some external configuration, you'd put this into your own custom configuration section and write a class which either implemented IConfigurationSectionHandler or inherited from ConfigurationSection. If you needed to change the configuration, you'd need to change these handler classes. While this is unspeakably better than parsing .ini files, it's still pretty brittle - especially if you are being agile and refactoring your code to any great extent.
What is Castle Windsor?
It's a container that figures out how to create classes, along with all their dependencies, on the fly, from a configuration file. Somewhat like Configuration Section Handlers, but it's an awful lot more. For example...
<component id="VirtualEarthTileServiceManager" type="ArcDeveloper.TileServer.Core.VirtualEarthTileServiceManager, ArcDeveloper.TileServer.Core"> <parameters> <services> <dictionary> <item key="colorado">${ColoradoVETileService}</item> <item key="tahoe">${TahoeVETileService}</item> </dictionary> </services> </parameters> </component>
This little snippet of XML defines how the VirtualEarthTileServiceManager should be created. Basically the type is instantiated, and an IDictionary containing the keys and items it depends upon is passed into the constructor. The ${somename} notation refers to other classes in the same configuration - and they use similar syntax to have their dependencies injected at instantiation.
To actually get a VirtualEarthTileServiceManager, you just use this code...
IWindsorContainer container = new WindsorContainer(new XmlInterpreter()); VirtualEarthTileServiceManager veTileManager = container.Resolve<VirtualEarthTileServiceManager>();
The container.Resolve call looks up the name (VirtualEarthTileServiceManager in this case) and creates the class, with all of it's dependencies.
Learning More
This post has just scratched the surface, so I recommend checking out some more information on Castle Windsor. There are quite a few articles on how Windsor works, but the best I found are a series of 4 written by Simone Busoli, and you can read them here: Part 1, Part 2, Part 3, and Part 4.
Since I'm also a fan of Scott Hanselman, give a listen to this podcast on Mock Objects, and this one on MonoRail and the Windsor container.
For more on the Virtual Earth Tile Service, see this previous post, or check out the ArcDeveloper project at Assembla.
I'm Dave and this is my blog. I'm usually writing about .NET Software Development, ArcGIS, or Agile Practices, but other stuff does creep in from time to time. I hope you find something of use, and feel free to contact me if you have any questions. You can also check out my profile on LinkedIn
dojo.DTSAgile.com is our technology preview / demo site. As I and my team cook up cool things we post them here.
ArcDeveloper.net is a site that hosts a set of open source projects related to ArcGIS. This includes Tile Cache for .NET (TC4N) and Feature Server for .NET (FS4N). Come over and check it out!
Assembla is a free service that provides Subversion source control, wikis and work Tracking. The ArcDeveloper project is run from here. It rocks. Check them out today.
Agilistas is a LinkedIn group focused on discussing and promoting Agile practices. Everyone is welcome to join in the conversation as we evolve the process of creating software to make it more enjoyable for all involved.