I think I've hit a wall on my attempt to impement NServiceBus' IContainer which is too bad because I've only got a couple failing tests remaining. :-D
There's a BuildAll method on the interface:
IEnumerable<object> BuildAll(Type typeToBuild)
I thought this was an easy match for GetAllInstances.
When the tests were failing, I started reading the docs and the rest of the web.
I take your point from here:
NServiceBus has several interfaces that can be implemented by consumers which is nice from an OCP point of view. Examples: IWantToRunWhenTheBusStarts, IMutateIncomingMessages.
I think that's great. From reading your comments, I think where the design misses the mark slightly is that calls to BuildAll/GetAllInstances should be paired with calls to RegisterAll or equivalent.
In their case, it probably works ok, because they will always call BuildAll for these things. It doesn't matter if zero, one, or many services were registered.
I can see from your perspective writing the injection framework itself, you don't want to allow Register/GetAllInstances combinations because, if there are multiple instances registered, what do you get back? It's happenstance.
You're pretty set on this and that makes sense to me.
So I've got three choices:
- Convince the NSB guys to change the interface for IContainer to have a ConfigureAll or equivalent method and then implement that for all their existing containers
- Hack something together where I "listen" to all the registrations and if I see one service registered multiple times, call RegisterAll, else Register and then test NSB to see that it actually adheres to those semantics. I'd probably need to add
a "Done Registering" method to the interface to know when I can actually do the registrations. This seems like a bad bad road.
- Abandon hope. :-D
Unless you have a magical idea of how I could make this work, I'm leaning towards #3.
Complete side note: in the course of figuring out what was going on, I read the docs about the decorators and interceptors. Very very nice. Thanks for this project.