How to inject event handlers into events with Unity

How can I embed (attach) event handlers to .net events of instances created by the IoC Unity container ?

Example. I have a class that reports errors through a standard .net event:

class CameraObserver
{
    public event Action<Exception> UnhandledException;      
    [...]
}

I have another class that is responsible for handling these events:

class CrashMonitor
{
    public static void HandleException(Exception x)
    { ... }
}

What I would like to do is automatically insert a handler from CrashMonitor into each instance of CameraObserver, as in this pseudocode:

UnityContainer container = new UnityContainer();
container.RegisterInstance<Action<Exception>>(CrashMonitor.HandleException)
     .RegisterType<CameraObserver>(new InjectionEvent(UnhandledException));

var observer = container.Resolve<CameraObserver>();
// CrashMonitor.HandleException is now attached to observer.UnhandledException

Unity? , , CameraObserver , . ( ). , [Dependency] , .

+5
1

codeplex

http://unity.codeplex.com/Thread/View.aspx?ThreadId=80728

" ". EventBroker, , , ( ). , KISS .

+3

All Articles