Specifying Specific Dependencies with Windsor Fluent Registration
So it's become apparent that the documentation (actually concrete examples in my personal opinion) for Windsor Container are a little lacking in the area of showing people how to indicate a specific component instance registration for depenency injection. At least once a week I get an email on the topic asking my "how-to" knowledge. After searching around the internets it appears to me that concrete examples on this are a bit thin for anything other than XML configuration so I figure that I'll do a solid for you and show just how simple the fluent registration code is.
Say we have a component registered (MyAwesomeComponent) that takes a constructor dependency on IMyOtherComponent. However you have 2 instances of this interface registered, say MyOtherComponent1 and MyOtherComponent2. Now if you do nothing then Windsor will resolve the first (or default) IMyOtherComponent registered, which is MyOtherComponent1, and supply that at build time for MyAwesomeComponent. That's great and all but what if I didn't want to inject a MyOtherComponent1 but instead MyOtherComponent2 to the MyAwesomeComponent? Well of course you can simply reverse the order of the registrations but that's error prone and frankly lame. So here's the code:
Container.Register(Component.For<MyAwesomeComponent>().DependsOn(Property.ForKey( "C2" )));
Yup, that's it! So here's to great containering away!