oguzh4n
oguzh4n

Reputation: 682

Windsor nested resolving

I have services as follows:

class Order

interface IOrderService
    void SendOrder(Order order)
class SmsOrderService:IOrderService(constructor ISmsService)
class FaxOrderService:IOrderService
class MailOrderService:IOrderService

interface ISmsService
    void SendSms(string message,string phone)
class XSmsService
class YSmsService(constructor YSmsServiceConfiguration)

class YSmsServiceConfiguration
    string Username
    string Password

class OrderManager(constructor IOrderService)
    void SendOrderAndLog(Order order)

I need use OrderManager with all of IOrderService implemented class.

My windsor config is:

static void Main(string[] args)
    {
            WindsorContainer container = new WindsorContainer();
            // OrderManager implementations
            container.Register(Component.For<OrderManager>()
                                   .ImplementedBy<OrderManager>()
                                   .Named("OrderManagerWithSmsService")
                                   .DependsOn(Property.ForKey<IOrderService>().Is<SmsOrderService>()));

            container.Register(Component.For<OrderManager>()
                                   .ImplementedBy<OrderManager>()
                                   .Named("OrderManagerWithFaxService")
                                   .DependsOn(Property.ForKey<IOrderService>().Is<FaxOrderService>()));

            container.Register(Component.For<OrderManager>()
                                  .ImplementedBy<OrderManager>()
                                  .Named("OrderManagerWithMailService")
                                  .DependsOn(Property.ForKey<IOrderService>().Is<MailOrderService>()));

            // IOrderService implementations
            container.Register(Component.For<IOrderService>()
                                   .ImplementedBy<SmsOrderService>());
            container.Register(Component.For<IOrderService>()
                                   .ImplementedBy<FaxOrderService>());
            container.Register(Component.For<IOrderService>()
                                   .ImplementedBy<MailOrderService>());

            // ISmsService implementations
            container.Register(Component.For<ISmsService>()
                .ImplementedBy<YSmsService>()
                .DependsOn(Property.ForKey<YSmsServiceConfiguration>().Eq(GetConfiguration()))
                .Named("YSmsService")
                .LifestylePerThread());

            container.Register(Component.For<ISmsService>()
                .ImplementedBy<XSmsService>()
                .Named("XSmsService")
                .LifestylePerThread());

            var manager = container.Resolve<OrderManager>("OrderManagerWithSmsService");
            manager.SendOrderAndLog(new Order()
                                       {
                                           ProductName = "New York Steak"
                                       });
            manager.SendOrderAndLog(new Order()
                                    {
                                        ProductName = "Red Wine"
                                    });

            Console.Read();
    } 

How can i use YSmsService in OrderManager using effective windsor configuration?

like this: container.Resolve("SmsOrderService(YSmsService)"); // parse key for determinate constructor parameters

not like this:

    var manager = container.Resolve<OrderManager>(new
    {
        orderService = container.Resolve<IOrderService>( new
                {
                    service = container.Resolve<ISmsService>("YSmsService")
                })
    });

Upvotes: 0

Views: 355

Answers (1)

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

This is called service override and you're already using it. What's the problem again?

Upvotes: 1

Related Questions