Wednesday, August 07, 2013

Brian brings some Unity to using Prism and dynamically discovering and loading Modules at runtime

brian lagunas - Prism: Dynamically Discover and Load Modules at Runtime

If you develop WPF applications with Prism, then you are probably already aware of the many ways in which you can load a module.  Loading a module starts with what is called a ModuleCatalog.  You can’t load a module unless it has been added to a ModuleCatalog.  Once the module has been added to a ModuleCatalog, Prism will then take care of loading the module assembly for you.  Prism even comes with a handful of module catalogs to give you flexibility in how you register your modules with your Prism application.  You can populate a module catalog from code, from XAML, with XML in an app.config, or from a directory.  Heck, you can even use a combination of all these options to populate your module catalog.

When I am giving a Prism talk at a public event or an internal lunch and learn at a company, I am sure to explain all the different ways of loading your modules and which catalog to use.  This is about the time where the questions really start getting interesting.  Of these questions, the most common is about the DirectoryModuleCatalog.  This particular catalog allows you to specify a folder path to load your modules from.  Now the interesting question… “but, what happens when a new module assembly is dropped into the folder?  Will it automatically be loaded into the app while it is running?”  That is a great question, and the answer is NO.  The DirectoryModuleCatalog does a one time scan of the directory and then loads all the modules that it finds.  If you drop a new module assembly into the directory, it will not be loaded until the application is restarted.  Now the follow-up question… “well, is it possible to dynamically discover the modules and load them from the directory as well?”  Answer; well of course it is.  If you’re using MEF, it’s easy.  If you’re using a container such as Unity, you will need to write the code to handle it yourself.  “Well, we don’t use MEF, so can you show us how?”  This is where my reply is always the same, “a simple web search (Google or Bing) should help you find what you are looking for”.

Well, it turns out, that’s not the case.  It seems that no one has blogged about or shared any code that handles the dynamic discovery and loading of modules using a DI container such as Unity.  Not that I could find, nor anyone who is asking me to show them could find.  Which leads me to this post.  I am going to show you an approach that I have used to support such a scenario.  I am actually going to give you two approaches.  One is the “Quick and Dirty” way.  Basically, I will throw together the simplest sample to achieve the goal.  Then I will show you “A Better Way” in which we will encapsulate this functionality into a custom ModuleCatalog that will handle everything for us.

...

My name is Greg and I pretend to write LOB Prism apps...

Well I am in the middle of using Prism & Unity to build a LOB app I and I ran into exactly what Brian is blogging about. I wanted to do a runtime discovery of modules and just couldn't figure out how to do it quickly enough, so gave up and just did it the hard way. Now that Brian has shown the way, I'm going to have to revisit that... :)

No comments: