0
autofac是否提供了一種獲取組件的所有「活動」實例的方法? 所以,下面的工作:Autofac解析IEnumerable <TComponent>僅用於實例化的項目
public class ViewManager {
public ViewManager(Func<IEnumerable<MyView>> viewProvider, Func<string, MyView> viewFactory) {
ViewProvider = viewProvider;
ViewFactory = viewFactory;
}
public Func<IEnumerable<MyView>> ViewProvider { get; private set; }
public Func<string, MyView> ViewFactory { get; private set; }
public MyView GetView(string viewId) {
var existing = from view in ViewProvider() //this would get me all the active views (not the available implementations)
where !view.IsDisposed
&& view.Id == viewId
select view;
return existing.FirstOrDefault() ?? ViewFactory(viewId);
}
}
public class MyView: IDisposable {
public MyView(string id) {
Id = id;
}
public string Id { get; private set; }
public bool IsDisposed { get; private set; }
public void Dispose() {
IsDisposed = true;
}
}
感謝尼古拉。我已經非常使用我自己的緩存來採取這樣的事情。 OnRelease僅在父容器處置時調用,還是有其他方式強制組件發佈? – Damian 2010-11-19 15:29:16
要在容器前釋放組件,您需要使用'BeginLifetimeScope()'或'擁有的' - http://code.google.com/p/autofac/wiki/OwnedInstances –
2010-11-21 22:13:35