c# - Autofac resolve IEnumerable<TComponent> for instantiated items only -
does autofac provide me way "active" instances of component? following works:
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 = view in viewprovider() //this me active views (not available implementations) !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; } }
not out of box i'm afraid - you'll need come scheme scenario. might able use onactivated()
, onrelease()
events keep own registry of active views:
builder.registertype<viewregistry>() .as<iviewregistry>() .singleinstance(); builder.registertype<myview>() .onactivated(e => e.context.resolve<iviewregistry>().add(e.instance)) .onrelease(e => { e.instance.dispose(); e.context.resolve<iviewregistry>().remove(e.instance)) });
Comments
Post a Comment