c# - WeakReference, WeakEvents: Usefulness for dynamically loaded/unloaded server modules, DLLs? -
i thinking whether weakreferences , weakevents suitable in case of server modules interface. design?
but question performance, there need invoke in weakevent pattern , accessing weakreference, perhaps too
update: details
server:
- has module manager,
- can load/unload dll's implementing classes shared interface, imodule,
- so module manager creates , keeps instances of imodule,
modules:
- have name or unique code,
- need interact other modules, use theirs methods, properties, events,
the issue when module gets instance of module(provided name example module manager), point there no guarantee module ever garbage collected before unload.
but there generic class(weakrefmodule t: imodule) of given imodule implementation, constrained imodule, internally store weakreference on imodule. , given module expose public methods through extensions methods or inheritance based on weakrefmodule. hiding instance of module behind weakreference. same properties , events(weakevents).
such modules guarantee, other module not prevent garbage collection.
so question is, how design , if there hidden issues?
that design works. need ensure there strong reference modules want keep loaded. otherwise gc non-deterministically kill weak references.
one problem modules cannot rely on other modules being there. if module unloaded , weak ref turns null calls module either fail or nothing. also, point in time @ ref turns null non-deterministic.
i not use weak references here @ all. can create wrapper imodule merely delegates imodule. can clear wrapper when want unload module.
class wrappedmodule : imodule { imodule inner; public void dispose() { inner = null; } void imodule.somemeth() { if (inner == null) throw ...; else inner.somemeth(); } }
now might leak wrapper object small. not lead real module.
modules never access other modules directly. instead, passed "handle" class. handle can disabled @ time.
Comments
Post a Comment