Modified T4 code generator (continued)

Source: Internet
Author: User

Original article address:

Http://www.cnblogs.com/ASPNET2008/archive/2012/07/29/2612954.html

 

 

 

Recently, I used my spare time to create a customer-company-styleCodeGenerator. This code generator is in the previousArticle(Modified T4 code generator), I did not start from scratch, but referred to the work of a garden friend. Features of the original work:

1: Based on WPF.
Compared with the traditional winform, WPF has great advantages in user experience.
Advantage 1: It is easy to implement visual-style interfaces.
This makes the user familiar with it and does not spend much time learning how to use it.

Advantage 2: drag the UI element freely
For example, we can easily split a page into different small blocks, and then we can drag them to different areas as needed.


2: Based on T4 Text Template

In fact, T4 Text Template came out a long time ago, but the attention has not been very high (I think ). However, since Microsoft's Asp.net MVC adopted T4 itself, it was a very successful case. When selecting a solution, one of the most important reference indicators is whether there are successful cases. With Microsoft's practice, it is enough to explain the practicality of T4.

In fact, I have always maintained a neutral attitude towards tool selection. Some developers often complain that the ready-made tools are not easy to use and cannot meet their own needs. Once the existing functions cannot be implemented, the software is not easy to use. Microsoft was not very popular with T4 on ASP.net MVC. Once someone successfully applies a certain technology to a project, we can see that this technology is still good, but we don't know how to make better use of it. Sometimes, we need the ability to improve functions on the basis of others to meet our own needs, and the premise is that we need to get an open source project, otherwise you need to start from 0.

3: it is open-source.
Only when the project is open-source can I continue my code generator from scratch. After all, it is too difficult for a person to do one thing from scratch, in addition, it is impossible for me to start from the beginning with so much time. More importantly, no one is perfect. If other works have completed functions that you are not familiar with, isn't it great.

Well, I 've talked so much about it. Let's share some of the issues I 've encountered when I modified the original work and applied T4 templeate.

Defects: Memory leakage.
When generating code from T4, we can define our own texttemplatingenginehost, which contains such a method.

Private String Domainname = " Generation app domain " ;
Public Appdomain providetemplatingappdomain ( String Content)
{
// Return appdomain. currentdomain;
Currentdomain = appdomain. createdomain (domainname );
Return Currentdomain;
}

:

It is a method in the itexttemplatingenginehost interface. In the sample code provided by Microsoft, it is also newly created when the appdomain is returned. In the code, there is a piece of commented content and the current appdomain is returned, however, I found some problems in the operation and did not know how the code was handled.

During use, we will first create a new host: texttemplatingenginehost host = new texttemplatingenginehost (). The problem is that each new host creates an appdomail, and finally finds that each call is performed, the memory will increase (1 to 3 m memory, one time multiple batches are executed, and the memory will be up to M). Finally, I will try to manually uninstall these newly created appdomail, finally, the problem is solved. For the convenience of calling, I asked texttemplatingenginehost to implement idisposable.

Public Appdomain currentdomain { Get ; Set ;}
Public Void Dispose ()
{
If ( Null ! = Currentdomain)
{
Try
{
Appdomain. Unload (currentdomain );

}
Catch (Exception ex)
{
Throw New Exception ( " Appdomainunload error! " + Ex. tostring ());
}
Finally
{
Currentdomain = Null ;

}
}
}


Then, call: Using (texttemplatingenginehost host = new texttemplatingenginehost ()){......}

Enhanced: edmx is automatically generated.

Due to the style of our company's project, we use the database first in LINQ to entity for database access, that is, we first design the database and then add edmx to the UI. So our problem was how to generate an edmx file:
Problem 1: edmx has a complex relationship. It is difficult to implement it using the T4 template.
Problem 2: each entity has a positional relationship on the UI interface, and the X-axis and Y-axis values. These values determine their layout on the interface, which is not well solved.

Solution: edmgen2.exe is encapsulated in edmgun and can directly generate edmx files. edmgen cannot directly generate edmx. It can only generate some source data files, such as CSDL and SSDL, MSL.

Edmgen2.exe problem: I wanted to call it directly to a certain extent, but I found some problems when passing parameters. When entering parameters in the Command window, we usually include the parameters with quotation marks. This allowsProgramThis is a complete parameter. The reason is that most of the EXE parameters are a string [], which separates the input parameters into arrays by spaces, if our parameters contain spaces, We Need To enclose them in quotation marks. This method is okay in the Command Prompt window, but now we want to call EXE in the application and pass the parameter to it. The following is the incorrect format:
Escape characters:

String Argus = " /Modelgen " + " \" " + Sqlconnectionstring + " \" " + " \" " + " System. Data. sqlclient " + " \" " + " \" " + Namespace + " \" " ;


Solution: Even if edmgen2 is an open-source project written in. net, why not directly call the method to avoid passing such parameters first.
First, add the edmgen2 code to the project.
Second, because edmgen2 is an EXE, we need to change the default main method name to runmain and identify it as a helper instead of a startup object.
Third, directly pass the string [] instead of the string type parameter.

String [] Argslist = New String [] { " /Modelgen " ,
Sqlconnectionstring,
" System. Data. sqlclient " ,
Namespace
};
Edmgen2.runmain (argslist );



Problem: T4 Text Template Escape Character "\".

For example, the following code:

<Projectreference include = " .. \ Businesslogic \ <# = namespace. Value #>. businesslogic. csproj " >

In this way, <# = namespace. Value #> won't be recognized as the correct syntax, and it is no problem to write it like this: Add a space before <.

<Projectreference include = " .. \ Businesslogic \ <# = namespace. Value #>. businesslogic. csproj " >

Problem: Although T4 recognizes variables at this time, we need to perform special processing, that is, after T4 generates text, we need to delete the spaces that are added, it is definitely wrong to add a space in a normal path.

Solution: T4 Escape Character "\", so that I can write it like this: we do not have to delete spaces.

<Projectreference include = " .. \ Businesslogic \\< # = namespace. Value #>. businesslogic. csproj " >


This article is a long one. I will write some remaining content in the next article:
1. How to generate a project
2. How to generate an SLN
3: how to automatically reference the generated CS file
4 :......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.