For a specific controller, make Windsor creates instances of different classes.

I use the S # arp architecture, which uses Windsor Castle for IoC. Now I have a new controller, which, unlike all other project controllers, needs a different implementation of the same interfaces. That is, all controllers use ProductsRepository: IProductsRepository as an implementation, but the new one must use SpecificProductsRepository.

How to configure it for automatic recognition and control? Either in a pure Windsor way, or using ASP.NET MVC help (for example, in custom factory controllers).

OK looks like I need subcontainers. Search is still.

+3
source share
2

Windsor.

. :

container.Register(Component.For<IProductsRepository>
                     .ImplementedBy<ProductsRepository>()
                     .Named("defaultProductsRepository"),
                   Component.For<IProductsRepository>
                     .ImplementedBy<SpecificProductsRepository>()
                     .Named("specificProductsRepository"));

, ProductsRepository. :

container.Register(Component.For<NewController>()
     .ServiceOverrides(ServiceOverride
          .ForKey("productsRepository")
          .Eq("specificProductsRepository"));

.

: AllTypes, , . :

container.Register(AllTypes.[how you used to].Configure(c => c.Named(GetKey(c)));

GetKey . - :

public string GetKey(ComponentRegistration registration)
{
    return registration.Implementation.Name;
}
+6

, , , ... , .

     // create subcontainer with specific implementation
     var mycontainer = new WindsorContainer();
     mycontainer.Register(AllTypes.Pick()
        .FromAssemblyNamed("My.Data")
        .WithService.FirstInterface()
        .Where(x => x.Namespace == "My.Data.Custom")
        .Configure(x => x.LifeStyle.Is(LifestyleType.PerWebRequest)));
     container.AddChildContainer(mycontainer);

     ControllerBuilder.Current.SetControllerFactory(new ExtendedControllerFactory(
        new Dictionary<string, IWindsorContainer> { {"", container}, {"Lm", mycontainer} }));

factory . , Release (controller) , , . , - ( HttpContext), BaseController, ..

0

All Articles