Few months ago I wrote a series of articles and released a library to create connectors between the entries of compartment shapes in Microsoft DSL Tools. In the meantime I found a setting that wasn’t working with my library and now it is time for version 2. I will release this new version as part of JaDAL – Just another DSL-Tools Addon Library on CodePlex.
If you don’t have any idea what I’m talking about please take a look at (at least) the first part of the series, but be careful and DO NOT read the second part! The second part contains obsolete information that will be corrected here. Part 3 and part 4 describe the inside of the library and they are still valid for the new version.
Changes from version 1 to 2
In the original version you have to decide (using the xml file) for the target and source of the connection to be mapped to a compartment or a regular shape. I thought this wouldn’t be an issue since you must know the mapping of your classes. I believed it until I created my DSL with the use of inheritance and some base class that can be mapped to regular or compartment shapes. For this base class I couldn’t create a mapping that goes from a regular or a compartment shape to a regular or compartment shape.
I removed this constraint and in version 2 you needn’t care about it. The library can handle all these cases (even the trivial mapping from regular to regular shape). And even the best: it turned out that removing this constraint made the code much clearer and simpler (both the code of the library itself and the code you have to write). Before this change there were many different cases (first regular to compartment, then compartment to regular, then compartment to compartment and at the end regular to regular which throws an exception) and now everything works the same way.
Lesson learned: If you can do something in an abstract and general way, don’t create different cases and handle only some of them!
And again: the user guide
In the 2nd part of this series I presented a user guide with a step by step walkthrough of creating a DSL using the compartment mapping. I will now create the same DSL with version 2. Most steps are still the same but I repeat them for completeness. The main difference is the xml file format at step 7 and the class you have to write yourself in steps 9 to 11.
You will find this example (and a more advanced one) in the release download over at CodePlex.
- You need TTxGen to generate some code from my templates. See my article for more information and a workaround for an installation problem.
- I assume you have a DSL solution and at least two Domain Classes mapped to two compartment shapes and two more Domain Classes used as entries of the compartment shapes created. In my example these classes are called
The entries need a unique identifier. You could use a name property for this, but there will be some difficulties if the user creates two entries in one shape with the same name, so I will use a guid. (Don’t forget to create a new guid in the constructor or another proper place.)
- Create a Reference Relationship from
Parent2. This will be automatically named
Parent1ReferencesParent2. We will use this reference to present the relationship from one entry to another. I would like to create Reference Relationships from this relationship to the entries, but relationships of relationships are not supported. We have to store the guids of the entries in the relationship and add two Domain Properties for this purpose to it. I named them
- Set the
Allows Duplicates property of the
Parent1ReferencesParent2 relationship to
- Set the
Is Custom property of the Connection Builder (
Parent1ReferencesParent2Builder) in the DSL Explorer to
- Add a reference to
JaDAL.dll to both the
- Add a new xml file (in my example
CompartmentMappings.xml) to your solution and write the following code in it:
<?xml version="1.0" encoding="utf-8" ?>
<DomainClass name="Parent1" />
(If you place the xsd file in the same directory you get IntelliSense and error checking for free.)
- Place the
CompartmentMappings.tt file in the same directory, right-click on the xml file and choose Generate xGen Template. A
CompartmentMappings_xGen.tt file will be created below the xml file and there you have to add the following lines:
<#@ ParentFileInjector processor="TTxGenDirectiveProcessor"
<#@ include file="CompartmentMappings.tt" #>
(Be sure you use the right xml file name here.)
- Now you have to write some code by yourself. In the generated cs file (
CompartmentMappings_xGen.cs) there will be a class named
Parent1ReferencesParent2BuilderInstance with three missing methods you have to override. Create a partial class in a new file and implement these methods (see 10 and 11).
- Implement the
CreateElementLink() method in this file. Here you have to add code to store the selected compartment entries for source and target in the properties of the relationship (see 3).
protected override ElementLink CreateElementLink
result = new Parent1ReferencesParent2(source, target);
// use a empty guid for the Head
result.fromEntry = Guid.Empty;
result.fromEntry = sourceEntry.Guid;
// use a empty guid for the Head
result.toEntry = Guid.Empty;
result.toEntry = targetEntry.Guid;
- In the
CreateElementLink() method you have stored the source and target entry information somewhere; my library does not know about this location. So we need two additional methods to compare an entry with a relationship and answer the question "Is this entry the source of a given relationship?" and the same for the target. These code resists in the same file:
public override bool IsEntryConnectionSource
if (entry == null)
public override bool IsEntryConnectionTarget
if (entry == null)
Always care for
entry == null, this will be used to check whether the head of the shape is meant. Even if you do not allow the head to be source or target this method will be called with a
null parameter from time to time.
- Create a
partial class for your Domain Model (
CompartmentMappingExampleDomainModel) and add the following methods.
protected override Type GetCustomDomainModelTypes()
(If you have already some custom rules, you can use an overloaded version of the
DslPackage project create a
partial class for the generated Command Set and add the following code:
protected override IList<MenuCommand> GetMenuCommands()
That’s all. The mapping from compartment entry to compartment entry should be working after compiling the solution.
As I mention above you can mix compartment and regular shapes. You only need to create a DSL with these shapes and configure the xml file another way. As you see, inside the
<Connection> element there is no longer a reference to the diagram elements, only the domain classes are used here. Obviously if you use inheritance all your classes need to have the same base class for
<DomainClass> itself and for
<EntryDomainClass>. If the concrete class is mapped to a compartment shape, the compartment mappings, otherwise a mapping of regular shapes, will be used.
If one of source or target is always a regular shape, please use
At the bottom of the xml file you have to list all shapes that play a role in your mapping. Use the xml elements
Parent1ReferencesParent2BuilderInstance class you can override some more methods to allow or forbid the creation of connectors. I think the names and signatures speak for themselves:
In addition to the sample above I build a second one. There I want to create mappings form some Inputs to some Outputs, where one of these Inputs or Outputs can be an entry of a compartment shape or a whole regular shape. This regular shape contains a property Kind that makes the shape to an Input or an Output (to show you the use of the
The DSL model looks like this:
The project with all sources is part of the samples in the JaDAL download.
The compartment mapping library and both samples are part of JaDAL – Just another DSL-Tools Addon Library which you can download from CodePlex.