Using DSL to generate C # code

Currently, the project I'm working with does not have completely fixed models (due to external influences), and therefore, I would like to write them a little flexibly. Currently, they are replicated at three different levels of the application (db, web api and client), and each of them has the same logic (for example, verification).

I was wondering if there is an approach that would allow me to write a model file (say, in ruby), and then convert this model to the necessary C # files. Nowadays, it seems that I'm just writing a lot of template code that can change at any stage, while this generated approach will allow me to focus on much more important things.

Does anyone have recommendations for something like this, dsl / language, in which I can do this, and does anyone have any experience regarding something like that?

+6
c # code-generation dsl
source share
3 answers

This is easy to do with ANTLR . If the output is similar enough, you can simply use the text template engine, otherwise it can generate an abstract syntax tree for you.

+5
source share

I saw a system that used partial classes and partial methods to regenerate code without affecting user code. "Rules engine" if you are completely formed from the Visio status diagram. This is basically a bad mans workflow, but very easy to modify. The Viso diagram was exported to XML, which was read using powershell and T4 to generate classes.

The above example is for external DSL. I.E. external to the programming language in which the application runs. Alternatively, you can create an internal DSL that is implemented and used in a programming language.

This and the previous article on Code-Magazine 's DSLS article are pretty good.

In the link above, Neal Ford shows you how to create an internal DSL in C # using a free interface.

One thing he hasn't mentioned yet is that you can put this [EditorBrowsable (EditorBrowsableState.Never)] attribute in your methods so that they don't appear in intellisense. This means that you can hide methods other than the DSL (if you want) class from the DSL user, which makes the free API more understandable.

You can see that this video is currently recording Daniel Cazzulino ’s free interface about writing an IoC container with TDD

Regarding external DSLs, you also have the Oslo option (CTP for now) , which is powerful enough to allow you to create external DSLs that can be executed directly, and not to use code generation, which concludes that this is not a very big part of DSL at all.

+5
source share

I think you're on the right track.

What I usually do in this situation is a simple language design that captures my needs and creates the LL1 parser for it (recursive descent).

If the language should have non-trivial C # syntax in it, I can either specify this, or simply wrap it in brackets that I can recognize, and simply pass it to the output code.

I can either generate the structure of the parse tree and generate, say, 3 different types of code, or I can just make it generate the code on the fly, either using a mode variable with 3 values, or just write code for 3 different output files.

There is more than one way to do this. If you are afraid to write parsers (as some programmers do), SO has a lot of other help.

+1
source share

All Articles