1

Suppose a Prism version 8 WPF module has a ViewModel which needs to call on a service. the service implements IService, but there exists a number of implementations of this service. Each implementation is a file (class library), possibly as a IModule (see below).

The user shall be able to configure which file to use either by configuration or by a folder's content.

Obviously(?) I am thus thinking of Module discovery by creating the right type of ModuleCatalog while "bootstrapping" the application and the service could thus be contained in this module. If the call is a void call ("fire-and-forget") I guess I could simply use EventAggregator (implementing the service as an observer), however the call returns a value.

What is the best approach solving this? (I would like to avoid writing my own assembly "discovering/loading" of some kind of a swappable service implementation dll file)

EuroEager
  • 167
  • 1
  • 2
  • 11

1 Answers1

0

If you can inject IEventAggregator, you can inject IService, can't you?

If no module registered an implementation, you'll get an exception. If more than one module did, the last one wins (with unity as container, at least).

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • A SO comment restricts number of chars, please see consecutive comments: Thanks for your simple answer which hints me of the possibility. The remaining problem was as follows: The "well-known" module (the one with a ViewModel, from now called the "dependant module", depends on the service module. I tried first with making both modules "well-known" (adding project reference from the application to both modules). All worked fine as long as the service module is configured before the in the "dependant module" in ConfigureModuleCatalog (otherwise the service injection to ViewModel throws). – EuroEager Oct 18 '21 at 08:47
  • Then I removed the service module from the application project references and no more AddModule in ConfigureModuleCatalog (kept the AddModule for the "dependant module"). Finally overrode CreateModuleCatalog and created a DirectoryModuleCatalog for the directory with the service module in it, then I forced an initialization of this module catalog right away which initializes the service module before the "dependant module" and all works fine. – EuroEager Oct 18 '21 at 08:49
  • My problem was thus that the service module was initilized too late. Wish I understood in more details how to control the order if for example both modules should be discovered by DirectoryModuleCatalog. And what about multiple directories (would multiple IModuleCatalog.Initialize() be useful providing Path property is changed before each call? Anyway, thanks for hinting me towards correct approach. – EuroEager Oct 18 '21 at 08:49
  • @EuroEager you do not want to start `Resolve`ing before everything is `Register`ed. You should do the initial navigation (except for a loading screen, perhaps) in `OnInitialized`, i.e. when everything is loaded. – Haukinger Oct 18 '21 at 09:20
  • Thanks again, I perform initial navigation from OnInitialized and it works fine. If application complexity increases (inter module dependencies between discovered modules etc.) and similar happens I will ask again – EuroEager Oct 18 '21 at 11:29