dependency-injection - 温莎城堡 : OnCreate for BaseOnDescriptor

标签 dependency-injection castle-windsor ioc-container

正如您可能知道的那样, “OnCreate”方法用于 ComponentRegistration 类,由 Component.For<> 调用创建。 为什么Classes.FromAssembly缺少这个功能? 为什么我们不能对指定命名空间中的每个类都使用 OnCreate。 有解决办法吗?

我想按如下方式完成:

Classes
  .FromThisAssembly()
  .InSameNameSpaceAs<TInterface>()
  .OnCreate<TInterface>( instance => instance.Method());

最佳答案

Classes 允许您根据约定注册组件,因此它不处理单个组件,而是处理组件集合;您无法使用流畅的界面单独配置它们。但是您可以使用Configure方法让您可以操作每个组件来满足您的需求。

Classes
  .FromThisAssembly()
  .InSameNameSpaceAs<TInterface>()
  .Configure(c => {
       c.OnCreate( instance => instance.Method())
  });

还有 ConfigureForConfigureIf 方法,可让您过滤要处理的约定中的组件。

关于dependency-injection - 温莎城堡 : OnCreate for BaseOnDescriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26142520/

相关文章:

dependency-injection - 如何使用 Ninject 处理具有静态方法的类?

dependency-injection - Wiredep Gulp 依赖注入(inject)不起作用

c# - 我应该使用 Unity Config 文件还是代码来注册类型和实例?

java - 通过注解进行依赖注入(inject)

php - ZF2 : Dependency injection done the proper way

unit-testing - Windsor 和 Rhino 的自动模拟容器

caSTLe-windsor - 如何配置CaSTLe Windsor以根据提供给Resolve()的参数(“名称”除外)动态选择提供程序

c# - 使用 CaSTLe Windsor 在开始新范围时我需要一个对象的新实例

c# - 为什么每次都需要调用BuildServiceProvider获取依赖?

c# - 温莎城堡使用 FactoryMethod 和 LifestyleTransient