Windsor set instance of a local variable?

51 Views Asked by At

I read the following code for Windsor installer.

public class MessagingInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        var subject = new Subject<IMessage>();
        var ss = Subject.Synchronize(subject);
        container.Rgister(
            Component.For<IObserver<IMessage>>().Instance(ss.AsObserver()),
            Component.For<IObservable<IMessage>>().Instance(ss.AsObservable()),

And classes inject IObserver<IMessage> publisher, which will call publisher.OnNext(...) to push values, and other classes inject <IObservable<IMessage> source, which subscribe source.

The question is ss is a local variable in the method Install(...). How does ss be used by other classes?

1

There are 1 best solutions below

0
David Osborne On

It's not possible to use it from other classes. It's only in scope for the lifetime of the Install() method.

The instance returned from the ss.AsObservable() method call is being registered with the container, and is accessible to other classes via DI. I'm assuming that the instance returned by the AsObservable() method is not the same instance as ss?

If you want the instance ss refers to, to be accessible to other classes via DI, you would have to register that instance instead:

container.Register(Component.For<SomeType>().Instance(ss));