Ninject 中 Autofac 的 PropertiesAutoWired 等价物是什么
What is the Equivalent of PropertiesAutoWired of Autofac in Ninject
在 Autofac 中我们有 PropertiesAutoWired。它写在那里
如果组件是反射组件,使用PropertiesAutowired()修饰符注入属性。
看来我们应该在需要进行 属性 注入时使用它。所以我想知道在 Ninject.
的情况下会怎样
Ninject 没有与 Autofacs PropertiesAutowired()
等效的功能。取而代之的是用属性 [Inject]
标记属性 - 组件的绑定不受影响:
public class FooBar
{
// will be injected
[Inject]
public IDependency Dependency { get; set; }
// will not be injected
public IFalaffel Falaffel {get; set;
}
绑定不受影响。例如
Bind<FooBar>().ToSelf();
完全有效并且(属性)属性将被注入。
另请参阅 ninject wiki 上的 Property Injection 文档。
另外请注意,构造函数注入是首选方案。你应该只使用 属性 注入,以防你不能使用构造函数注入或其他一些特殊情况,比如你不能摆脱继承层次结构并且不想在 [= 中将构造函数参数向下传递 10 个步骤30=] 层级...
替代使用属性
如果您不想让您的代码因引用 Ninject 而变得混乱,您也可以像这样进行 属性 注入:
Bind<FooBar>().ToSelf()
.OnActivation((ctx, instance) => instance.Dependency = ctx.Kernel.Get<IDependency>());
在 Autofac 中我们有 PropertiesAutoWired。它写在那里
如果组件是反射组件,使用PropertiesAutowired()修饰符注入属性。
看来我们应该在需要进行 属性 注入时使用它。所以我想知道在 Ninject.
的情况下会怎样Ninject 没有与 Autofacs PropertiesAutowired()
等效的功能。取而代之的是用属性 [Inject]
标记属性 - 组件的绑定不受影响:
public class FooBar
{
// will be injected
[Inject]
public IDependency Dependency { get; set; }
// will not be injected
public IFalaffel Falaffel {get; set;
}
绑定不受影响。例如
Bind<FooBar>().ToSelf();
完全有效并且(属性)属性将被注入。
另请参阅 ninject wiki 上的 Property Injection 文档。
另外请注意,构造函数注入是首选方案。你应该只使用 属性 注入,以防你不能使用构造函数注入或其他一些特殊情况,比如你不能摆脱继承层次结构并且不想在 [= 中将构造函数参数向下传递 10 个步骤30=] 层级...
替代使用属性
如果您不想让您的代码因引用 Ninject 而变得混乱,您也可以像这样进行 属性 注入:
Bind<FooBar>().ToSelf()
.OnActivation((ctx, instance) => instance.Dependency = ctx.Kernel.Get<IDependency>());