Thursday, January 17, 2008

DSL - How to use DesignVerbs with shapes?

Although I've previously wrote in this post about how you could add Menu Commands to shapes, I am still searching for alternative (maybe better?) approaches for exposing commands from my domain classes.

Why? Because the other way is too complex (too many steps) and I have many actions/commands.

Every time you click on a shape, a PropertyGrid is used to expose the ShapeProperties + DomainProperties (yes, they are merged in the PropertyGrid).

So, one natural approach would be to think "Why not add DesignVerbs as done for Custom Controls?". This question was asked in the msdn forum with null replied (until now ;) )

What I did was combined the idea from Geoff Appleby with a Custom partial class for each shape (off course only for the ones you need some special command/verb).

This is how the final solution looks like:

As you can see, "My Custom Action" and "Synchronize" are my exposed Actions/Commands. Here is the code that defines what to show:

public DesignerVerbCollection Verbs
get {
DesignerVerbCollection verbs = new DesignerVerbCollection();
verbs.Add( new DesignerVerb("My Custom Action", new EventHandler(ExecuteAction1) ));
verbs.Add(new DesignerVerb("Synchronize", new EventHandler(Synchronize)));
return verbs;

private void Synchronize(object sender, EventArgs e)
ExampleElement element = this.ModelElement as ExampleElement;
if ( element == null ) return;
MessageBox.Show("Action 1 was executed on model Element:" + element.Name);

Off course it's not only that since you need to also implement 2 interfaces:

public partial class ExampleShape : DslDiagrams::NodeShape, IComponent, IVerbsHost

private ISite _site;

public ISite Site
if (_site == null)
_site = new VerbSite(this, new VerbMenuCommandService(this));
return _site;
set { _site = value; }

public interface IVerbsHost { DesignerVerbCollection Verbs { get; }}

Drop me an e-mail if you are interested in the complete source code ( blogger does not have file upload i think.. need to check..)

TODO : Next I would like to combine both techniques be able to access this design verbs not only from the PropertyGrid but also from the Shape's right mouse and DSL Explorer.

Tuesday, January 15, 2008

Cleaning "old" DSL Languages

If you are , like me, new to DSLs you will end up creating many DSL projects. ;)

After a while your "New Item" dialog will end up looking really crowded.

Don't ask me why but the "Reset the Microsoft Visual Studio 2008 Experimental hive" option does not seem to work on all cases, and I still have several old dsl languages which won't go away. ( Note: I guess that the reset option by design will not mess us local user's document as explained below)

First, I've tried , with no success, to follow the tip from "El Bruno" (spanish blog) .

What's the solution ?

You need to "remove"all the zip files in the following directories:

C:\Users\\Documents\Visual Studio 2008\My Exported Templates
C:\Users\\Documents\Visual Studio 2008\Templates\ItemTemplates\CSharp\1033
C:\Users\\Documents\Visual Studio 2008\Templates\ItemTemplates\VisualBasic\1033

Note: On every DSLPackage build , a new file .zip will be created and placed on that directory. So before trying to open your DSL make sure you build the project first.

Wednesday, January 9, 2008

Text Templating (tt) : calling custom method

The simplest re-usability technique inside .tt files would be to call a class method.

Below there is an example of how to achieve this:

It's important to notice that this syntax has changed because long time ago you could have seen it as <#! , otherwise look at this old post from Gareth

Text Templating (.tt) Validation

DSL supports a nice validation model for the Domain classes/model.

But what about the Text Templating requirements?

This are validations that apply only to a specific generation task. Let's say if have a property named DotNetType. It could be that this property does not affect generation of [t1] but I need this property for [t2]. For this cases, you will need to somehow validate in the (tt) Template itself.

Note: You could argue (please don't) that the example below is a bad example, I know but there are still some other real cases.

The 'correct' way

I must say that before finding out that I've tried this 2 other options:

  • Raise an exception (hard but work since it's integrated with the Error list as shown below):

  • Just add a comment to the output file (this is not always possible ( example : template based controls like GridView don't support XML comment inside their tpl tags..)