Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite a component with castle windsor?

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

like image 907
Lars Corneliussen Avatar asked Mar 18 '09 13:03

Lars Corneliussen


2 Answers

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()
            );
like image 188
Peter Munnings Avatar answered Nov 13 '22 17:11

Peter Munnings


I agree with Krzysztof that this is usually not a good idea... However as far as I can tell OverWrite() does not overwrite the default component, it just overwrites the lifestyle defined by attribute (i.e. [Singleton]).

If you want to replace a component, you can use container.Kernel.RemoveComponent(string key) followed by the registration of the new component.

Here's an example where this does make sense.

like image 10
Mauricio Scheffer Avatar answered Nov 13 '22 17:11

Mauricio Scheffer