How to get a list of instances that were currently instances of some dependency in Castle Windsor?

Suppose I have a component Barthat makes Fooand notifies of this call a method FooHappenedfor all services that implement the interface IFooConsumer.

I can write Barlike this

class Bar
{
    public Bar(IEnumerable<IFooConsumer> fooConsumers) { ... }

    public void Foo()
    {
        // foo-ing
        foreach (var f in _fooConsumers) f.FooHappened();
    }
}

It will work, but the instance Barwill create all possible IFooConsumers. What should I do if I need to notify only those IFooConsumerthat exist at the moment when it Foohappened?

Is there any way to get some kind of tracker that knows about all instances of the instance IFooConsumer?

, , IWindsorContainer.Kernel.ComponentCreated, , - ? , , ?

+4
2

, , , . Winsor Caliburn.Micro. , , . Bar, , (, IEventAggregator) . , , . , IHandle, . .

- , .

class EventRegistrationFacility : AbstractFacility
{
    private IEventAggregator _eventAggregator;
    protected override void Init()
    {
        Kernel.ComponentCreated += ComponentCreated;
        Kernel.ComponentDestroyed += ComponentDestroyed;
    }

    void ComponentCreated(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IHandle)) return;
        if (_eventAggregator == null) _eventAggregator = Kernel.Resolve<IEventAggregator>();
        _eventAggregator.Subscribe(instance);
    }

    void ComponentDestroyed(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IHandle)) return;
        if (_eventAggregator == null) return;
        _eventAggregator.Unsubscribe(instance);
    }

}

=== ====

, :

public interface IBouncer {
    IEnumerable<IFooConsumer> WhoIsInside {get;}
    void WelcomeTo(IFooConsumer consumer);
    void EscortOut(IFooConsumer consumer);
}

public class Bouncer {
    private IList<IFooConsumer> _inside {get;}
    void WelcomeTo(IFooConsumer consumer) {
        _inside.Add(consumer);
}
    void EscortOut(IFooConsumer consumer);
        _inside.Remove(consumer);
    }
    IEnumerable<IFooConsumer> WhoIsInside {
        get {
            return _inside;
        }
    }



public Consumer: IFooConsumer {
    FooHappened() {
        // Do something.
}
    // no need to implement constructor/dispose
}


class Bar
{
    public Bar(IBouncer bouncer) { ... }

    public void Foo()
    {
        // foo-ing ==> alernatively create a function on Bouncer that does this. And keep WhoIsInside private.
        foreach (var f in bouncer.WhoIsInside) f.FooHappened();
    }
}


class BouncerRegistrationFacility : AbstractFacility
{
    private IBouncer _bouncer
    protected override void Init()
    {
        Kernel.ComponentCreated += ComponentCreated;
        Kernel.ComponentDestroyed += ComponentDestroyed;
    }

    void ComponentCreated(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IFooConsumer)) return;
        if (_bouncer == null) _bouncer = Kernel.Resolve<IEventAggregator>();
        _bouncer.WelcomeTo(instance);
    }

    void ComponentDestroyed(Castle.Core.ComponentModel model, object instance)
    {
        if (!(instance is IFooConsumer)) return;
        if (_bouncer == null) return;
        _bouncer.EscortOut(instance);
    }
}

, , FooConsumers / . FooConsumers, . , / / .

P.S. .

+2

, , Castle Windsor, ; , , , .

IBouncer. singleton IFooConsumer, ( , )

public interface IBouncer {
     IEnumerable<IFooConsumer> WhoIsInside {get;}
     void WelcomeTo(IFooConsumer consumer);
     void EscortOut(IFooConsumer consumer);
}

public Consumer: IFooConsumer {
    public Consumer(IBouncer bouncer) {
        bouncer.WelcomeTo(this);
    }

    public Dispose() {
        bouncer.EscortOut(this); // dispose pattern ommitted
    }
}

, IFooConsumer Bar, IBouncer , .

class Bar
{
    public Bar(IBouncer bouncer) { ... }

    public void Foo()
    {
        // foo-ing
        foreach (var f in bouncer.WhoIsInside) f.FooHappened();
    }
}
+1

All Articles