Partilhar via


Tip: Where to Put Your DSL's Custom Code

Alan Wills recently released and posted about writing custom code for your DSL written with our DSL Toolkit [download zip here].  There's a good tip in the document for how to write custom code in your DSL project.

Basically, the domain model (.dsldm file) and designer definition (.dsldd file) are used to generate the code for you designer. Any time you regenerate the templates in your project, the associated code files get completely regenerated. Any code you may have added there is lost at that point. And, you may be regenerating a lot if your changing you model or shape definitions very much. The easiest way to maintain your custom code, even when code is regenerated, is to put you custom code in a separate file and use the partial classes feature in VS 2005 to allow you to place implementation code for a single class into separate files. All of the generated classes are defined as partial classes, so all you have to do is add the same class declaration in your new file, like so:

            public partial class MyShape

{ … }

Also, make sure to place the class in the same namespace as the original class definition. And, to include all of the appropriate namespaces from the generated file, so you can write code against the appropriate APIs. Finally, write any additional custom code here.

To further separate the custom code from the generated code, you may wish to put custom code files in a separate folder in you Designer project (named something like Custom).

Update: Alan just posted more on his blog about using partial classes with DSL Tools.