The last couple of days I have been working on a MMC spike that leverage dependency injection and some kind of presentation pattern (in this case MVP), to search for ways to make MMC applications more testable. The scenario I chose for the spike was having a plug-in that could connect/disconnect network adapters through a service that used WMI objects. The sample is very fresh, and there is still work to do to decouple things a bit more (for example, moving strings for WMI objets to config files).
To work with MMC, you need the Microsoft.ManagementConsole.dll assembly, which comes as part of the Windows SDK 6.1 for Windows Server 2008. This is important because versions 7.0 and 7.1 do not include this assembly. Additionally, "MMC is one of the very few extensibility point in the OS that will not support .NET 4. MMC " (from this thread), which is why MEF went out the door and in came Unity.
The MMC documentation, explains the basics on how to get started, so if you have never coded an MMC plug-in, I recommend you to start there. To summarize things, you are required to inherit from a couple of classes, override some methods, and put some attributes in place to get the ball rolling. The Hello World Sample goes over this.
Implementation Details
One of the decisions I had to take was there to make the calls so Unity would resolve and inject the necessary objects. This can be challenging, specially because we have no control over when our Views are instantiated. As you can see in the following code, MMC makes us set a ViewType for a particular description, but gives to control over how the view itself is created.
How does the View get a reference to the presenter? I decided to use the Tag property of the SnapIn class to store the container. The View can then access the Tag property, make the necessary cast and resolve the presenter.
After that, Unity is in the game, so we can resort to constructor injection and normal object resolution. Below is some code from the constructors of the View and Presenter classes.
Alternative Approaches
Another possible approach I though of could be using a WinForm View instead of a List View, but having it as a placeholder for the real views we want to show depending on the Node that is clicked. This approach leaves the instantiation of the placeholder form to MMC, but would give us control for the instantiation of the other views. This is just an idea, and I might get deeper into this without getting the expected results.
Getting the Code and Plug-in
You can download the source code from here, and the plug-in (built assembly) from here. To install the plug-in, open the Windows SDK console, cd to the directory where the assemblies are, and run InstallUtil.exe MMCSpike.dll (as shown below).
That's all for this post. If I get any deeper with the sample or find other interesting approaches I'll let you know.