我正在使用Autofac和ASP.NET Core。Autofac參數化實例化,針對不同參數的不同解決方案
我的依賴是一個Reporter
:
public class Reporter {
public Reporter (bool doLogging) { DoLogging = doLogging ; }
public string DoLogging { get; set; }
// other stuff
}
我需要使用這樣的:
public class Foo
{
public Foo(Func<bool, Reporter> reporterFactory) { _reporterFactory = reporterFactory; }
private readonly Func<bool, Reporter> _reporterFactory;
}
而且我希望它來解決這樣的:
_reporterFactory(false) ---> equivalent to ---> new Reporter(false)
_reporterFactory(true) ---> equivalent to ---> new Reporter(true)
我想每個請求的相同實例(即Autofac的InstancePerLifetimeScope
),對於相同的bool
參數。當我多次撥打_reporterFactory(false)
時,我想要相同的實例。當我多次撥打_reporterFactory(true)
時,我需要相同的實例。但是這兩個實例必須彼此不同。
所以我註冊這樣的:
builder
.Register<Reporter>((c, p) => p.TypedAs<bool>() ? new Reporter(true): new Person(false))
.As<Reporter>()
.InstancePerLifetimeScope(); // gives "per HTTP request", which is what I need
然而,當我解決我得到相同的情況下,不管bool
說法:
var reporter = _reporterFactory(false);
var reporterWithLogging = _reporterFactory(true);
Assert.That(reporter, Is.Not.SameAs(reporterWithLogging)); // FAIL!
爲"Parameterized Instantiation"文檔說
不止一次地解析對象,你會得到相同的對象實例無論您傳入的參數是多少,只要傳遞不同的參數都不會破壞對生命週期範圍的尊重。
這解釋了行爲。那麼如何正確註冊?
http://docs.autofac.org/en/latest/faq/select-by-context.html#option-3-use-keyed-services有幫助嗎? – mjwills
@mjwills嗯我需要看看,我希望我想要的是可能的,我只是使用錯誤的語法或什麼。 – grokky
你的'人'似乎是一個域實體;不是應用程序_component_。 DI容器旨在構建_components_的對象圖。在構建這些對象圖的過程中,不應該存在關於它的依賴關係的歧義,它不包括使用運行時數據,如實體,DTO,視圖模型和其他數據對象。 – Steven