DCSIMG
How to add custom types to DSL and how to serialize/deserialize them - IHateSpaghetti {code}

IHateSpaghetti {code}

VSX, DSL and Beyond by Eyal Lantzman

Syndication

Coding / Architecture

Extensibility /DSL

Projects

Articles

How to add custom types to DSL and how to serialize/deserialize them

Simple types such as string's and int's are nice but if you really want to leverage you DSL and use you types you need a couple of things:


1) You need to add it to the DSL designer

2) You need to serialize it somehow

The first part is not very hard there two ways to do it:


1) Edit you DslDefinition.dsl in xml editor – look for //Types/ExternalType and add one of your owns (don’t forget to reference the assembly)
2) You can use DSL Explorer window and add it from there – just click on the root node (you language) and then “Add External Type”

AddExternalType


Regarding the second part it’s a little bit tricky:


1) you need to create a “CustomCode” Directory and then add Serializer.cs and remove the “.CustomCode” suffix from the namespace.
2) Now if you element is in ExampleElement then you will have set IsCustom = true on DSL Explorer->Xml Serialization Behavior->ExampleElement.

CustomizeSerialization
3) Click “Transform All Templates” and just fix the build errors by implementing the custom seralization/deserialization in Serializer.cs you’ve created in step 1 (use partial class called ExampleElementSerializer) you can look on the other model elements for examples.

 

I know that the serialization/deserialization is a bit hard and I'll try to post on a different approach when it will be applicable.

Update: Duncan has pointed out another way - serialize you model to xml and then use T4 to generate your hand crafted stuff from in-memory model - check out his samle,

Published Wednesday, November 12, 2008 7:20 AM by Eyal

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: