DomainDataSource - MVVM and MEF

Aug 22, 2011 at 3:35 PM


I have a Silverlight 4 - MVVM project that is using MEF (no Prism) to import / inject external .xap files. So far so good... I have my large project broken up into several different projects and .xap files that contain user controls.

But now I'm puzzled; When I need data... where do I place the DomainDataSource? I'm using RIA Services and that information is located in the main project.

It would seem that I need to place it in the external projects ViewModel, but again, the external project knows nothing of the RIA Services. So, do I then place it into the main apps ViewModel? That's the page that is using MEF to load the external .xap file... It seems like it should go there. But then, how do I bind it to the incoming View that's being loaded by MEF?

Also, it seems that the external View still needs a ViewModel of it's own so I can bind the controls to properties.

Can someone please give me some clues? Point me to some links? Or, does anyone have an example they can share?

Anyway, any help would be greatly appreciated.

Many thanks in advance,

~ Dave Navarro

Sep 7, 2011 at 4:45 PM

Well...I'm no expert and am having troubles of my own with recomp, but I have my project setup like so:

Project.Main (Starting point that loads the "modules/xaps"...)
Project.Main.Web (Web Host)
Project.Data (The ONLY Silverlight client with a RIA Services Link)
Project.Data.Web (RIA Services)

Project.Main is the entry point and has a reference to Project.Data which houses the class a class like MyDataModel which has a bunch of methods to query data from the RIA data contexts.  Project.Module1 and Project.Module2 both have a reference to Project.Data (CopyLocal = False) and use that same class (MyDataModel) via the [ImportingConstructor] attribute to get the export for the MyDataModel instance.  That class (MyDataModel) has the PartCreationPolicy.Shared attribute so it will be shared across all projects that reference it, that way MEF will create only one instance of the class and share it/"export" it to all others "requests" for [Import]/[ImportingConstructor].

If you place a break point in the ctor of MyDataModel, it should only ever be hit one time.

So that breakpoint will be hit when Project.Main is loaded, but not when Project.Module1 or Project.Module2 are loaded, however both modules will get the same instance of MyDataModel that Project.Main has and thus can use it to get data from the RIA Services via the Project.Data classes.