Skip to content

Conversation

@jods4
Copy link
Contributor

@jods4 jods4 commented Dec 9, 2025

A long time ago I floated the idea of using text templates to generate code instead of CodeDom.

I finally got to do a prototype. This is not ready to merge but here's what already committed:

  1. I included Scriban library in the project and quickly hacked it into the code to generate the files instead of CodeDom.
  2. I created a Scriban template to generate proof-of-concept C#. More about what it supports below.
  3. The model that drives the template is still the same CodeDom, I haven't changed the C# side of things yet. This is a temporary hack of course. The end goal is to replace that code by a clean POCO "data" model of the schema to generate and getting rid of CodeDom completely.
  4. I have created a few temporary helpers methods in ScribanGlobals. Those methods are used inside the template to "extract" from CodeDom information about the model being generated.

Please bear in mind the template is a proof of concept but it's not final. It could be better split in smaller parts, the model could have better names, we can add some comments, etc.

It's already somewhat capable, though. I have experimented with schema Microsoft.Search.Query.xsd which is reasonably large and makes use of:

  • Elements and attributes, both optional and required
  • Elements with simple content
  • Default values
  • Enums
  • Restrictions on atom types such as string

If you run the code on this branch, the generated code is exactly the same as the current generator if you ignore whitespace differences. I was not masochist enough to match whitespace 1:1. Generating exactly the same code helps ensure there is no regression.

Tip

There is a VS Code extension to highlight the Scriban syntax

What would be the next steps?

I was surprised how quickly I was able to put this together, which speaks in favor of templates.
I am also extremely tempted to modify the templates to improve generated code, something I never felt empowered to do in CodeDom. But I won't do that before a 1:1 match of every generated file is possible and merged. Improvements are planned but will come afterwards.

The big amount of work right now is to (1) remove CodeDom and generate a data-only equivalent model; (2) support all features, this prototype is far from complete.

Things that could be possible in the future but I don't envision in this PR:

  • Having multiple sets of templates, maybe to generate code in other languages like VB or F#... or even non-.NET languages!
  • Support bring-your-own template where users could fix/extend/modify templates. I mean... they can easily do that locally but I don't plan on having a system to load a modified template stored elsewhere.

@mamift That's a drastic change but I believe it's for the better. What do you think? Should I continue working on this?

@jods4 jods4 marked this pull request as draft December 9, 2025 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant