English 中文(简体)
卡斯尔语 Has from ninject
原标题:Castle Windsor equivalent to WhenClassHas from ninject

I m trying to convert some applications that use Ninject to Castle Windsor so that I can eliminate some dependencies.

我可以说明如何利用该集装箱实现某些Ninject功能。 风.的登记方法。

姓名(Ninject):

Kernel.Bind<ISessionProvider>().To<UnitOfWorkSessionProvider>();
Kernel.Bind<ISessionProvider>().To<ThreadSafeSessionProvider>()
  .WhenClassHas<RequireThreadSafeSession>();

这说明 Ninject,在建筑商中建造一个有ISessionProvider的班子时,使用UniOfWorkSessionProvider,除非它有属性要求ThreadSafeSession,在这种情况下,它获得一个ThreadSafeSessionProvider。

问题一——能否在温莎做到这一点? 文件令人困惑。

问题2——如何做到这一点? 似乎像有条件约束一样,在“AllTypes”组合类别中是允许的,但使用模式并不透明。

最佳回答

您可以使用登记文本中的“FactoryMethod”来使用过时的约束力并决定执行情况。 为此:


container.Register(Component.For<ThreadSafeSessionProvider>());
container.Register(Component.For<UnitOfWorkSessionProvider>());
container.Register(Component.For<ISessionProvider>().UsingFactoryMethod((x, u) => FindImpl(x, u)));
container.Register(Component.For<ClassUnderTest>());

private ISessionProvider FindImpl(IKernel kernel, CreationContext context)
{
   var classUnderTest = context.Handler.ComponentModel.Implementation;
   if (classUnderTest.HasAttribute<IsThreadSafeAttribute>())
   {
      return kernel.Resolve<ThreadSafeSessionProvider>();
   }

   return kernel.Resolve<UnitOfWorkSessionProvider>();
}

问题回答

暂无回答




相关问题
castle IOC - resolving circular references

quick question for my MVP implementation: currently I have the code below, in which both the presenter and view are resolved via the container. Then the presenter calls View.Init to pass himself to ...

Castle Windsor auto registration

I currently have the following registration set up private static void AddFrameworkComponentsTo(IWindsorContainer container) { container.AddComponent<ITypeConverter, TypeConversionFacade>();...

Authorization and Windsor

I m trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute { private readonly IUserService _userService; public MyCustomAuth(IUserService ...

How can I override a component registered in Castle Windsor?

I m just starting with Windsor, so please be gentle :) I have a scenario where I want to be able to override/replace components placed inside a windsor container. Read on ... In my prod code, I want ...

热门标签