Dynamically updating an application’s functionality using IronPython

About a month ago I blogged about an e-mail sender program that used the MVVM pattern to integrate business logic written in Iron Python with presentation logic written in C#. In this post I will show another great benefit of using this approach. The source code related to this post can be downloaded from here.

The old e-mail client only allowed users to send e-mails using a Hotmail or a Gmail account. Let’s say we our application to support new e-mail providers without having to restart it. When discussing this with Martin Salias, he suggested me to use a FileSystemWatcher, which is the approach I decided to take.

The first thing we need to do is create a FileSystemWatcher instance in the ViewModel’s constructor and have it listen to changes in the directory where the Python modules are obtained from. As we only want to get updates when a python module is updated, we specify that filter as well:  

private void InitializeFileSystemWatcher()
{
    this.fsWatcher = new FileSystemWatcher(PythonModulesPath, "*.py");
    this.fsWatcher.EnableRaisingEvents = true;
    this.fsWatcher.Changed += FileSystemChanged;
}

Now we need to re-load the modules being used by our application once a change in the path specified occurs and update the UI accordingly:

private void FileSystemChanged(object sender, FileSystemEventArgs e)
{
    this.LoadPythonModules();

    //update all bound properties
    this.RaisePropertyChanged(string.Empty);
}

To check how this is working follow these steps:

  1. Open the solution provided in the .zip file and run it.
  2. Expand the Combobox control to verify that only Gmail and Hotmail are supported.image
  3. Find the New Modules folder inside the .zip file and copy the emailDispatcher.py module to the WPFClient\bin\Debug\Python Code directory. Replace the existing file.image
  4. Expand the Combobox control to verify that Yahoo has been added as a provider.image
  5. (Optional) If you have a yahoo account send an e-mail to test that it works correctly.

Although this sample is quiet simple, I think it is a great way to illustrate the flexibility of having this kind of application.