views:

1214

answers:

1

From what I've read about Windsor/Microkernel it is in theory possible to do everything that you can do using xml files with code. As a matter of fact - and please correct me if I'm wrong - it seems like the major contribution of the Windsor layer is to add xml configuration for things Microkernel can already do.

However, I have been struggling lately with finding out how to implement some slightly more complicated functionality in code though (ie. how to assign a default constructor argument value). Now while I am going to use xml in my production release, I am registering components in code for my tests and this is getting to be quite problematic. This is not helped by the unfortunate state of their documentation and the fact that the only articles I can find focus on xml registration.

Does anybody know a source which lists how to register things in code (preferably with the xml equivalent)? Baring the existence of that, does anyone simply know of an open source/sample project where there is significant non-xml use of Castle Windsor/Microkernel?

+6  A: 

I've always found looking at the unit test the best way to learn how to use an open source project. Castle has a fluent interface that will allow you to do everything in code. From the WindsorDotNet2Tests test case:

[Test]
 public void ParentResolverIntercetorShouldNotAffectGenericComponentInterceptor()
 {
  WindsorContainer container = new WindsorContainer();
  container.AddComponent<MyInterceptor>();

  container.Register(
   Component.For<ISpecification>()
    .ImplementedBy<MySpecification>()
    .Interceptors(new InterceptorReference(typeof(MyInterceptor)))
    .Anywhere
   );
  container.AddComponent("repos", typeof(IRepository<>), typeof(TransientRepository<>));

  ISpecification specification = container.Resolve<ISpecification>();
  bool isProxy = specification.Repository.GetType().FullName.Contains("Proxy");
  Assert.IsFalse(isProxy);
 }

And for more, check out the the ComponentRegistrationTestCase and AllTypesTestCase

There is also a DSL for doing it, this is my prefered option, as it really simplifies things and offers alot of easy extensibility. The DSL is called Binsor, which you can read more about here: http://www.ayende.com/Blog/archive/7268.aspx But again, the best place for infor is the Unit Tests. This is a code example of whats possible with binsor:

for type in AllTypesBased of IController("Company.Web.Controller"):
    component type

Those two lines will register ever type that inherits the IController interface into the container :D

Chris Canal
Thank thank you for the wonderful response! I will have to dig into it. Where is the .Register method on the WindsorContaienr coming from? I don't see it via my intellisense. Is it an extension method?
George Mauer
Woah...the Castle repository is down "Repository has been disabled (by administrator)."
George Mauer