Search code examples
mvvmprismeventaggregator

Events filters and multiple instance of modules in prism


I am writing a WPF application using Prism and I am new to Prism

I am facing the following concern :

I will create modules that will be coupled with events, in my case a module will get an image from a camera and send it to a module that will treat the image. For now I am emulating by passing a string like in the event aggregator sample 14 from Prism.

I create the two modules like this

moduleCatalog.AddModule<ModuleProcess.ModuleProcessModule>("ModuleProcess-1");
moduleCatalog.AddModule<ModuleProcess.ModuleProcessModule>("ModuleProcess-2");

Now if I instantiate 2 camera module and 2 process module I want to be able to filter like this so that the process 1 only listen to events from camera 1 (with Ids for example)

So in the constructor of my ViewModel

_ea.GetEvent<MessageSentImageEvent>().Subscribe(MessageImageReceived, x => x._iDest == iId);

My Message paylod is like this

public class MessageImagePayload
{
     public int _iDest;
     //...
}

What I don't understand is how set the iId value in the view model when the modules are created (is it possible to find back the original module name ("ModuleProcess-1") from the view model or the solution is something else ?


Solution

  • The module does know nothing of the module catalog, so it doesn't make sense (in a Prism context) to load one module multiple times.

    If you want two instances of one camera driver (with two different configurations), you have to put loading the configuration either into the driver (if it's registered with the container directly) or the driver manager (if you need more than the container can provide).

    Once the camera driver has its configuration data, it can use that to, for example, direct its output to a specific processing unit.

    Side note: I'd check whether something like TPL Dataflow isn't a better tool for linking the components than is the event aggregator.