简体   繁体   English

从C#生成JavaScript的良好架构是什么?

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

I have an set of objects in C# that are used to generate JavaScript. 我在C#中有一组用于生成JavaScript的对象。 There is a Web UI where you choose a ton of options, this builds up a large object in C#. 有一个Web UI,您可以在其中选择大量选项,这会在C#中构建一个大对象。 The object is then converted to JavaScript using a bunch of custom functions. 然后,使用一系列自定义函数将对象转换为JavaScript。 This JavaScript is the core of our application, it is run server-side using the V8 engine. 此JavaScript是我们应用程序的核心,它使用V8引擎在服务器端运行。

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. 当前,JavaScript实际上是在客户端上生成的,它非常混乱,无法测试,难以维护等。它使用大量的for循环和字符串连接来生成JS。 I'd like to move this generation into C# and make it more testable. 我想将这一代人移植到C#中并使其更具可测试性。 What are some good methods for generating this JS? 有什么好的方法可以生成此JS? Is string concatenation still my best option? 字符串串联仍然是我的最佳选择吗?

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 . 如果Javascript的结构是非常可预测的(我假设是因为可以使用字符串连接),则可以使用文本模板化语言,例如NVelocityDotLiquid If it is completely unpredictable, you might need to generate a full Abstract Syntax Tree , and generate the code from that. 如果它是完全不可预测的,则可能需要生成完整的抽象语法树 ,并从中生成代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM