I presented at the Black Marble Architecture Forum in the North event in December and one of the things I showed was how to create a toolbox item extension to add a UML design pattern into Visual Studio’s UML toolbox. Having done this I checked and couldn’t find any extensions that covered the Gang of Four design patterns, so I thought I’d create an extension to do just that. Let’s start at the end with where to download them and what they give you…
The toolbox items extension gives you some extra toolbox items that cover the majority of the core Gang of Four design patterns. Once installed, if you open a UML class diagram you should see the following additions under Structural, Creational and Behavioural (yes, British spelling) Patterns:
Select one of the toolbox items and this will place a ready to go representation of that design pattern on the class diagram, in this example it’s the Abstract Factory:
Underlying the visible Toolbox extension is a UML profile that contains a number of UML stereotypes to mark classes and interfaces with roles in the relevant pattern. So in the example above there is an IFactory interface stereotype and an Abstract Factory class stereotype.
If that looks interesting then depending on what you want you can get hold of this in two places:
I’ve published the .vsix extension to the Visual Studio Gallery here. All you need to do is download the .vsix, install it and restart Visual Studio. To check it installed successfully then go to Tools | Extension Manager and you should find these extensions have been added (I’ve filtered on UML to simplify it):
Then open or create a UML modelling project and once you have a class diagram you should see the new toolbox items.
I’ve added the Visual Studio solutions that I created this extension from to Codeplex here. You can download the solutions, take a look at the projects and update/improve/extend/adapt the toolbox items to suit your needs.
Take a look at the Codeplex project if you want to download the full solutions but as an overview this is essentially simple. It breaks down into 4 key aspects:
I wanted to create a UML profile in order to define the stereotypes that I would use in the design patterns. As an example I wanted to have <<Singleton>> stereotype so that any class that you wanted to implement the Singleton pattern could be identified as such without having to rely on naming conventions, and the name can be independent of the stereotype. A secondary reason is that if I wanted to generate code from the design patterns this would allow the T4 templates to generate the appropriate code for the specified stereotype. I haven’t got round to this part but using a profile means that this will be possible in the future.
The documentation on creating a custom profile is here. This is what I followed but a summary is:
1: <profile dslVersion="184.108.40.206"
2: name="GoFDesignPatterns" displayName="GoF Design Patterns"
2: <stereotype name="IVisitor" displayName="IVisitor" >
4: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IInterface" />
7: <stereotype name="ConcreteVisitor" displayName="ConcreteVisitor" >
9: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IClass" />
12: <stereotype name="Element" displayName="Element" >
14: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IClass" />
17: <stereotype name="ConcreteElement" displayName="ConcreteElement" >
19: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IClass" />
2: <!-- List of metaclasses (that is, classes in the UML spec) that are mentioned in the stereotype definitions. -->
3: <metaclass name="Microsoft.VisualStudio.Uml.Classes.IClass"/>
4: <metaclass name="Microsoft.VisualStudio.Uml.Classes.IInterface"/>
Having created the profile itself (actually I did this iteratively and not in one go), then you need to package this as a Visual studio extension, a .vsix file. This is actually fairly straightforward and there are good instructions here. The key points I would highlight are:
I followed the documentation here and it was pretty easy. My points to note:
2: <customToolboxItem fileName="AbstractFactory.classdiagram">
7: <value>Creational Patterns</value>
10: <bmp fileName="DesignPatternIcon.bmp"/>
16: <value>Groups object factories that have a common theme.</value>
Exactly the same as the extension project for the profile with the following exceptions:
I hope that either the toolbox items themselves or the explanation on how to create them are useful or interesting, and I’d encourage you to think of opportunities to extend and customise your Visual Studio environment to help you work more effectively. And if you don’t like my implementation of the patterns then please improve them via the Codeplex project.
Great Work - Thanks :-)
looks very interesting! anyway, when i try to run the installer package, i get message saying "This extension is not installable on any currently installed product" despite i have of course VS2010 installed... any idea?
Hi Lubos, yes this is deliberate :-) I specified in the installer that this should only be installed in Visual Studio 2010 Ultimate or Premium as these are the only editions that support UML, and Ultimate is the only edition that supports creation of UML diagrams. So - assuming you don't have Ultimate or Premium then this is expected behaviour. If not, then let me know and I'll see if there is another issue.
yes - it's correct. i got it wrong, my expectation was that this will extend class diagram functionality in professional as well...
i love your blog, i have it in my rss reader and always like new things coming up from it.
Is there any way that this is useable in the Premium edition? There doesn't seem to be any modelling projects - so how can you use you toolbox editions?
This is limited to the Ultimate edition as that's the edition that allows you to create UML models and diagrams. A model that's created in Ultimate can be viewed in Premium but it's a read-only experience.
Hi! I try to use but show this message:
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
Since the October, 2012 refresh, which indicates it supports VS2012, myself and a number of others are getting error messages when trying to install into VS2012 Professional. Please see the messages on the Gallery page at visualstudiogallery.msdn.microsoft.com/dc41bd3a-85f6-4015-aa39-c57bfa332b20
Please advise. Thanks.
Hey man, great work. I wanted to give it a try but it says "This extension is not installable on any currently installed product" despite I have VS2010 Ultimate. Any Ideas?