Castle Windsor: How do I inject all implementations of interface into a ctor?

前端 未结 4 2181
臣服心动
臣服心动 2020-12-28 14:59

I\'ve written an interface which is implemented by multiple classes. I want to write a Service class which will have all the registered implementations injected into its cto

相关标签:
4条回答
  • 2020-12-28 15:12

    The answers here are all correct but I just wanted to add a little extra wrinkle to it: You have to add the sub-resolver BEFORE you register your components.

    This will work:

    container = new WindsorContainer();
    container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));
    container.Register(
        Component.For<IVehicle>().ImplementedBy<Car>(),
        Component.For<IVehicle>().ImplementedBy<Truck>(),
        Component.For<IVehicle>().ImplementedBy<Motorbike>()
    );
    

    This will NOT work

    container = new WindsorContainer();
    container.Register(
        Component.For<IVehicle>().ImplementedBy<Car>(),
        Component.For<IVehicle>().ImplementedBy<Truck>(),
        Component.For<IVehicle>().ImplementedBy<Motorbike>()
    );
    container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));
    
    0 讨论(0)
  • 2020-12-28 15:13

    Found this answer useful though I was still missing how to register all the implementations of a given interface.

    Hope this helps!

    container.Register(
                Classes.FromAssemblyContaining<IVehicle>()
                    .BasedOn<IVehicle>().WithService.FromInterface()
                );
    
    0 讨论(0)
  • 2020-12-28 15:17

    You have to use CollectionResolver. Check this Castle Windsor FAQ:

    Windsor, by default when you have dependency on IFoo[], IEnumerable or IList will check if you have a component registered for that exact type (array or list of IFoo), not if you have any components registered for IFoo (array of components, is not the same as a component which is an array). You can change the behavior to say "When you see array or list of IFoo just give me all IFoos you can get" you use CollectionResolver.

    Direct link to Castle Resolvers: Resolvers.

    0 讨论(0)
  • 2020-12-28 15:27

    I know this has already been answered, but I thought an example of how to add the CollectionResolver would be useful, so here it is.

    Call AddSubResolver before registering the components in the container, e.g.

    container = new WindsorContainer();
    container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));
    

    Register the components as normal:

    container.Register(
        Component.For<IVehicle>().ImplementedBy<Car>(),
        Component.For<IVehicle>().ImplementedBy<Truck>(),
        Component.For<IVehicle>().ImplementedBy<Motorbike>()
    );
    
    0 讨论(0)
提交回复
热议问题