I renamed the method from WithParameters to DynamicParameters to avoid confusion and be consistent with Parameters method which is used for static parameters.
I just committed small new feature to Castle Windsor, that I think can nicely clean up your code. It remedies the following problem:
What if you have a component that relies on a dynamically provided primitive value? Things like current request’s Uri, or DateTime.Now?
Solution – the old way
Until now you had two choices:
- wrap the dependency in a service, like ICurrentUriProvider, or IClock and take dependency on that interface
- in the code using that service, reference the container and pull the service yourself, instead of having it injected.
Neither of these approaches is ideal. The new feature I mentioned is here to save the day. It basically lets you take the best of both approaches, without paying their price. You get to pass the parameters without additional layer of indirection, and without referencing container and pulling the service manually.
Solution – the new way
To use it, we need to use a new method on the fluent registration API: DynamicParameters, like this.
the method takes a delegate that will be invoked when the component is being resolved, but before any actual resolution, parameter matching, appropriate constructor searching etc happens. It gives you access to two things: the container itself, and the dictionary of parameters, that were passed from the call site. You can then inspect the parameters, add new, remove them, etc.
What’s more interesting – you can use it not only for primitives. You can also dynamically override components.
This test will pass.