Adding a Model Template

This walkthrough introduces you to Model Templates and allows you to package and re-use models across solutions.


This walkthrough will add a new Model Template to the Services Factory that contains basic Data Contract element, that can be re-used across solutions. When you are finished, you will be able to make a Model Template and deploy the template inside the “Create Model” dialogue.
Figure 1: Creating a model from a custom Model Template.


In order to complete this walkthrough, you will need to:
  1. Have the Service Factory source code installed on your computer.
  2. Have a copy of the Service Factory source code registered and installed within Visual Studio’s Experimental Hive.
  3. Be familiar with the basic usage scenarios for the Services Factory.


This walkthrough requires you to:
1. Create a model that contains the re-usable elements.
2. Modify the Service Factory Guidance Package project.
  • Modify CreateModelAction.cs
  • Modify ModelType.cs
  • Modify ModelPropertiesView.cs
  • Add a VisualStudio Template
3. Re-register the Service Factory Guidance Package
4. Test the Model Template



Creating a Model that Contains the Reusable Elements

Note: You can skip Step 1 and download an example from myCommonDataTypes.datacontract and myCommonDataTypes.datacontract.diagram.
1. Open a new instance of the Services Factory and create a new DataContract Model called “MyCommonDataTypes.datacontract” that contains the data contracts you would like to be able to reuse.
After saving the model you should have the following two files in the Model Project directory.
  • MyCommonDataTypes.datacontract. The .datacontract file contains all the elements and properties in your model.
  • MyCommonDataTypes.datacontract.diagram. The .datacontract.diagram file contains the layout within the designer.

Once you use this template, both these files should be added to the Model Project.

Creating a Model Template

2. Close all open instances of the Services Factory runtime and open the Service factory solution Service Factory Guidance package.sln. Both the “Create Model” context menu item and the dialog that allows us to choose a model are contained in the “Service Factory Guidance Package” project.
3. Navigate to the “Items” folder (located under Guidance Package\Service Factory Guidance Package\Templates\ in the solution explorer) and add both files to this folder. In the Properties window, ensure that the Build Action is set to “Content” and Copy to Output Directory is set to “Copy if newer”.

Adding a model to the solution is done through a Visual Studio Item Template. For more information about these templates, see Visual Studio Templates on MDSN.

If your filename is MyCommonDataTypes, you should be able to copy and reuse the template below.

4. Add a new file called MyCommonDataTypes.vstemplate to the Items folder and paste the following xml into the editor. In the Properties window, ensure that the Build Action is set to “Content” and Copy to Output Directory is set to “Copy if newer”.

<VSTemplate Version="2.0.0" Type="Item" xmlns="">
     <Description>Reusable datacontracts</Description>
     <ProjectItem TargetFileName="$fileinputname$.datacontract" ReplaceParameters="true">MyCommonDataTypes.datacontract</ProjectItem>
     <ProjectItem TargetFileName="$fileinputname$.datacontract.diagram" ReplaceParameters="true">MyCommonDataTypes.datacontract.diagram</ProjectItem>
     <Assembly>Microsoft.Practices.RecipeFramework.VisualStudio, Version=1.0.60429.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>

Once these 3 files are added to the solution, we should add them to the Dialog that shows us the list of models we can add to the project. The recipe that shows this dialog is called “CreateModel” and is also contained in the “Service Factory Guidance Package” project.
First we should add our new model template to the ModelType enumeration.

5. Open the ModelType.cs file (located under Guidance Package\Service Factory Guidance Package\Recipes\CreateModel in the Solution explorer). Then add a new field called MyCommonDataTypes to the ModelType enumeration.

 public enum ModelType

You should also add a new entry to the listview, contained in the dialog that allows us to select a model. This is done in ModelPropertiesView class.

6. Open the ModelPropertiesView.cs file (located under \Guidance Package\Service Factory Guidance Package\Recipes\CreateModel\Presentation in the Solution explorer). This file contains the UserControl that is shown in the "Create Model"- dialog. The code behind this UserControl contains a private method called CreateModelItems. This method populates the ListView containing the different types of models to add.

7. Modify the ModelPropertiesView.CreateModelItems() method to the following:
 private void CreateModelItems()
           ListViewItem serviceModel = new ListViewItem();
           serviceModel.ImageKey = ModelType.ServiceModel.ToString();
           serviceModel.Text = ModelType.ServiceModel.ToString();
           serviceModel.Tag = ModelType.ServiceModel;
           ListViewItem dataContractModel = new ListViewItem();
           dataContractModel.ImageKey = ModelType.DataContractModel.ToString();
           dataContractModel.Text = ModelType.DataContractModel.ToString();
           dataContractModel.Tag = ModelType.DataContractModel;
           //added code below
           ListViewItem myCommonDataTypes = new ListViewItem();
           myCommonDataTypes.ImageKey = ModelType.DataContractModel.ToString();
           myCommonDataTypes.Text = "My Common Data types";
           myCommonDataTypes.Tag = ModelType.MyCommonDataTypes;
           //end of added code
           lstModelTypes.Items.AddRange(new ListViewItem[] {
              myCommonDataTypes // added code

Once this is done, you should add the last bit of logic that will unfold the template we previously added to our solution. This is not done in the UserControl but in the CreateModelAction class.

8. Open the CreateModelAction.cs file (located under \Guidance Package\Service Factory Guidance Package\Recipes\CreateModel in the Solution explorer).

The CreateModelAction adds to template to the solution, after someone has clicked the "finish" button on the Create Model dialog. In its Execute() method it unfolds a template, based on the selection in the dialog.

You should add a new case for our new Model in the switch statement it contains.

9. Modify the switch statement in the Execute method to the following:

           case ModelType.ServiceModel:
               sourceFilePath = this.GetTemplatePath(@"Items\ServiceModel.vstemplate");
               modelExtension = Constants.ServiceModelExtension;
           case ModelType.DataContractModel:
               sourceFilePath = this.GetTemplatePath(@"Items\DataContractModel.vstemplate");
               modelExtension = Constants.DataContractModelExtension;
          //added code below
          case ModelType.MyCommonDataTypes:
              sourceFilePath = this.GetTemplatePath(@"Items\MyCommonDataTypes.vstemplate");
              modelExtension = Constants.DataContractModelExtension;
          //end added code

Re-building and Re-registering the Service Factory Guidance Package

10. After having applied the changes to the Service Factory Guidance Package, you should be re-build and re-registered the Guidance package in the experimental hive:
  1. Make sure all other instances of Visual Studio are closed
  2. Re-build the Service Factory Guidance Package.
  3. Re-register the Services Factory Guidance Package in the experimental hive.

Testing the Model Template

11. Using the Model Template can be done after registration in the experimental hive.
  1. Open a new instance of Visual Studio in the experimental hive
  2. Create a new Services project (under “Guidance Packages\Services Software Factory” in the “New Project” dialogue).
  3. Right-click the project and choose “Create Model”
  4. In the “Create Model”- dialogue, choose the “My Common Data Types”-icon and click the “Finish” button.
  5. The model that is created should now contain the Data Contracts modeled in step 1.

Last edited Jun 8, 2007 at 1:10 AM by ndelgado, version 7


No comments yet.