Shawn
Shawn

Reputation: 19803

What's a good architecture for generating JavaScript from C#?

I have an set of objects in C# that are used to generate JavaScript. There is a Web UI where you choose a ton of options, this builds up a large object in C#. The object is then converted to JavaScript using a bunch of custom functions. This JavaScript is the core of our application, it is run server-side using the V8 engine.

Currently the JavaScript is actually generated on the client and it's a huge mess, impossible to test, difficult to maintain, etc. It uses a ton of for loops and string concatenation to generate the JS. I'd like to move this generation into C# and make it more testable. What are some good methods for generating this JS? Is string concatenation still my best option?

Upvotes: 4

Views: 424

Answers (1)

mikerobi
mikerobi

Reputation: 20878

If the structure of the Javascript is extremely predictable (which I assume because it is possible for you to use string concatenation), you might be able to use a text templating language such as NVelocity or DotLiquid. If it is completely unpredictable, you might need to generate a full Abstract Syntax Tree, and generate the code from that.

Upvotes: 2

Related Questions