[译]Smart Code Generator .Net-具有Visual Studio和ASP.NET的代码生成经验-体系结构概述.
By robot-v1.0
本文链接 https://www.kyfws.com/applications/smart-code-generator-net-code-generation-experienc-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 24 分钟阅读 - 11747 个词 阅读量 0[译]Smart Code Generator .Net-具有Visual Studio和ASP.NET的代码生成经验-体系结构概述.
原文地址:https://www.codeproject.com/Articles/17056/Smart-Code-Generator-Net-Code-Generation-experienc
原文作者:Shahed.Khan
译文由本站 robot-v1.0 翻译
前言
This article describes how I coupled Visual Studio and ASP.NET and created a very powerful code generator. Also describes the internals of SmartCodeGenerator and how it is possible to extend the framework to meet your needs. SmartCodeGenerator uses cutting edge technologies that are available today.
本文介绍了如何将Visual Studio和ASP.NET耦合在一起,并创建了一个非常强大的代码生成器.还描述了SmartCodeGenerator的内部结构以及如何扩展框架以满足您的需求. SmartCodeGenerator使用当今可用的尖端技术.
背景(Background)
我在使用代码生成工具,发现Code Smith看起来很有前途,它使用ASP.NET主机引擎来解析和生成输出.我开始在ASP.NET和ASP.NET主机引擎上进行更多研究,并且意识到可以通过普通的ASP.NET和Visual Studio来完成在Code Smith中完成的所有工作.结果,我发明了SmartCodeGenerator.这项研究的大部分是在一年前完成的,但是我忙于(I was playing with code generation tools and came across Code Smith which seemed very promising and uses an ASP.NET host engine to parse and generate output. I started studying more on ASP.NET and the ASP.NET Host Engine and realized that all that is done in Code Smith can be done via plain ASP.NET and Visual Studio. As a result, I invented SmartCodeGenerator. The large majority of this research was done about a year ago, however, I got busy with) Pageflakes(Pageflakes) 和其他项目,但没有机会发表我的任何作品.无论如何,最后我安排了一些时间并发表了(and other projects and did not get a chance to publish any of my work. Anyway, finally I arranged some time and published) SmartCodeGenerator(SmartCodeGenerator) (SCG)在((SCG) at) Codeplex(Codeplex) .(.)
有关最新的源代码和更新,请参阅(For the latest source code and updates please refer to) Codeplex(Codeplex) .本文基于CTP2.5,并且更多地是SCG的体系结构概述.要熟悉如何使用SCG生成基于文本的输出,请参阅以下位置的教程(. This article is based on CTP2.5 and is more of an architectural overview of SCG. To get familiar on how to use SCG to generate text based output, please refer to the tutorials at) SmartCodeGenerator快速入门v2.0(SmartCodeGenerator Quick start v2.0) 并在代码项目中查看此文章"(and check this Article at Code Project “) SmartCodeGenerator-使用Visual Studio和ASP.NET进行代码生成的经验-用法概述(SmartCodeGenerator - Code Generation experience with Visual Studio and ASP.NET- Usage Overview) “.有关允许共享基于SCG的资源和思想的用户论坛,请参阅(”. For the user forum which allows the sharing of SCG-based resources and ideas, please refer to) SmartCodeGenerator社区站点(SmartCodeGenerator Community Site) .(.)
介绍(Introduction)
SmartCodeGenerator(SCG)是一个ASP.NET 2.0网站或ASP.NET 1.1 Web应用程序,并且是一个完全基于模板的成熟代码生成器,可让您生成任何文本语言的代码.模板在Visual Studio中作为ASP.NET用户控件编写.可以使用属性来自定义代码生成过程.(SmartCodeGenerator (SCG) is an ASP.NET 2.0 website or an ASP.NET 1.1 web application and is a fully-fledged template-based code generator that allows you to generate code for any text language. Templates are written in Visual Studio as ASP.NET User Controls. The code generation process can be customized by using properties.)
SmartCodeGenerator的当前功能列表包括:(The current feature list of SmartCodeGenerator includes:)
- 所有开发都在VS2005或VS2003中完成(All development done in VS2005 or VS2003)
- 可扩展模板生成引擎(Extensible Template Generation Engine)
- MS SQL Server,MySQL和Oracle数据库架构发现API(MS SQL Server, MySQL & Oracle Database schema discovery API)
- 使用现有的ASP.NET 2.0网站应用程序或ASP.NET 1.1 Web应用程序概念(Uses existing ASP.NET 2.0 website application or ASP.NET 1.1 web application concepts)
- 生成基于文本的输出(Generates text based output)
- 以批处理方式生成输出(Generates output in batch mode)
- 完全可定制的基于模板的代码生成(Fully customizable template-based code generation)
- Visual Studio提供的Intellisense,编译,调试,代码段,sourceview,designview和所有其他无数酷功能.(Intellisense, compilation, debug, code snippet, sourceview, designview and all other countless cool features that are offered by Visual Studio.) 使用Visual Studio通过创建ASP.NET应用程序来完成使用SmartCodeGenerator创建自定义模板的整个开发生命周期.因此,在模板生成期间,您可以使用智能感知,编译,调试,源视图,设计视图,代码隐藏文件和Visual Studio的所有其他功能(2005或2003).(The entire development life cycle of creating custom templates using SmartCodeGenerator is done using Visual Studio by creating an ASP.NET application. So during the generation of templates, intellisense, compilation, debug, source view, design view, codebehind file and all other features of Visual Studio (2005 or 2003) are available to you.)
SCG支持自定义属性,以及在(SCG supports custom properties, and any properties that are defined in the) TheProperties
该类由SCG框架自动提取,并生成相关的UI来从最终用户收集数据.您可以将任何.NET对象类型定义为(class are automatically picked up by the SCG Framework and a relevant UI is generated to collect data from an end user. You can define any .NET Object Type as properties of the) TheProperties
SCG项目中的课程.中的每种属性类型(class in the SCG project. Each property type in the) TheProperties
然后将类映射到(class is then mapped to a) UIProperty
目的.在此阶段,它支持常见的类型,例如字符串,整数,布尔值,枚举,StringCollection和ScTableSchema类型(代表任何数据库表模式).但是,SmartCodeGenerator框架是可扩展的,并且(object. At this stage it supports common types such as string, int, boolean, enum, StringCollection, and ScTableSchema type (which represents any database table schema). However, the SmartCodeGenerator Framework is extensible and) UIProperty
任何.NET类型的对象,以及(objects for any .NET types and) DBSchemaProviders
对于任何数据库源都可以轻松编写.(for any database source can be easily written.)
SmartCodeGenerator术语和动词(SmartCodeGenerator Terms and Verbs)
在阅读介绍时,您遇到过诸如财产类型,(While reading the introduction, you came across terms such as Property Type,) TheProperties
类,模板,(Class, Template,) UIProperty
,(,) DBSchemaProvider
等,您可能想知道这些是什么.因此,让我们先对其进行澄清,然后再探讨SCG的体系结构和框架.(etc., and you might be wondering what these are. So, let’s clarify them first and then we will explore the architecture and framework of SCG.)
- SmartCodeGenerator项目:仅是一个ASP.NET 2.0网站项目或一个ASP.NET1.1 WebApplication.(SmartCodeGenerator Project: is simply an ASP.NET 2.0 website project or an ASP.NET1.1 WebApplication.)
- SmartCode Generator模板:是ASP.NET用户控件.(SmartCodeGenerator Template: is ASP.NET User Control.)
- 属性类型:任何.NET类型.示例包括:字符串,布尔值,整数或任何自定义对象,例如客户,员工等.(Property Type: Any .NET Type. Examples include: string, boolean, int, or any Custom Object like Customer, Employee etc.)
TheProperties
类:只是一个.NET类,该类中定义的任何属性都会由SCG Framework自动获取,并生成相关的UI来从最终用户收集数据.例如,我定义了一个字符串属性(Class: is simply a .NET class and any properties that are defined in this class are automatically picked up by the SCG Framework and a relevant UI is generated to collect data from an end user. For example, I have defined a string property)TestProperty
.(.)
SCG框架将自动为此属性生成一个UI,如下所示.(And the SCG Framework will automatically generate a UI for this property like this.)
图:自定义属性UI(Fig: Custom Property UI)
SmartCode Generator UK属性:是ASP.NET用户控件.的(SmartCodeGenerator UIProperty: is ASP.NET User Control. The) UIProperty
对象是基于用户属性类型创建的自定义用户界面对象.(objects are custom User Interface objects which are created based on the property types of the) TheProperties
类.如(class. As) UIProperty
对象是ASP.NET用户控件,它们很容易编写.(objects are ASP.NET User Controls they are very easy to write.)
因此,您应该已经注意到,无需学习任何新技术.编写代码的方式与通常为ASP.NET应用程序编写代码的方式相同.(So you should have already noticed there is no need to learn any new technologies. Code is written as you would normally write code for an ASP.NET application.)
SmartCodeGenerator框架(SmartCodeGenerator Framework)
我将从两个不同的角度讨论SCG框架.首先,我将讨论SCG工作流程.(I will discuss the SCG Framework from 2 different perspectives. Firstly, I will discuss the SCG workflow.)
其次,我将讨论SCG Framework的内心和灵魂(Secondly, I will discuss SCG Framework’s heart and soul) SCPageBase
和(and) SCEventArgs
,以及如何(, and also how) SCGPageBase
将事件分类以创建2个不同的管道,以及如何(Events are categorized to create 2 different pipelines and how) SCEventArgs
在管道中传递.(are passed in the pipeline.)
LoadUIProperty管道:(LoadUIProperty Pipeline:)
-
OnPreLoadUIProperties
:之前致电(: Called before)UIProperty
生成.(is generated.) -
OnLoadUIProperties
:在(: Called during)UIProperty
代.(generation.) -
OnLoadUIPropertiesComplete
:后调用(: Called after)UIProperty
代(generation) 生成(输出)管道:(Generate (Output) Pipeline:) -
OnPreGenerate
:在生成输出之前调用.(: Called before generating output.) -
OnGenerate
:在输出生成期间调用.(: Called during output generation.) -
OnGenerateComplete
:输出生成完成后调用.(: Called after output generation is complete.) -
OnCleanTheProperties
:最后一次清洁活动.(: A final event to allow cleaning.) 根据职责,SmartCodeGenerator框架可以分为四个部分.(Based on responsibility, the SmartCodeGenerator Framework can be divided into four sections.) -
识别自定义属性(Identify Custom Properties)
-
生成自定义属性的UI(Generate UI for Custom Properties)
-
识别模板和输出路径(Identify Templates and Output Path)
-
生成输出.(Generate Output.)
识别自定义属性(Identify Custom Properties)
我已经看到Code Smith提供了自定义属性,这是一个非常方便的概念,最终用户可以输入自定义属性的值,并相应地生成代码.在Code Smith中,我们使用这样的标签声明自定义属性(I have seen custom properties offered in Code Smith and it’s a very handy concept where an end user can enter values for the custom properties and code is generated accordingly. In Code Smith we declare custom properties using tags like this)
将其声明为标签非常麻烦,我对这种样式并不满意.最初,我曾经将SCG的自定义属性声明为ASP.NET Profile对象,这为我免费提供了两个好处:(Declaring this as a tag is very cumbersome and I was not very happy with this style. Initially, I used to declare custom properties for SCG as an ASP.NET Profile object, which offered me two good things for free:)
- 智能支持(the intellisense support)
- 编写时,每个页面上都有配置文件对象(the Profile Object is available on every page, while writing)
SCGTemplates
.(.) 我能够进行个人资料. (点)和VS2005会动态创建一个Profile类,并在Profile对象上提供智能感知支持.据我所知,Code Smith在自定义属性上没有智能感知支持.我对此非常满意,但是当我想编写一个也支持.NET 1.1的SCG版本时,我很快意识到ASP.NET 1.1框架中没有Profile对象.我不得不稍微修改一下代码,并介绍了(I was able to do Profile. (dot) and the VS2005 would create a Profile class on the fly and give intellisense support on the Profile object. And to my knowledge, Code Smith does not have intellisense support on custom properties. I was quite happy with it but when I wanted to write a version of SCG that will support .NET 1.1 as well, I soon realized that the Profile object is not available in the ASP.NET 1.1 framework. I had to change my code a bit and introduced the)TheProperties
类达到相同的目的.(class to serve the same purpose.)
我用来定义自定义属性的方式也有所改变-不再添加标签,现在自定义属性被定义为类的属性.通过执行此更改,我可以使用VS 2005中的代码段功能.如果安装了代码段,请尝试键入” prop"并按Tab,您将看到为您自动声明了该属性.我对这种解决方案比较满意,但是不得不牺牲一件事:我不能使用(The way I used to define custom properties changed a bit also - no more tagging, and now custom properties are defined as properties of the class. By doing this change I could use the code snippets feature from VS 2005. If you have code snippets installed, try typing “prop” and press Tab you will see the property is automatically declared for you. I was happier with this solution, but had to sacrifice one thing: I cannot use the) System.Web.UI.Page
和(and) System.Web.UI.UserControl
不再需要(anymore as I need the) TheProperties
在SCGTemplates中用于智能感知.我不得不写(in SCGTemplates for intellisense. I had to write) ScPageBase
和ScTemplateBase并添加(and ScTemplateBase and add) TheProperties
作为自定义属性,因此它会在Pages和(as a custom property, so it is carried on the Pages and) SCGTemplates
.(.)
如果您已经探索过任何(If you have already explored any of the) SCGTemplate
s,您可能已经注意到它继承自(s, you might have noticed that it inherits from) SCTemplateBase
.(.)
SCG框架会选择在此定义的属性(The SCG Framework picks up properties that are defined in this) TheProperties
使用反射类,并为最终用户生成相关的UI.这个很酷的功能是现成的,但SCG框架不会强迫您将这些生成的UI用于自定义属性.您可以编写自己的向导并收集自定义属性的数据,并将任何自定义对象传递给(class using reflection and generates a relevant UI for the end user. This cool feature comes out of the box but the SCG Framework does not force you to use these generated UIs for custom properties. You can write your own wizard and collect data for custom properties and pass any custom object to the) TheProperties
的对象(object of the) ScPageBase
.然后,它将在所有具有智能感知支持的情况下提供给所有SCGTemplate.您应该已经注意到(. Then, it will be available to all the SCGTemplates with full intellisense support. You should have already noticed that the) TheProperies
的对象(object of the) ScPageBase
类期望使用System.Object类型,并且您可以分配任何有效的.NET类型.(class expects a System.Object type and you can assign any valid .NET type.)
SCG提供了这种灵活性,允许您创建自己的自定义向导(完全按照自己的样式并根据自己的要求)来收集自定义属性的数据.另外,由于您将为向导编写ASP.NET页面,因此它可能是最容易编写和扩展的页面之一.(SCG offers this flexibility to allow you to create your own custom wizard (entirely in your own style and as per your own requirements) for collecting data for custom properties. Also, as you will write ASP.NET pages for your wizard, it is probably one of the easiest to write and extend.)
生成自定义属性的UI(Generate UI for Custom Properties)
在这里,我将描述如何动态生成UI. SCG框架拾取传递给对象的对象.(Here I’ll describe how the UI is generated on the fly. The SCG Framework picks up the object passed to the) TheProperities
的对象(object of the) ScPageBase
类并使用反射来发现其属性.然后遍历它们,并检查是否为此.NET类型映射了任何UI元素,并在页面上生成它们.(class and uses reflection to discover its properties. Then it iterates through them and checks if there is any UI element mapped for this .NET Type and generates them on the page.)
.NET类型和(A .NET type and a) UIproperty
映射到(is mapped in the)**PropertyAndUIPropertyMaps.xml(PropertyAndUIPropertyMaps.xml)**您将在SCG项目的根文件夹中找到该文件.通过打开文件,您将看到类似以下内容:(file which you will find in the root folder of an SCG Project. By opening the file you will see something similar to this:)
该文件包含一个数组(This file contains an array of) PropertyTypeAndUIPropertyMap
反序列化并加载到(which is de-serialized and loaded into) UIPropertyMapCollection
.(.)
如果您仔细检查任何(If you carefully examine any of the) PropertyTypeAndUIPropertyMap
节点,您将可以注意到将.NET Type的完全限定名称映射到ascx用户控件的非常简单的映射,例如System.String映射到(nodes you will be able to notice a very simple mapping of a fully qualified name of a .NET Type to an ascx usercontrol e.g. the System.String is mapped to)ScStringUIProperty.ascx(ScStringUIProperty.ascx).(.)
现在,您可能会问SCG框架在哪里可以找到(Now you might be asking where the SCG Framework will find the)**ScStringUIProperty.ascx(ScStringUIProperty.ascx)**文件.如果打开任何SmartCodeGenerator项目,您会注意到有一个名为" PropertyControls"的文件夹. SCG Framework在此文件夹中查找映射的ascx用户控件.此外,上述XML文件中映射的所有ascx控件都存储在此文件夹中.(file. If you open up any of your SmartCodeGenerator Projects you will notice there is a Folder named “PropertyControls”. SCG Framework looks for the mapped ascx usercontrols in this folder. In addition, all the ascx controls that are mapped in the above XML file were stored in this folder.)
识别模板和输出路径(Identify Templates and Output Path)
在一个(In an) Onclick
生成按钮事件时,SCG框架会从以下位置生成基于文本的输出(event of the generate button, the SCG Framework generates text based output from) SCGTemplates
,但是框架如何知道要调用哪些模板?要了解这一点,请打开(, but how does the framework know about which templates to call? To understand this, please open the)**Default.aspx.cs(Default.aspx.cs)**文件并查看(file and look at the) _Default_OnPreGenerator
方法.您将看到类似于以下内容:(method. You will see something similar to the following:)
请注意,该框架随附(Notice the framework comes with the) InputTemplateAndOutputPath
您可以在其中定义输入模板位置和输出路径位置的类.在上面的代码中,我仅添加了几个(Class where you can define an input template location and an output path location. In the above code I have simply added a couple of) InputTemplateAndOutputPath
IOList集合的对象.(objects to the IOList collection.)
的(The) ScPageBase
IOList集合附带了类,以促进批处理模式代码的生成.(class comes with this IOList collection to facilitate batch mode code generation.)
因此,您的Templates文件夹可能最终包含数百个模板,但是您可以从本节中运行所需的模板.只需映射所需的输入和输出(使用(So, your Templates folder may end up having hundreds of templates but you can run your desired templates from this section. Simply map your desired input and output (using the) InputTemplateAndOutputPath
对象),然后像上面一样将它们添加到列表对象(IOList)中.(object) and add them to the list object (IOList) like I did above.)
在这一阶段,我们必须通过编写代码将对象手动添加到IOList中,但是可以创建一个UI/向导来映射(At this stage, we have to add objects to the IOList manually by writing code, but a UI / Wizard could be created to map) InputTemplate
和(and) OutputPath
.在设计UI时,我们要记住的一件事是,我们必须让框架知道从何处读取模板以及在何处生成输出.将来,我计划编写一个UI/向导来简化映射过程.(. While designing the UI the one thing we have to keep in mind is we have to let the framework know where to read the template from and where to generate output. In future I have plans to write a UI/Wizard to ease up the mapping process.)
产生输出(Generate Output)
这是我生成输出的最后一步.打开(This is the final step where I generate the output. Open up the)**default.aspx.cs(default.aspx.cs)**文件.请注意以下两种方法的作用.(file. Notice what is done in the following 2 methods.)
在这里,我遍历InputOutputTemplate列表(IOList)并通过调用生成文件(Here I iterate through the InputOutputTemplate list (IOList) and generate files by calling) _Util.GenerateOutputAsFile
SmartCodeGenerator框架随附的方法.我还提供了(method that comes with the SmartCodeGenerator Framework. I also provided the) _Util.GeneratedOutputAsText
该方法将生成的输出作为字符串返回.(method which will return the generated output as string.)
现在让我们从管道的角度看一下SCG框架.我将在本节中讨论(Let’s now look at the SCG Framework from the pipelines point of view. I will discuss in this section) DefaultPage
,(,) SCPageBase
,(,) SCEventArgs
和SCG管道.(and the SCG Pipelines.)
默认页面(The Default Page)
默认页面在SCG框架中负有巨大责任.它在上述职责之间进行协调.它充当UI的主机,并提供"生成"按钮以启用基于文本的输出.另外,它报告生成哪些文件.默认页面继承自(The Default Page has a huge responsibility in the SCG Framework. It coordinates among the responsibilities mentioned above. It acts as the host for the UI and offers a Generate Button to enable text based output. In addition, it reports which files are generated. The Default page inherits from) ScPageBase
类并使其自身陷入SCG框架中的某些事件.让我们详细看看它们.(class and hooks itself in to some of the events in the SCG Framework. Let’s look at them in detail.)
公共局部类_Default:(public partial class _Default :) ScPageBase
ScPageBase事件(ScPageBase Events)
ScPageBase
事件可以根据其职责进行分类,如下所示:(Events can be categorized according to their responsibilities as follows:)
LoadUIProperty管道:(LoadUIProperty Pipeline:)
-
OnPreLoadUIProperties
:之前致电(: Called before)UIProperty
生成.(is generated.) -
OnLoadUIProperties
:在(: Called during)UIProperty
代.(generation.) -
OnLoadUIPropertiesComplete
:后调用(: Called after)UIProperty
代(generation) 生成(输出)管道:(Generate (Output) Pipeline:) -
OnPreGenerate
:在生成输出之前调用.(: Called before generating output.) -
OnGenerate
:在输出生成期间调用.(: Called during output generation.) -
OnGenerateComplete
:输出生成完成后调用.(: Called after output generation is complete.) -
OnCleanTheProperties
:最后一次清洁活动.(: A final event to allow cleaning.) 在深入讨论之前,我还想介绍一下(Before going deeper in to the discussion, I also want to introduce the)ScEventArgs
类.(class.)
的(The) ScEventArgs
类只是继承自(class simply inherits from) EventArgs
并具有一个名为Item的Hashtable属性.现在您可能会问为什么我添加了Hashtable对象?这将充当管道中的属性包,并且可以在管道中的任何位置进行访问.如果你看(and has a Hashtable property named Item in it. Now you might be asking why I added a Hashtable object? This will act as a property bag in the pipeline and can be accessed anywhere along the pipeline. If you look at the) Page_Load
默认页面的事件,您将看到以下代码:(event of the Default page you will see the following piece of code:)
在这里我创建了一个(Here I created a) Hashtable
目的(object) dict
并称为(and called the) ScPropertyBase.LoadUIProperties_PipeLine
方法并通过了(method and passed the) PanelProperties
(ASP.NET Panel)作为管道的一部分((ASP.NET Panel) in the pipeline as part of the) ScEventArgs
.(.)
LoadUIProperties管道(LoadUIProperties Pipeline)
的(The) LoadUIProperties_Pipeline
来电(calls) PreLoadUIProperties
,(,) LoadUIProperties
和(and) LoadUIPropertiesComplete
按顺序.您可以使用这些事件并钩住您自己的代码,它们将被触发.默认情况下,我要做的是将一些代码连接到(in sequence. You can use these events and hook your own code and they will be fired. By default, what I do is I hook up some codes to the) this.OnLoadUIProperties
使用反射来遍历属性的事件(event that uses reflection to iterate through the properties of) TheProperties
类并加载自定义属性的UI.完整的代码如下所示:(class and load the UI for custom properties. The complete code looks like the following:)
上面的代码是不言自明的.(The above code is self explanatory.)
生成(输出)管道(Generate (Output) Pipeline)
如果您打开(If you open up the)**Default.aspx.cs(Default.aspx.cs)**文件,您将看到以下代码:(file you will see the following piece of code:)
还有另一个用于生成基于文本的输出的管道,并且在单击"生成"按钮时执行该管道.在里面(There is another pipeline for generating text based output and this pipeline is executed when the Generate button is clicked. In the) OnClick
“生成"按钮的事件(event of the Generate button I prepare an) ScEventArgs
对象并将其传递给管道.的(object and pass it to the pipeline. The) Generate_Pipeline
来电(calls) PreGenerate
,(,) Generate
,(,) GenerateComplete
和(and) CleanTheProperties
按顺序.您会发现此管道更有趣,因为所有(in sequence. You will find this pipeline more interesting as all the) UIProperties
和(and) SCGTemplates
挂接到该管道.打开任何(are hooked in to this pipeline. Open up any) UIProperty
在您的项目中,您会注意到以下模式,这里的代码段来自(in your project you will notice this following pattern, here the code snippet from) ScStringUIProperty
:(:)
在构造函数中,我将其连接(In the constructor I hook up this) UIProperty
到(to the) ParentPage.OnPreGenerate
事件处理程序,在这种情况下,父页面是(event handler, and in this case the parent page is the)**Default.aspx(Default.aspx)**页.这样做是因为我需要确保使用最终用户的最新输入生成基于文本的输出.在这里我更新了(page. This is done as I need to ensure that the text based output is generated with the end user’s most recent input. Here I updated the) TheProperties
最终用户在文本框中输入最新数据的对象.(object with the end user’s most recent entered data in the textbox.)
如果您打开(If you open the)**Default.aspx.cs(Default.aspx.cs)**在页面上,您还将注意到,我还将其他两种方法也连接到了管道.让我们探索它们.(page you will also notice that I hooked up a couple other methods to the pipeline as well. Let’s explore them.)
上面的代码很容易解释.在里面(The above code is fairly self explanatory. In the) OnPreGenerate
我准备IOList集合对象的事件处理程序,向其中添加"报告"对象(event handler I prepare the IOList collection object, add a “report” object to the) ScEventArgs
对象并将其传递到管道中.然后在(object and pass it in the pipeline. Then in the) OnGenerate
我称之为的事件处理程序(event handler I call the) GenerateFiles
方法(稍后我会介绍这种方法),最后在(method (I am coming to this method in a moment) and finally in the) OnGenerateComplete
事件处理程序我从管道中检索"报告"对象,并在ASP.NET Label控件中显示数据.结果,您将看到类似的报告输出,如下所示:(event handler I retrieve the “report” object from the pipeline and display the data in an ASP.NET Label control. As a result you see similar output of the report as below:)
好,让我们来探索(OK, let’s explore the) GenerateFiles
方法更详细:(Method in more detail:)
在这里,我遍历IOList集合,可以调用(Here, I iterate through the IOList collection and can call the) _Util.GenerateOutputAsFile
或_Util.GenerateOutputAsString方法.另外,请注意传递了” e"对象.(or _Util.GenerateOutputAsString methods. Also, notice the “e” object is passed.)
在这里,我动态加载了映射(Here, I dynamically load the mapped) UserControl
并写入从中获取的字符串(and write the string retrieved from) GetGeneratedTemplate
归档方法.请注意," e"对象已传递给(method to file. Notice the “e” object has been passed to the) GetGeneratedTemplate
方法.(method.)
最后,这是一段神奇的代码,称为(Finally, this is the magical piece of code called) GetGeneratedTemplateCode
这使我能够创建SmartCodeGenerator.(that enabled me to create SmartCodeGenerator.)
这不是火箭科学.(It is not rocket science.) ctrl.RenderControl
做整个工作.在这里,我也称(does the whole job. Here, I also call the) PreGenerateTemplateCode
方法并传递" e"对象.仔细看一下这段代码:(method and pass “e” object. Look carefully at this piece of code:)
这就是为什么所有(This is why all) SCGTemplates
您编写的内容需要从(that you write needs to inherit from the) ScTemplateBase
类并需要实现该方法(class and need to implement the method) PreGenerateTemplateCode
.您还可以注意到," e"对象已传递给所有SCGTemplates,因此,如果需要在任何模板中使用它,则可以.这也可以充当在模板之间进行通信的属性包.因此,管道通过(. You can also notice that the “e” object is passed to all the SCGTemplates so if you need to use it in any template, you can. This can also act as a propertybag to communicate among the templates. So, the pipeline passes the) ScEventArgs
一直这样的对象:(object all the way like this:)
PreGenerate
=>(=>) Generate
=>(=>) GenerateComplete
=>(=>) CleanTheProperties
.(.)
最后,如果您需要进行任何清理,可以使用(And finally if you need to do any clean up, you can do this using the) OnCleanTheProperties
事件.(event.)
DBSchemaProvider(The DBSchemaProvider)
使用SmartCodeGenerator,您将获得用于MS SQL Server,Oracle和MySQL数据库引擎的数据库架构发现API.我已经使用Microsoft®Provider Design Pattern编写了Schema Discovery API.要进一步了解提供者模式,请参阅我之前的文章"(With SmartCodeGenerator you get a Database Schema Discovery API for MS SQL Server, Oracle and the MySQL database engines. I have used Microsoft® Provider Design Pattern to write the Schema Discovery API. For a refresher on the Provider Pattern, have a look at my previous article “) 使用提供程序模式的灵活且基于插件的.NET应用程序(Flexible and Plug-in-based .NET Applications Using the Provider Pattern) “.为了让您发现数据库架构详细信息,提供程序附带以下签名.(”. To allow you to discover the database schema details, the providers come with the following signature.)
要为其他数据库编写新的提供程序,您要做的就是实现这些方法.我如何实现MS SQL Server,Oracle和MySQL提供程序的所有源代码都可以从以下位置下载:(To write a new provider for a different database all you have to do is implement these methods. All the source code of how I implement providers for MS SQL Server, Oracle and MySQL is available for download from) CodePlex(CodePlex) .(.)
编写SVG模板并使用SmartCode Generator(Writing SCG Templates and Using SmartCodeGenerator)
如果您已经知道如何编写ascx UserControl,那么这里没有什么新的知识可学.您要做的就是编写一个ascx UserControl,但要记住从继承该UserControl(There is nothing new to learn here if you already know how to write an ascx UserControl. All you have to do is write an ascx UserControl, but remember to inherit the UserControl from) ScTemplateBase
类,并确保您实施了(class and make sure you implement the) PreGenenerateTemplateCode
方法.这是背后代码的示例:(method. Here is an example of the codebehind:)
这是一个简单的例子(And here is a simple example of a) SCGTemplate
有关如何编写模板以及如何使用SmartCodeGenerator的更多详细信息,请参阅以下网站上的教程:(For more details on how to write templates and how to use SmartCodeGenerator please refer to the tutorials at) http://www.smartcodegenerator.com(http://www.smartcodegenerator.com) .(.)
如何编写自己的UIProperty(How to write your own UIProperty)
你可以介绍(You can introduce) UIProperties
您想要使用的任何.NET类型.在此阶段,您将获得为您提供的string,int,boolean,enum,StringCollection,MandatoryString和ScTableSchema等的UIProperties.然而,(for any .NET Type that you wish to use. At this stage, you get UIProperties for string, int, boolean, enum, StringCollection, MandatoryString and ScTableSchema etc. provided for you. However,) UIProperties
也是ascx控件,非常容易编写.有关如何编写的完整教程(are also ascx controls and are very easy to write. For a complete tutorial on how to write) UIProperties
请参阅团队博客上的这篇文章((please refer to this post at Team Blog () 在SmartCodeGenerator中引入新的UIProperty(Introducing New UIProperty in SmartCodeGenerator) ).如果你碰巧写任何新的(). If you happen to write any new) UIProperty
要么(or) SCGTemplate
,我要求您与社区共享它们,并将它们上传到SmartCodeGenerator的论坛站点:(, I would request you to share them with the community and upload them to the forum site of SmartCodeGenerator:) SmartCodeGenerator社区站点(SmartCodeGenerator Community Site) .您也可以下载(. You can also download) SCGTemplates
和(and) UIProperties
由该站点的社区共享.(shared by the community from this site.)
结论(Conclusion)
SmartCodeGenerator是一个非常强大的代码生成框架.在这里,它结合了Visual Studio和ASP.NET.但是,此框架可以与世界上存在的任何ASP.NET IDE一起使用,并且可以生成代码.您只需要创建一个ASP.NET项目,添加对SCG随附的DLL的引用. SCG还随附了适用于MS SQL Server,Oracle和MySQL的数据库架构发现API,并且为不同的数据库实现新的提供程序非常容易. SCG项目只是一个ASP.NET项目,不依赖任何第三方工具,而SCGTemplates和UIProperty是ascx UserControls.对于ASP.NET开发人员来说,开始使用SmartCodeGenerator没有什么新鲜的知识.我坚信您会同意我的说法:“代码生成从未如此简单”.(SmartCodeGenerator is a very powerful code generation framework. Here it couples Visual Studio and ASP.NET. However, this framework can be used with any ASP.NET IDE that exists in the world and code can be generated. You simply need to create an ASP.NET project, add a reference to the DLLs supplied with SCG. SCG also ships with Database Schema Discovery API’s for MS SQL Server, Oracle and MySQL, and implementing new providers for different databases is very easy. An SCG Project is simply a ASP.NET project and not dependent on any third party tools, and SCGTemplates and UIProperties are ascx UserControls. For a ASP.NET developer there is nothing new to learn to start using SmartCodeGenerator. I strongly believe that you will agree with me in saying: “Code Generation has never been this easy”.)
谢谢您到目前为止与我在一起,请加入(Thank you for being with me so far and please join the) 社区(community) 并共享您的SCGTemplates和UIProperty.(and share your SCGTemplates and UIProperties.)
证明阅读者:克里斯托弗`希尔(Proof reading done by: Christopher Heale)
与SmartCodeGenerator相关的资源和链接(Resources and Links related to SmartCodeGenerator)
- 在以下位置下载源代码和发行版:(Download source code and releases at) Codeplex(Codeplex) .(.)
- 社区:(Community:) SmartCodeGenerator社区站点(SmartCodeGenerator Community Site) .(.)
- 社区论坛:(Community Forums:) 讨论页面(Discussions Page)
- 教程:(Tutorials:) SmartCodeGenerator快速入门v2.0(SmartCodeGenerator Quick start v2.0) .(.)
- 团队博客:(Team Blog:) 团队博客(Team Blog)
- 和我的博客:(And My Blog:) Shahed Khan(MVP)(Shahed Khan (MVP)) .(.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# VB C#2.0 Windows .NET .NET2.0 ASP.NET VS2005 Dev Architect 新闻 翻译