Event Broker in MEF

Feb 18, 2009 at 10:37 AM
Are there any resources/hints how a MEF-based Event Broker can be implemented?
Feb 18, 2009 at 10:42 AM
Ohhh... wow this is very much my cuppa tea... ;~)

I wrote the EventBroker for the WCSF and wrote the post on MEF:ifying the web: http://blog.noop.se/archive/2008/09/15/managed-extensibility-framework-mef-used-to-make-a-composite-web.aspx. The code sample from that is available at codeples MEFContrib http://www.codeplex.com/mefcontrib

What you need, I guess you know, is a DI container that creates your instances and then you need to enable the container to do your MEF composition.

What is your scenario?

M.

On Wed, Feb 18, 2009 at 12:37, DenisVuyka <notifications@codeplex.com> wrote:

From: DenisVuyka

Are there any resources/hints how a MEF-based Event Broker can be implemented?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Magnus Mårtensson
Senior Consultant - Scrum Master - MCSD, MCTS
Dotway AB

Tel: +46 (768) 51 00 36

http://blog.noop.se/
Feb 18, 2009 at 10:59 AM
Thanks for a quick reply,
Actually I'm seeking the way events can be exported/imported for the parts, similar to Unity EventBroker having a pair or event subscription attributes. I would prefer giving control to MEF instead of resolving objects via DI container, otherwise I don't need MEF i think ;)
As far as I see this is not supported by MEF for the moment or at least I cannot find walkarounds/hacks for that.
Any ideas how this can be acomplished?

Feb 18, 2009 at 2:52 PM
Hi Denis

1. For point to point communication, MEF allows you to export methods which are imported as delegates on the other side. This allows you to use these methods for facilitating communication between an exporter and an importer. It does not handle the multi-cast scenario however.
2. For a multicast pub/sub facility, check out the EventAggregator included in CompositeWpf (http://msdn.microsoft.com/en-us/library/dd458915.aspx). It provides a very light facility that can easily be exported from MEF (by adding an Export attribute or using a property export), which allows you to wire up publishers with 1 to n subscribers through a loosely coupled fashion. p&p built this service to be extremely light, and it's very easy to use and non-invasive (not attribute based, strongly typed, and not bound to any container). The source for the EventAggregator can easily be ripped from the CompositeWpf source, or you can just reference the binary and use EventAgg without anything else.
3. Create a custom service that you export, which exposes an event that consumers can subscribe to by importing it. This is a very simple code pattern, you can read more about it here http://msdn.microsoft.com/en-us/library/dd458878.aspx in the section on SharedServices

HTH
Glenn
Feb 18, 2009 at 4:11 PM
I've been using Composite WPF as well as Unity in different projects so ripping EventAggregator is something I was considering. Guess this will be the best option in my scenario.
Thanks
Feb 20, 2009 at 8:25 PM
I've tried using EventAggregator within MEF and must admit it perfectly fits the MEF schema. Thanks for the assistance.
Feb 21, 2009 at 11:20 AM
Demo coded pls... ;~) Perhaps a small demo project to upload to codeplex.com/mefcontrib?

Cheers,

Magnus

On Fri, Feb 20, 2009 at 22:25, DenisVuyka <notifications@codeplex.com> wrote:

From: DenisVuyka

I've tried using EventAggregator within MEF and must admit it perfectly fits the MEF schema. Thanks for the assistance.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Magnus Mårtensson
Senior Consultant - Scrum Master - MCSD, MCTS
Dotway AB

Tel: +46 (768) 51 00 36

http://blog.noop.se/
Sent from: Malmo Skåne Sweden.
Feb 21, 2009 at 12:27 PM
Edited Feb 21, 2009 at 5:00 PM
I've just published an article related to use of EventAggregator within a MEF-enabled WPF application
Feb 21, 2009 at 7:27 PM
That's OK I just meant a working demo sample! ;~)

On Sat, Feb 21, 2009 at 14:27, DenisVuyka <notifications@codeplex.com> wrote:

From: DenisVuyka

You can see the drafts here: http://www.codeplex.com/carcass
I'm not sure I can post EventAggregator to mefcontib as I don't see here any contribution from my side ;)

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Magnus Mårtensson
Senior Consultant - Scrum Master - MCSD, MCTS
Dotway AB

Tel: +46 (768) 51 00 36

http://blog.noop.se/
Sent from: Malmo Skåne Sweden.
Feb 22, 2009 at 6:42 PM
Denis, you can post it on Contrib if you want to.
Glenn
Feb 22, 2009 at 6:58 PM
Denis, I added a few comments on your blog post. Instead of manually adding the EventAgg to the container, I recommend you put an Export attribute on it and have it added through catalogs.
Feb 22, 2009 at 7:33 PM
Thanks, Glenn
I will post it on Contrib later on.
As for marking EventAggregator with Export attribute... yes I was thinking of that also. I think the decisions might be taken according to the scenarios and target architecture. By marking it with Export you get dependency on MEF while EventAggregator could yet stay independent on external parts of the framework.
But I think both approaches are right, that's up to developers decide what to choose,
Thanks for your feedback
Feb 22, 2009 at 10:23 PM
Edited Feb 22, 2009 at 10:26 PM
Actually Denis, there's two options here.

1. Inherit from EventAggregator and create MefEventAggregator that simply adds on the attribute :-)
2. As EventAggregator takes no dependences, you can create a new part called EventAggregatorPart that has a property export of IEventAggregator. Then the property can just instantiate the instance. As long as the part is a singleton, it will work exactly the same way, and you won't need any custom derived class.

Example below.

[CompositionOptions(CreationPolicy = CreationPolicy.Shared)]
public class EventAggregatorPart {
  [Export(typeof(IEventAggregator))]
  public property EventAggregator {
    get{return new EventAggregator();}
  }
}

This is a common approach when you want to export types that are sealed where you can't inherit and add an attribute.

Glenn
Feb 23, 2009 at 1:30 AM
I just paired with a friend and we came up with a new way to do this which removes the need for EventAgg at all.....

Blog post pending.
Feb 23, 2009 at 6:23 AM
Yes, this is a more intresting approach :) I like keeping independent things independent, so your last suggestion might be the best way preserving EventAggregator isolated.
Thanks
Feb 23, 2009 at 8:30 AM
Just posted a different approach to EventAgg with MEF.

http://codebetter.com/blogs/glenn.block/archive/2009/02/23/event-aggregation-with-mef-with-and-without-eventaggregator.aspx