views:

386

answers:

1

I'm looking into doing a project in Silverlight 3 using Prism and I really like the eventing aggregation as shown here http://development-guides.silverbaylabs.org/Video/Prism-Eventing

All the resources seem to be pointing to using Unity with Prism and not Ninject. In Ninject is there a similar way of aggregating events? Or if I want to use Prism am I limited to Unity?

+7  A: 

You are in luck here.

the EventAggregation in Prism isn't a function of the Unity container, but something that Prism automatically puts in the container for client classes to use. So classes declare they have an IEventAggregator dependency and an instance of that is given to that class via dependency injection. The thing to take away here: it's standalone.

So, you have several options.

  1. If you don't need any of the other features of Prism (component modularity, etc) you can simply use Ninject and insert an instance of the EventAggregator into your Ninject container for the rest of your application to use.

  2. If you want the other features of Prism, you can replace the DI container implementation. I wasn't able to find a specific example of this, unfortunately, but I think all you'd have to do was reimplement the base "UnityBootstrapper" type with one of your own making "NinjectBootstrapper". I think that UnityBootrapper is the only point at which the Prism components (EventAggregator, RegionManager, etc) come into contact with Unity (specifically in the implementation of "ConfigureContainer"). Edit: I'm wrong here. You also have to implement IServiceLocator with a Ninject-specific implementation. No big deal, though.

  3. Embrace Unity. It's a pretty good DI container with a lot of flexibility. It doesn't have the fluent interface that Ninject does, but it's certainly not hard to use.

  4. You can use another similar eventing system that is included in the MVVMFoundation (http://mvvmfoundation.codeplex.com). It's called the Messenger and it does pretty much what the EventAggregator does in Prism, but the library you download is much lighter.

Hope this is enough options for ya!

Anderson Imes
Wow, now that's a comprehensive answer. Thanks a lot. I'm not sure which route I'll take yet. The only reason I was favouring ninject over Unity is that I'm starting using Ninject in some web apps and I thought it would be good to just learn 1 DI product, but I may give Unity some more thought.Thanks for your input!
Ciaran
Great answer! With pt2; creating you implementation of IServiceLocator. Why do you need it? And who should you give your implementation to?
stiank81
@stiank81: There are various bits of Prism that use the "Common Service Locator" to abstract away the container you use. Ninject-Contrib has a version out there for you, but you can also use this one written by a fellow that upgraded it to the latest version of Prism: http://sweux.com/blogs/pombeiro/index.php/2009/05/14/migrating-ninjectcontribcompositewpf-bootstrapper-to-prism-v2/
Anderson Imes