[英]How can I generate a downloadable c# class from within an ASP.NET MVC website?
I'm sure this is a very weird requirement, but here goes. 我确信这是一个非常奇怪的要求,但是这里有。
We have some customer configuration code that is represented by C# classes that we compile into a class library. 我们有一些客户配置代码,由我们编译成类库的C#类表示。 Creating these configuration classes is currently monkey work that I'd like to assign to someone other than a developer.
创建这些配置类目前是我想要分配给开发人员以外的人的猴子工作。 We're therefore building a "Configurator" website that a business analyst can use to generate valid C# classes by filling in a form with the relevant options selected.
因此,我们正在构建一个“Configurator”网站,业务分析师可以通过填写包含所选相关选项的表单来生成有效的C#类。
What is the best way to generate a C# class from a template in an MVC website? 从MVC网站中的模板生成C#类的最佳方法是什么? Note that I'm not talking here about dynamically generating and executing a class at runtime: we're essentially generating a text file that just happens to contain C# code that will be included in a totally different project.
请注意,我不是在谈论动态生成和在运行时执行类:我们实际上生成的文本文件恰好包含将包含在完全不同的项目中的C#代码。
This is proving to be a very difficult topic to google properly! 这被证明是谷歌正确的一个非常困难的话题! Options that have come up so far...
到目前为止出现的选项......
Any ideas on how best to approach this? 关于如何最好地接近这个的任何想法?
As requested, here's an example of the output class/file. 根据要求,这是输出类/文件的示例。 Values in here override values set in a base class with customer-specific labels, validation, etc. This is how we customise our product for customers.
此处的值覆盖基类中设置的值,具有客户特定的标签,验证等。这就是我们为客户定制产品的方式。
public class AreaRiskTemplate: AreaRiskTemplateBase
{
public override string EntityName { get { return "risk"; } }
public override string EntityNamePlural { get { return "risks"; } }
public override string EntityNameArticle { get { return "a"; } }
public override string CGovItemRefPrefix { get { return "R"; } }
public override void SetFieldDefinitions()
{
FieldDefinitions.Level1.IsImplemented = true;
FieldDefinitions.Level1.Label = "Category";
FieldDefinitions.Level1.IsVisibleInGrid = true;
FieldDefinitions.Level2.IsImplemented = true;
FieldDefinitions.Level2.Label = "Team";
FieldDefinitions.Level2.IsVisibleInGrid = true;
FieldDefinitions.Description.IsImplemented = true;
FieldDefinitions.Description.Label = "Description";
FieldDefinitions.Description.IsVisibleInGrid = true;
}
public override ModelStateDictionary Validate()
{
var msd = new ModelStateDictionary();
// add template-specific validation here
msd.Merge(base.Validate());
return msd;
}
public override List<string> Level1Options
{
get
{
return new List<string>
{
"Organisational",
"Financial",
"Operational",
"External",
"IT"
};
}
}
}
Update: currently investigating T4 templates at http://msdn.microsoft.com/en-us/library/ee844259(v=vs.100).aspx (thanks to comments below). 更新:目前正在http://msdn.microsoft.com/en-us/library/ee844259(v=vs.100).aspx上调查T4模板(感谢下面的评论)。 Looks promising.
看起来很有希
I suggest avoiding the website, and instead simply going with CodeSmith. 我建议避开网站,而只是简单地使用CodeSmith。 You can design CodeSmith scripts so that it will ask for information and then generate the required code.
您可以设计CodeSmith脚本,以便它会询问信息,然后生成所需的代码。 Seems like a lot less work, although you do have to buy CodeSmith.
虽然你必须购买CodeSmith,但似乎工作量少得多。
The method for doing this with the least overhead would be to use Microsoft's built in CodeDOM API . 以最少的开销执行此操作的方法是使用Microsoft内置的CodeDOM API 。 It allows you to build and manipulate CLR code documents with cross-language compatible Document Object Model.
它允许您使用跨语言兼容的文档对象模型构建和操作CLR代码文档。
The only downside to this approach is that it is not immediately obvious how to implement some language specific features. 这种方法的唯一缺点是如何实现一些特定于语言的功能并不是显而易见的。 An example is C#
readonly
fields, because they are not available in every CLR language, but only in C#. 一个例子是C#
readonly
字段,因为它们不是在每种CLR语言中都可用,而是仅在C#中可用。 The solution for this particular intricacy is to use a CodeSnippet
and insert the manual code as a string. 这种特殊复杂性的解决方案是使用
CodeSnippet
并将手动代码作为字符串插入。 Other more complex mismatch issues can be mitigated by integrating the CodeDOM with a templating engine. 通过将CodeDOM与模板引擎集成,可以减轻其他更复杂的不匹配问题。 Oleg Sych wrote a useful article on integrating CodeDOM with the T4 templating engine .
Oleg Sych写了一篇关于将CodeDOM与T4模板引擎集成的有用文章 。 I prefer this kind of approach to a pure text-generation approach, because a strongly typed document object model is less error prone than pure text.
我更喜欢这种纯文本生成方法的方法,因为强类型文档对象模型比纯文本更不容易出错。
In addition, if you ever do make the move to wanting the output code to be compiled, this is also supported by the CodeDOM API. 此外,如果你曾经想要编译输出代码,CodeDOM API也支持这一点。
If this is a practice that you think you may use a lot, you may want to wrap some common DOM manipulation functionality into fluent method calls by using the builder pattern . 如果这是您认为可能会使用很多的实践,则可能需要使用构建器模式将一些常见的DOM操作功能包装到流畅的方法调用中。 I've built a personal library that uses this approach and it has been very successful in increasing my productivity via code generation.
我已经构建了一个使用这种方法的个人库,它通过代码生成非常成功地提高了我的工作效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.