API for model elements in text templates

Editor
Jun 24, 2012 at 9:01 AM
Edited Jun 24, 2012 at 9:01 AM

 

Hi,
I am evaluating the Pattern Toolkit for creating some guidance for my team to follow. So far I find the toolkit a bit rough around the edges but other than that it is exactly what I've been looking for. 
I installed the Pattern Toolkit and I followed the Hands-On Lab. I also created my first pattern for WPF projects, that is able to create a view/viewmodel pair that is correctly wired into the application. There also is a file in that project that refers to all viewmodels and I wanted to use a text template to generate it when a new view/viewmodel pair was created. I can't figure out how to do this with the Pattern Toolkit however.
I tried to look at the samples but hit some bumps on the way. I am providing them for your information, maybe you can fix them:

  • Installing Pattern Toolkit without SDK installed gives an error dialog that only shows a string that looks like a GUID
  • Opening the MVC sample source fails with many projects unable to load. After starting a different pattern project and checking the include was the same I did a reload of the projects and they loaded just fine.
  • Building the MVC Sample source results in a Validation Error, with the error in the Pattern Toolkit Extensions Output being "Object is not set to an instance of an object"
  • The text template that the command on the ViewModel element refers to (viewmodel.tt) is not listed as one of the assets in the Solution Builder.
  • Installing the MVC Sample VSIX and trying to run a new project gives compile errors because one of the unit tests is calling LogOn with the wrong signature.
  • Fixing that and running the project that resulted from the MVC Pattern gives errors when I click "Log On".
  • I tried to create a new pattern by basing it on the installed MVC Sample. The resulting pattern does not build: the generated code keeps referring to 'MVCAppToolkit'. I couldn't find the place where this identifier is set. Also some icons were missing. And all assets (such as the text template) were missing.

Could you answer a few questions I still have:
  • What exactly is the API for interacting with pattern elements in the text templates? I can see that you can provide a CodeIdentifier and that will mean that you can refer to it like an interface. But what properties are defined on that interface? I see InstanceName used in some places, and I see that you can define your own variables. Are there any other built-in properties all such interfaces share? How about collections? How do I loop over the elements of a collection?
  • Is there some way to debug the T4 template when it is instantiated in the Experimental Instance of Visual Studio?
  • What is the status of the Pattern Toolkit project at Microsoft? Who are developing it and is there some kind of roadmap?
Thanks,
Steven

 

LinkedIn profile: http://www.linkedin.com/in/zteven

Aug 24, 2012 at 8:10 AM

Any luck with this, I can't get it to work either?

Regards,

Craig Gardiner.

Developer
Aug 27, 2012 at 9:22 PM

1) For the text template you have full access to the pattern model. There are a few things to note. The sample code in the unfolded template casts the Current Element to IProductElement

var currentElement = (IProductElement)this.Element;

These are the generic pattern model types. VSPAT automatically creates a strongly typed set of classes for your specific collections and elements (IWorkflows, IController, etc).

In most cases, you will want to cast it to more specific model element type for your pattern using the .As<IType>() operation:

var people = currentElement.As<IPeople>();

If, for example you have an element Person with a 0 to many relationship to the pattern root, you would access the list of Person elements (IPerson) like this:

var persons = pattern.DefaultView.Persons

Then you can iterate over the collection and access all of the properties:

foreach (IPerson person in persons) {

string name = person.InstanceName;

int age = person.Age;

string gender = person.Gender;

}

You can see all of the properties and collections for your pattern in the GeneratedCode folder by examining the CollectionInterface.cs and ElementInterface.cs files. Note: from the root you must always traverse through a View. In most cases you will only have one View, called the DefaultView.

2) You can set breakpoints in the template.  First, be sure template debugging is enabled in the Template directive at the top of the template

<#@ Template Inherits="Microsoft.VisualStudio.Patterning.Library.ModelElementTextTransformation" HostSpecific="True" Debug="True" #>

The breakpoint will remain an open circle until it is actually executed. Sometimes I have had inconsistent results where the breakpoint is not hit. But if you retry a few times, it should work. VS will open the template file from the Extensions directory for the actual step-through (you will see two copies of the file open in VS). WARNING: Don't get fooled into modifying that file, because it will be overwritten by the one in your VSPAT project the next time you build.

3) VSPAT is being maintained by the community at this time. I don't know the specific roadmap. Jezz Santos would be the one to ask.

 

Hope this helps.