Recently, I’ve had some problems with StructureMap. Mostly incomplete documentation, but also they have a habit of drastically changing namespacing, object names, removing methods, and generally changing how the thing works.
For now, my code is written using poor man’s DI so that I can keep working on it, but I don’t want to leave it that way. I took a look at Castle Windsor a while back, but never really tackled it. Now, after taking another look, I need a bit of help in doing the migration.
With StructureMap, I would derive a custom Registry class where all the mapping is done, a custom Bootstrap class which loads one or more registry classes and initializes them, and then, call Configure on the bootstrap class in Global.asax.
How does one go about setting up the matching Castle code? I also need to know how the controller factory is affected. Does Castle provide a replacement, or are we left to write our own, as with StructureMap?
For your controller factory, I would recommend using MvcContrib. There is nothing that matches the SM Registry yet, they are looking into doing something similar. We use the IWindsorInstaller interface to bundle our wiring up:
global.asax
container = new WindsorContainer();
container.Install(new DefaultComponentsInstaller());
DefaultComponentsInstaller.cs
public void Install(IWindsorContainer container, IConfigurationStore store)
{
// Component registration here like so:
container.Register(
Component.For<ModelValidatorProvider>()
.ImplementedBy<CastleModelValidatorProvider>(),
Component.For<ModelBinderDispatcher>(),
Component
.For<IBootTask<MvcBootContext>>()
.ImplementedBy<RegisterSparkViewEngineWithWebConfigSettingsBootTask>(),
Component
.For<IBootTask<MvcBootContext>>()
.ImplementedBy<RegisterRoutesBootTask>(),
Component.For<IBootTask<MvcBootContext>>()
.ImplementedBy<RegisterValidatorProviderBootTask>(),
Component
.For<IBootTask<MvcBootContext>>().
ImplementedBy<FluentMvcControllerFactoryConfigurationBootTask>()
.DependsOn(Property.ForKey("assembly").Eq(assembly)),
};
}
Also, soon the core parts of Castle will be one assembly, not sure when it’s going to be done, but it shouldn’t be too long. If you want a build of the latest trunk, checkout: http://hornget.net
This would solve a lot of a my problems I think. Here is the error I am getting at the moment.
Could not load file or assembly ‘Castle.Windsor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Now, I’ve downloaded and reference each dll present in the packages for Castle / Microkernel, MvcContrib and MvcContrib.Extras.
Everything seems ok code-wise in that nothing breaks on compile, but something still isn’t right. It appears to be a versioning error which doesn’t make sense. These are the latest binaries from each project.
I think you downloaded the latest Castle release 2.1.1 from the first link and something was buid against 2.0.0.0 and is blowing up. I suspect its MvcContrib.Castle from the Extras Binaries.
So use castle dlls from the Extra Binaries or rebuild MvcContrib.Castle against 2.1.1 …