Search code examples
mefcaliburn.micro

Caliburn.Micro. Automatically call eventaggregator.Subscribe() for IHandle implementors with MEF


In Caliburn.Micro documentation the authors mention such possibility:

documentation link

IHandle inherits from a marker interface IHandle. This allows the use of casting to determine if an object instance subscribes to any events. This enables simple auto-subscribing if you integrate with an IoC container. Most IoC containers (including the SimpleContainer) provide a hook for being called when a new instance is created. Simply wire for your container’s callback, inspect the instance being created to see if it implement IHandle, and if it does, call Subscribe on the event aggregator.

How is it possible to achieve this with MEF?

This question is the same as Caliburn.Micro. Automatically call eventaggregator.Subscribe() for IHandle implementors with Autofac

So how is similar functionality as the described AutoSubscribeHandersModule implemented in MEF?


Solution

  • I blogged about how to do the auto-wiring for the Event Aggregator with MEF in Caliburn Micro here; http://www.kjetilk.com/2011/10/auto-wiring-eventaggregator.html.

    In short; You need to add the MEFContrib (nuget -> Install-Package MefContrib), implement an IExportedValueInterceptor that subscribes any IHandle instances, and plug the interceptor into the MEF creation pipeline using an InterceptionCatalog in the bootstrapper.