Thursday, 8 March 2012

Updating Castle Windsor from v2.5 to 3.0

Late last year the Castle Windsor project released a major update to some of their components - from version 2.5 to 3.0.
There are some breaking changes (most of which are documented in the BreakingChanges.txt files for Core and Windsor), so I thought I’d highlight a few issues that may be more common.

ILogger

The logging abstraction interface has been simplified, with a number of the overloaded methods removed. Whereas before you might have called ILogger.Debug("A message {0}", arg1), you now would use ILogger.DebugFormat() instead.

Component Registration

In 2.5 you could use the .Unless() method to add a condition (usually to skip registration if component already exists). This method has been replaced with the equivalent OnlyNewServices() method.

IHandler

This used to have a 'Service' property. Now refer to the IHandler.ComponentModel property which has a Services collection property.

Facilities

Previously, the IWindsorContainer.AddFacility() method required a string 'key' parameter. This is has been removed.

IHandler.AddCustomDependencyValue

This method has been removed, but you can now use the new Depends() method instead during registration. eg.
Component.For<IService>()
.ImplementedBy<Service>()
.DependsOn(
    Dependency.OnValue("parameterName", "CustomValue")
);

IKernel.RemoveComponent

This method has been removed. There is now no way to remove a component.

Compatibility

NServiceBus 2.x

NServiceBus 3 is compatible with Castle 3.0, but if you're still using NServiceBus 2 and don't want to upgrade, you'll need to update your WindsorObjectBuilder class. Have a look at the source code in NServiceBus 3 and adapt it to your implementation - https://github.com/NServiceBus/NServiceBus/blob/master/src/impl/ObjectBuilder/ObjectBuilder.CastleWindsor/WindsorObjectBuilder.cs

NHibernate

As of NHibernate 3.2, there is no longer a requirement to have a Castle-compatible bytecode provider.
The old NHibernateIntegration facility is not compatible with Castle 3.0 or NHibernate 3.2.
There is an alternate facility- 'Castle NHibernate' that leverages FluentNHibernate for configuration. This has been patched to work with Castle 3.0 but not NHibernate 3.2 (partly because FluentNHibernate hasn't been updated for 3.2). It also depends on Castle.Transactions, which ironically whilst at version 3.0 isn't actually compatible with Castle 3.0 either.
So for the meantime if you want to use NHibernate with Castle 3.0 you're going to have to get your hands dirty and update those dependant projects yourself.
I've done some of this on forks of these projects, and will update this post to the source repositories when they are available.

No comments: