Data dictionary generation tools (7): NVelocity code generator and generation tool nvelocity
This system has not been updated for a long time, and people are also getting lazy. From now on, at least three articles are written every month. You are welcome to supervise it. By the way, we will announce that this series will bring you a series of WebApp development articles, so stay tuned. First, let's take a few pictures and put them at the end of the article. You are welcome to preview them!
This chapter will explain how to use NVelocity and lead you to implement a simple code generator.
For more information about NVelocity, see the NVelocity syntax in the previous article.
Reading directory
- NVelocity Implementation Code Generator
- Summary
- Preview of new series of articles
- Download tool source code
- Learning and using
Back to Top NVelocity to implement Code Generator
To implement a simple code generator, you must first define the code structure. Here, only the entity layer in the three-tier mode is generated. You can write the complex structure code generation on your own.
1. template Definition
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Mysoft.Code.Entity{ #foreach($p in $T.Rows)## #if($velocityCount==1)## ///<summary>$p.get_item("table_name_c")</summary> public class $p.get_item("table_name") { #end ///<summary>#if($p.get_item("field_name_c")!="")$p.get_item("field_name_c") #else $p.get_item("field_name") #end</summary> public#if($p.get_item("date_type")=="bigint") double#else string#end $p.get_item("field_name") { get; set; } #end } }
As you can see, the template is very simple. The final generated code is similar to the following:
Using System; using System. collections. generic; using System. linq; using System. web; namespace Mysoft. code. entity {// <summary> stage settings table </summary> public class Pack_Stage {// <summary> Update package GUID </summary> public string StageGUID {get; set ;}/// <summary> Update package name </summary> public string StageName {get; set ;} /// <summary> stage sequence number </summary> public string OrderNo {get; set ;} /// <summary> start package merging </summary> public string IsHBPackage {get; set ;}}}View Code
2. read information about all tables and tables from the database
The built-in tables and views of SQL Server provide a lot of useful information, such as querying all user tables and table column information descriptions.
Sys. tables: User table information
Sys. extended_properties: Table and column description
Select t. name AS table_name, T. OBJECT_ID, ISNULL (CONVERT (VARCHAR (MAX), E. value), '') AS table_name_c, C. name AS field_name, ISNULL (CONVERT (VARCHAR (MAX), D. value), '') AS field_name_c, ROW_NUMBER () OVER (partition by t. name order by c. colid) AS field_sequence, TYPE_NAME (C. xtype) AS date_type, (case when exists (SELECT 1 FROM sysobjects WHERE xtype = 'pk' AND name IN (SELECT name FROM sysindexes WHERE id = C. id AND indid IN (SELECT indid FROM sysindexkeys WHERE id = C. id AND colid = C. colid) THEN 1 ELSE 0 END) AS pk, ISNULL (C. isnullable, 1) AS isnullable, ISNULL (COLUMNPROPERTY (c. id, c. name, 'isidentity '), 0) AS isidentityFROM sys. tables as t left join syscolumns as c on c. id = T. object_id left join sys. extended_properties as d on d. major_id = T. object_id and d. minor_id = C. colid and d. major_id = C. id left join sys. extended_properties as e on e. major_id = T. object_id and e. minor_id = 0View Code
You can check the preceding SQL statements and query the table information. The next chapter focuses on this knowledge point.
3. Code Generation
With templates and data sources, you can generate the final code. If you do not understand the code, refer to the previous article. You can download the sample code and view it on your own.
Download this chapter
Back to the top of this Chapter
The entire code generator is still not difficult to do. The front-end interface is built with the recently popular miniui. If you are interested, you can download the research. You can modify the output path and database link of the code file in the instance in Web. Config.
In addition, I would like to share some of the issues encountered during the development process:
1. How to Use DataTable as a data source in NVelocity
# Foreach ($ p in $ T. Rows)
($ P. get_item ("date_type ")
# End
2. DataTable for Data Filtering
DataView dv = dt. DefaultView;
Dv. RowFilter = "tableid = '" + id + "'";
DataTable dt2 = dv. ToTable ();
Well, there are so many contents in this chapter. I hope it will be helpful to you after reading it!
Return to the top to preview the new series of articles.
Go back to the top tool source code for download
Currently, a total of seven versions have been upgraded and the latest version is available.
Data dictionary generation tool V2.0 Installer |
Latest Installer |
Source code of the data dictionary generation tool |
Latest source code |
Go back to the top
If you have used this tool or want to learn it, join this group to discuss the data dictionary generation tool and make it easier to use it.147425783 QQ Group.
For more information about the data dictionary generation tool, click the data dictionary generation tool topic.