Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Castle Windsor respond to a class which implements multiple interfaces?

For example I have two interfaces: ICustomerService and IOrderService which each has a couple of functions like GetCustomer, GetOrder, etc.

I want one class to implement both interfaces: Server.

How does Castle Windsor respond to this? Is it possible in the first place? When I resolve the Server object based on one of the two interfaces, will I get the same object? What happens when I have a constructor that has both interfaces in its parameters? Will there still be one object constructed.

assuming the LifeStyle is left to its default: Singleton.

like image 745
Gerrie Schenck Avatar asked Oct 14 '22 17:10

Gerrie Schenck


1 Answers

There's no hard one-to-one mapping between a CLR type and a Windsor service or component (glossary if needed is here).

So you can have any of the following scenarios:

  • Many Components with different implementation types expose a single Service

    container.Register(
       Component.For<IFoo>().ImplementedBy<Foo1>(),
       Component.For<IFoo>().ImplementedBy<Foo2>()
    );
    
  • Many Components with same implementation type expose a single Service

    container.Register(
       Component.For<IFoo>().ImplementedBy<Foo1>(),
       Component.For<IFoo>().ImplementedBy<Foo1>().Named("second")
    );
    
  • Many Components with same implementation type expose different Services

    container.Register(
       Component.For<IFoo>().ImplementedBy<Foo1>(),
       Component.For<IBar>().ImplementedBy<Foo1>().Named("second")
    );
    
  • Single Component expose different Services

    container.Register(
       Component.For<IFoo, Bar>().ImplementedBy<Foo1>()
    );
    

As you can see, yes - it is possible, and whether or not you'll get the same instance (assuming singleton) depends on which option you chose - if both Services will be exposed by the same Component, or not.

like image 133
Krzysztof Kozmic Avatar answered Oct 18 '22 13:10

Krzysztof Kozmic