3

I'm just starting with Windsor, so please be gentle :) I have a scenario where I want to be able to override/replace components placed inside a windsor container. Read on ...

In my prod code, I want to be able to register a component which implements a base class, and use a container to resolve the implementer. So far, using

container.Register(Component.For<LoggerBase>().ImplementedBy<DebugLogger>());

and

container.Resolve<LoggerBase>();

In my tests, I'd like to add a stub/mock implementation to override the "DebugLogger" implementation so that when my prod code calls container.Resolve<LoggerBase>(); it gets the overridden implementation.

Any pointers would be welcome!

Jason Hyland
  • 744
  • 1
  • 8
  • 21
  • Version 3 now implements the `IsDefault` member, allowing you to solve this problem. http://stackoverflow.com/questions/9253388/in-castle-windsor-3-override-an-existing-component-registration/9254043#9254043 – Andrew Shepherd Feb 13 '12 at 02:31

1 Answers1

4

Instead of chasing down that path, you should rather use different container instances for different scenarios. Each instance could be configured differently. That's the whole point of a DI Container.

However, in general DI Containers should not be used for unit testing.

Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Mark, thanks for the suggestion, I hadn't thought about that approach. The only downside I guess is if I have a large number of registrations in the container and you want to override a single component. This does however provide a good solution - thanks again! – Jason Hyland Nov 06 '09 at 15:06
  • If you need multiple containers that are almost identical, you can create derived containers and then use the Template Method design pattern to override their configurations where they differ. – Mark Seemann Nov 06 '09 at 15:26