簡體   English   中英

與Ninject的[Inject]屬性一樣,autofac中的等價物是什么?

[英]Like [Inject] attribute from Ninject, what is the equivalent in autofac?

我是Autofac的新手。 我知道使用Autofac,我需要注冊這些屬性: Property Injection 我正在尋找Autofac中的東西,就像Ninject中的[Inject]屬性一樣。 像這樣:

[Inject]
public Interface Someproperty { get; set; }

這樣的事情可以用Autofac完成嗎? 任何幫助將不勝感激!

Autofac不使用屬性。 只需將要注入的屬性公開,並確保它們有一個set ,Autofac完成其余的工作。

var builder = new ContainerBuilder();

// Register the type you want the properties wired up
builder.RegisterType<YourType>().PropertiesAutowired();

// You have to register the thing you want injected to - the value of the property
// and it has to be registered to expose the type of the property. So if the property
// being injected is type `Interface` then make sure you say that here.
builder.RegisterType<AClass>().As<Interface>();

如果你真的,真的想控制哪些屬性通過屬性注入 - 我不推薦,因為將DI相關的東西與你的業務邏輯交織在一起並不是很好的做法 - 在Autofac repo中有一個工作的例子。單元測試的形式 該示例顯示了如何創建自己的自定義屬性並使用自定義Autofac屬性選擇器來僅連接具有自定義屬性的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM