我有以下的類,它暴露了一個公共的工廠方法,而不是一個公共構造:通過.UsingFactoryMethod解決的瞬態依賴性的生命週期是什麼,但沒有使用內核明確解決?
public class SomeDependency
{
private readonly string _someValue;
private SomeDependency(string someValue)
{
_someValue = someValue;
}
public static SomeDependency CreateSomeDependency(string someValue)
{
return new SomeDependency(someValue);
}
}
我在容器中配置它有一過性的生活方式,要通過靜態方法創建:
Container.Register(Component.For<SomeDependency>()
.UsingFactoryMethod(k => SomeDependency.CreateSomeDependency("a value"))
.LifestyleTransient());
此依賴關係注入的所有組件也被配置爲瞬態的。
當依賴於它的對象超出範圍或者這個依賴關係未能被處置並使所有事物都停滯不前時,容器是否會處置這種依賴關係?
在圖案A小細節:組件旨在是公開可用,但沒有公開施工的由於構建它需要不兼容的類型。通過使用靜態工廠方法可以獲得對象的實例。類型化的工廠不工作,因爲我試圖保持構造函數爲私有。因爲我希望能夠以分離使用DI具有消耗代碼調用靜態方法變得有問題的對象。最終,這歸結於容器的實現細節。 – moarboilerplate