Workaround: how to ensure event can be capture in load-on-demand module?

Topics: Prism v4 - Silverlight 4
Oct 21, 2011 at 10:33 AM

I have this code:

ModuleManager.LoadModule("ModuleA")

EventAggregrator.Public(EventA....)

ModuleA does have a subscriber to consume the the EventA.

But it won't worked when its being loading. It only worked the module is loaded.

How can I make ModuleA to consume EventA in the event it is being load-on-demand?

I try to make a wrapper around the ModuleManager to do a synchronous module loading, but I think this won't work as they are running on ui thread, either the thread will block the whole loading, or just back to same issue.

Developer
Oct 21, 2011 at 6:39 PM
Edited Oct 21, 2011 at 6:40 PM

Hi,

Based on my understanding one way you could achieve this is to confirm that your module was initialized, to ensure your suscription is made before publishing the event. To do this you could define an EventAgreggator wrapper class to obtain which modules were initialized and publish the event accordingly. You could compare the ModuleCatalog.Modules collection and check if your required module was initialized. An example of this could be the following method:

public void IsModuleInitialized(string requiredModuleName)
{
    return moduleCatalog.Modules.Where(e => e.State == ModuleState.Initialized).Where(e => e.ModuleName == requiredModuleName).FirstOrDefault() != null;
}

In case the module was initialized you could publish the event using the EventAgreggator. If not, you could subscribe to the ModuleManager.LoadModuleCompleted event and then request to load your required module, so you can publish the event when the module ends loading.

While this approach will probably help you in your specific scenario, it would be worth noting that this implies a little bit of coupling between the modules in your application. As the documentation mentions,

"To keep the application and modules loosely coupled, the application should avoid using this event to integrate the module with the application..."

So, in my opinion you could try loading the module before publishing the event to avoid this situation.

I hope you find this useful.

Agustin Adami
http://blogs.southworks.net/aadami


Nov 8, 2011 at 3:31 AM
Edited Nov 8, 2011 at 3:32 AM
aadami wrote:

...

Agustin Adami
http://blogs.southworks.net/aadami



Thx.

I attempted to look into synchronous approach, but this seems to be a no-way.

The event aggregrator is being adopted, using delegate method to execute post-loaded routine. this has to break/change my codes/design :(