dependency injection - How to inject event handlers into events with Unity -
how can inject (attach) event handlers .net events of instances created unity ioc container?
example: have class reports errors via standard .net event:
class cameraobserver { public event action<exception> unhandledexception; [...] }
i have class reponsible handling events:
class crashmonitor { public static void handleexception(exception x) { ... } }
what automatically inject handler crashmonitor every instance of cameraobserver in pseudocode:
unitycontainer container = new unitycontainer(); container.registerinstance<action<exception>>(crashmonitor.handleexception) .registertype<cameraobserver>(new injectionevent(unhandledexception)); var observer = container.resolve<cameraobserver>(); // crashmonitor.handleexception attached observer.unhandledexception
is there way unity? can think of ugly workaround deriving cameraobserver special constructor intendend dependency injection or or method injection. make syste more complex (because have write code). naively expect add [dependency] attribute on event , should work.
i have asked same question in unity discussion group on codeplex
http://unity.codeplex.com/thread/view.aspx?threadid=80728
and answer "there nothing". there demo of eventbroker more complex (autowiring of publishers , subscribers). still think kiss mechanism inject events useful , started myself.
Comments
Post a Comment