Getting List of Loaded Assemblies using PRISM

Topics: Prism v2 - Silverlight 4, Prism v2 - WPF 4, Prism v4 - Silverlight 4, Prism v4 - WPF 4
Mar 21, 2012 at 8:30 PM

Hi,

   We are working on a large application developed using Silverlight 4 and PRISM v2. There is a dependency between 2 modules where one is referred in other by adding reference to the binary. We are supposed to remove this dependency. I tried using IUnityContainer, but it requires the corresponding abstract class. As we have removed the binary reference, there is no reference to the specific type. 

So I thought of discovering the type using the System.Reflection. In order to achieve this, we need to know the list assemblies from the multiple modules.

Is there any possibility to get the list of assemblies ? Or please let me know the other ways.

 

Regards,

Raaj

Developer
Mar 21, 2012 at 9:16 PM

Hi Raaj,

Based on my understanding, the recommended approach when using types that are declared in different assemblies is to register and compose them through the container:

  • Usually, the classes that you need to export to other modules would implement an interface. You can register these classes, mapping them to their corresponding interfaces in the container, inside the Initialize method of your IModule class (lets call this module "ModuleB").
  • Then, in the module that requires them, you can obtain an instance of the corresponding type through the interface (lets call this module "ModuleA").
  • The aforementioned interfaces would be in a common project (for example Infrastucture), that could be referenced by both modules (as well as other projects in your application).

Doing this, the ModuleA will not require to reference ModuleB and it still will be able to obtain instances of the classes in ModuleB that were exported through the container. Also, as ModuleA will require the ModuleB to be initialized first, it's a good practice to declare a "loosely dependency" between those modules as explained in the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Mar 21, 2012 at 10:21 PM

Hi Damian,

I have proposed the same idea to my team. But we were asked to find any other potential solution is around. Is there any way to discover and load the assembly from a specific XAP file?

 

Regards,

Raaj

Developer
Mar 22, 2012 at 9:01 PM

Hi Raaj,

As far as I know Prism does not expose the assemblies of the modules as out of the box. However, I believe a possible approach could be to implement a shared service that could store the assembly of each module:

  • The shared service could be simple a Dictionary of <string, Assembly> that could be defined in the Infrastructure project.
  • In the Initialize method of each module, you could register the Assembly of the module in the aforementioned service. For example, this can be done doing something like this:
    public void Initialize()
    {

        Assembly asm = typeof(ThisModule).Assembly;

        //   ...  or ....

        Assembly asm = Assembly.GetExecutingAssembly();

        // This would be the service        
        service.Add("ThisModule", asm);

    }
  • This way, you can retrieve the assembly you need from the service and use it to perform reflection to obtain the required classes.

Note that you will need to ensure that the corresponding module was loaded before trying to retrieve the classes through reflection. A possible way could be to declare a "loosely dependency" between those modules as mentioned above.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini