Composite Extensions for Windows Forms

Have you by chance looked at the cool stuff we produced for Composite Application Guidance for WPF (aka Prism), and wished you could have the same capabilities for Windows Forms, but without needing to adopt CAB/SCSF? Now you can get at least part of those capabilities.

When we were drawing to a close on Prism 1, there were certain things that we would have liked to do, but had to skip to stay within scope and schedule.

One of those things was that we realized that a lot of what we had come up with could be used in Windows Forms as well. Specifically, our module loading, events, and UI Composition patterns could all be used in the context of a Windows Forms application with just a little more effort. For those things that we knew were more general, we at least factored them out to a separate library, Microsoft.Practices.Composite. Alas, that is as far as it went because our charter was for WPF, not Windows Forms.

So I have been trying to find time to complete the work needed to use the non-WPA capabilities of CAL in Windows Forms and finally found a chunk of time at 37K feet on a trip to get it done.

For now I’ve created these as a stand alone set of libraries that you can add into your own solutions. Feel free to take the code, change the namespaces, etc. Eventually I’ll integrate the capabilities into the CompositeWPFContrib libraries so it will be accessible there as well. What I added includes the following:

  • A SimpleUnityBootstrapper base class that removes the dependence on WPF and which removes the parts that create the Shell and Region related stuff. Regions in Prism were implemented in a way specific to WPF, and the CreateShell override in the UnityBootstrapper that comes with Prism expects you to return a DependencyObject, which is also a WPF base class. The SimpleUnityBootstrapper makes it so you can follow the same bootstrapping process as a WPF Prism application to get the container, module, and event services registered and ready to go.
  • A CompositeEvent class and supporting classes that allows you to have the same programming model and infrastructure as Prism loosely coupled Pub / Sub events, but this class supports UI thread dispatching to either Windows Forms, WPF, or any SynchronizationContext aware subscriber. If you are not familiar with the SynchronizationContext class, it came in with .NET 2.0 and provides a standard thread dispatching mechanism that is used by both WPF and Windows Forms under the covers. You can also create custom synchronization contexts for other places that you have thread affinity. If you want to see an example of a custom SynchronizationContext, check out the unit tests. I needed to write a mock SynchronizationContext to verify the dispatching worked correctly.
  • A sample application that shows a way to get a Prism Region-like capability to inject views into the shell.
  • Unit tests for all the supporting library code, most of which were blantantly adapted (mostly direct copy/paste) from the unit tests of Prism since I was trying to create something with pretty much the same API and the same behavior as the Prism counterparts.

I’m not going to pick apart the implementation here unless demand is great enough, but you are welcome to dig into the code in the source.

However, the point was to make it easy to have modular loading and loosely coupled events in a Windows Forms application. So let me show you the use of these capabilities in a sample application. The app itself is very simple, just a drop down list of products and a save selection button to simulate a save operation.

The app, in all its stunning UI glory, looks like this:

AppGrab

The combobox and button are part of a view loaded up by a module, which is loaded through the standard module enumeration and loading services in Prism. When the button is pressed in the view, its ViewModel fires a pub/sub event, which the shell subscribes to and updates the status bar. The view is a Windows Forms user control that I was able to add dynamically to the shell using a simple form of region based on resolution through the container.

So the derived bootstrapper class from the SimpleUnityBootstrapper base class looks like this:

internal class Bootstrapper: SimpleUnityBootstrapper
{
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Shell = Container.Resolve<Shell>();
    }
    protected override IModuleEnumerator GetModuleEnumerator()
    {
        return new StaticModuleEnumerator().AddModule(typeof(CompositeWFModule));
    }

    public Shell Shell { get; private set; }
 
}

I used the override of ConfigureContainer (same purpose as the UnityBootstrapper ConfigureContainer method) as a convenient place to construct the shell through the container, so that it can participate in dependency injection. This is an alternative approach to the CreateShell override that is required in the UnityBootstrapper of CAL, which is needed in CAL because the base class uses WPF Dependency Properties to add the RegionManager reference as an attached property to the shell after its construction. Since we are in Windows Forms land, that approach won’t work, so regions don’t exist in my modified libraries, but my sample app shows a way to achieve a lightweight form of dependency injection based on the shell being available through the container.

For module enumeration, you can see that I am just using the StaticModuleEnumerator that ships with CAL. All that is really needed to take advantage of the modular loading capabilities of CAL is a suitable bootstrapper base class that doesn’t assume you are in WPF land. Thus my SimpleUnityBootstrapper class.

The Shell property is exposed to make it easy to get the shell to pass to Application.Run in the program’s main method:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Bootstrapper bootstrapper = new Bootstrapper();
    bootstrapper.Run();
    Application.Run(bootstrapper.Shell);
}

To create a similar capability to Regions for view injection in the Shell in Windows Forms, I simply have the shell register its containers that it wants to act as a region with the container so they can be resolved by the modules that load up:

public partial class Shell : Form
{
    int m_UIThreadId = Thread.CurrentThread.ManagedThreadId;

    public Shell(IUnityContainer container,IEventAggregator evtAggregator)
    {
        InitializeComponent();
        container.RegisterInstance<Panel>("MainRegion",m_MainRegionPanel);
        evtAggregator.GetEvent<ProductSavedEvent>().Subscribe(OnProductSaved,
ThreadOption.SubscriberAffinityThread); } void OnProductSaved(Product p) { Debug.Assert(Thread.CurrentThread.ManagedThreadId == m_UIThreadId); m_StatusLabel.Text = p.Name + " saved"; } }

Notice that the shell is able to have the container and the CAL event aggregator service injected as dependencies because it was constructed through the container itself (in the bootstrapper shown earlier). It then registers the existing instance of a container control with the container as a named instance (MainRegion in this case) so that a loading module can resolve that named instance to know where to add its views as children.

Ignore the event subscription code for now, I’ll get back to that.

The solution has a module library with a module class in it following the patterns of Prism and using the CAL IModule interface definition.

public class CompositeWFModule : IModule
{
    IUnityContainer m_Container;
    public CompositeWFModule(IUnityContainer container)
    {
        m_Container = container;
    }

    public void Initialize()
    {
        Panel mainRegion = m_Container.Resolve<Panel>("MainRegion");
        ModuleView view = m_Container.Resolve<ModuleView>();
        view.Dock = DockStyle.Fill;
        mainRegion.Controls.Add(view);
    }
}

Here you can see how the module injects the view into the pseudo-region. It just resolves the region out of the container with a known container type (yes, you do have to live with some type coupling here, at least to the minimal common denominator base class Panel), and once it has that, it just adds the view as a child control. For  more advanced scenarios such as adding tabs to a tab control you would need more type information to add the child, activate it, set the tab text, etc., but you could still be reasonably loosely coupled to where that control exists in the UI and how it gets constructed.

That is about it for the module loading and a lightweight substitute approach for regions in Windows Forms.

Then the real thing I wanted to get working for Windows Forms is Prism’s loosely coupled pub/sub events model. This took a little more work simply because we tied ourselves a little too tightly (in my opinion) to WPF when implementing the UI Thread dispatching option for events in Prism. Both WPF and Windows Forms actually share a common approach to UI thread dispatching under the covers – SynchronizationContext. They both use it a little differently and encapsulate it so you can’t directly get to it through their API, but since Windows Forms 2.0, that is available under the covers and in WPF that is what the DispatcherObject base class of UIElements is using to dispatch things to the UI thread.

So what I wrote was a more generic approach to UI thread dispatching under the covers of the CompositeEvent class (following the patterns of the CompositeWpfEvent class in CAL). The dispatching approach checks to see if the subscriber has a synchronization context, and if so, captures it and uses it to dispatch the event back to the UI thread through the SynchronizationContext when the event is fired if the subscriber asked for the event to be dispatched on the “SubscriberAffinityThread” (the name of the ThreadOption enum member I came up with.

I didn’t want to call it UIThread because the pattern is actually more general than that. You can create your own SynchronizationContexts with a little work (in fact, I had to do just that to write a mock for the SynchronizationContext to test the code I was writing), and you might do that for specialized scenarios. For example, say you were going to be doing some background work in your app, you wanted to have a custom thread pool of 5 threads, and those threads had thread affinity (i.e. using ThreadLocalStorage). In that case, you could implement a thread pool synchronizer (see the example Thread Pool Attribute in our downloads at IDesign) that would ensure that there was a SynchronizationContext to dispatch to the appropriate thread. In that case, the CompositeEvent class I came up with will properly dispatch based on that SynchronizationContext instead of just a UI one.

So anyway, it is way more complicated to try to explain it than it is to use it. It follows the same simple but powerful patterns and capabilities as the CAL event.

To define an event type, you just derive from my CompositeEvent class instead of the CAL CompositeWpfEvent class, specifying the strongly typed event argument or payload as the generic type parameter:

public class ProductSavedEvent : CompositeEvent<Product>
{
}

To subscribe, you have the single line of code shown earlier in the shell constructor which points to the handling method (strongly typed based on the event argument type:

evtAggregator.GetEvent<ProductSavedEvent>().Subscribe(OnProductSaved,           
ThreadOption.SubscriberAffinityThread);

 

In this case I show the overload that just specifies the threading option, but just like the CAL events, this one supports the same three options in addition to just loosely coupled pub/sub:

  • Weak references or not for the subscriber object to avoid holding the subscriber object alive if the subscription is the only thing keeping it alive
  • Thread dispatching option for the notification
  • Filter delegate based on Predicate<T> to allow strongly typed decision based on the event payload to determine if your subscriber should be called or not.

The publisher code, in this case in the view being loaded up from the module, is just a single line of code like in CAL:

 evtAggregator.GetEvent<ProductSavedEvent>().Publish(product);

So there it is. If you have no exposure to Prism, you really need to start there, because I am not going to try to duplicate all the documentation on the capabilities of Prism. But once you are comfortable with the capabilities of Prism for WPF, now you have some code that will let you directly repeat the use of modules and events from Prism in a Windows Forms application with the exact same patterns, and you also have an example of how to pull off a “Region-like” capability using the IoC container and container controls in Windows Forms.

Feedback highly welcomed. I want to get any needed changes in there before I add it to the trunk for CompositeWPFContrib.

Here’s the goods.

Comments are closed.