In my last post, I introduced the Modeling project that will be introduced in the VSTS 2010 Architecture product. In this post, I'd like to demonstrate how you can leverage the extensibility mechanisms inherent in the Visual Studio Project template feature set, and provide your organization with a template that can be used to pre-populate new modeling projects according to your specific problem domain.
The example I will demonstrate here is fairly simple, but complex enough to show the real possibilities.
( As you go through this post, keep in mind ideas of modeling templates that you think we should ship in the box at RTM time. I'd love to hear the ideas! )
A common way of structuring models that has enjoyed some success in the modeling space was introduced in 1995 by Phillipe Kruchten, called "The 4+1 View Model of Software Architecture."
I want to show how you can take the Modeling project and create a structure that is largely conformant to the 4+1 view concept, and then make that a project template that can be used by the rest of your organization.
The Final Result
If you follow the detailed steps below, here's what you'll have ( here's the final template zip file ):
A new project template option seen in the "New Project..." dialog called "4PlusOneModelProject":
Here are the steps I took to enable this functionality.
Create the project structure using the existing template
The first thing we're going to do is create a new modeling project with the exact structure that we want to use as a template. Here we go:
You might want to make sure that new elements dropped on each of the diagrams is properly rooted to the right package. Once you're satisfied that things are working the way they should, you're now ready to export this project structure!
Export the template
Now that we have the modeling project in the state that we want it, it is now time to export the structure of this project so that we can use it as a template.
Modify the template
At this point, the template coming out of the Export Template wizard could actually be used and deployed, but there are some subtle problems with this template that you should be aware of, all around the IDs used for the various model elements and diagrams. The system assumes that IDs for various model elements are universally unique, so GUIDs are used for IDs. If you took the template we just exported and started creating new modeling projects based on that template, the IDs for the elements we created ( e.g., the IDs for the packages ) would be the same for all new projects. We don't want to do this, so we need to modify the template in a way that causes new IDs to be created for all the elements in the project each and every time that template is used.
It turns out that the template functionality in Visual Studio supports this scenario for most normal situations. Check out this article that is still relevant today, and will continue to be in Visual Studio 2010. This article talks about the ability to support "Parameter Replacements" and lists a number of Built-in parameter replacements, one of which is support for GUID generation.
It might be tempting to simply go through the template, replacing like IDs with parameter replacement directives like "$guid1$" and "$guid2$". Problem is we have more than 10 IDs we need generated, so unfortunately, we can't use the built-in GUID parameter replacement mechanism. There is another way however, so all is not lost. ;)
We will create "CustomParameters" and write a very small amount of logic that we can associate with the project template. Let's create the CustomParameters now. We will create a CustomParameter for each unique ID found in the *.uml files in the ModelDefinition directory, as well as in all the various diagram files. ( The one noted exception to this is for the primitive types found in the ModelDefinition.uml file ).
This is by far the trickiest part of this exercise, as the IDs must be correctly placed in order to make everything work appropriately. Instead of walking you through each step, I've included the final template that you can take a look for yourself. Let me describe a few of the hilites however.
Create code for ID replacement
There is one last piece to this puzzle. I mentioned above of a small bit of logic necessary to handle the generation of the GUIDs. Here is another article that you can reference for more of the details on how to implement the IWizard interface. The steps are the following:
Deploy new template
Deploying the new template is as simple as dragging the 4PlusOneModelProject.zip into the C:\Users\<USER NAME>\Documents\Visual Studio 2008\Templates\ProjectTemplates directory. You don't even need to restart Visual Studio to see the template when selecting "Modeling Projects" as seen in the "Final Results" section at the start of this post.
I've shown you how to create a project template that establishes a well-defined structure upon project creation. The simple idea here is to provide your organization a common starting point that is more specific to your organizations modeling needs. I've used the 4+1 structure as an example, but there are countless possibilities here.
What common templates should we include in the box by the time we ship the final version of product?