Igor Brejc
Igor Brejc

Reputation: 19004

Windsor Castle: Hooking up to container's resolving and releasing mechanism

I'm trying to implement automatic registration of my listeners to a singleton event aggregator when listeners are created by the IoC container - basically what Jeremy D. Miller is doing, but with Castle instead of StructureMap.

So I want to be able to "intercept" Windsor's object creation mechanism and, if the object supports the marker interface (let's say IListener), call the Subscribe method to an EventAggregator (which is also registered in the container) to make the newly created object a subscriber to events. Also, before the object instance has been released by the container, I want to be able to unsubscribe it.

I'm a little bit confused about what mechanism in Windsor Castle I should use to achieve something like this? I started looking at IInterceptor interface, but it seems to intercept all calls to the object, which is not what I really need (and want to avoid for performance reasons).

Upvotes: 0

Views: 545

Answers (2)

Gregor Slavec
Gregor Slavec

Reputation: 4904

You could also use OnCreate like this:

container.Register(
        Component.For(typeof (Foo)).OnCreate(
          (k, c) => {
            // ...
            eventAggregator.Subscribe(c);
            // ...
          }));

Upvotes: 0

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99750

IKernel exposes various events like ComponentCreated and ComponentDestroyed which you can use to build that. There are many samples on the web.

Otherwise you could just use the event wiring facility, but it's not convention based.

Upvotes: 1

Related Questions