How to overwrite a component with castle windsor?

Lars Corneliussen picture Lars Corneliussen · Mar 18, 2009 · Viewed 11.8k times · Source

I want to redefine an (default) implementation in a given windsor-container. Is that what OverWrite is for? Doesn't work, though.

container.Register(
                    Component.For<IServiceOperationAuthorization>()
                            .OverWrite()
                            .Instance(_authorization)
                    );

Any other Ideas?

cheers, Lars

Answer

Peter Munnings picture Peter Munnings · May 30, 2013

You could very simply do this in the place you need to override the default implementation. This is an example from our integration tests. Both implementations are now registered but your code will use the default one, which is the one you've just registered. Works like a bomb and doesn't have any impact on the rest of the application:

        var sendMailStub = MockRepository.GenerateStub<ISendMail>();
        _container.Register(
            Component
                .For<ISendMail>()
                .Instance(sendMailStub)
                .IsDefault()
            );