Information infrastructure improvement code generation

Source: Internet
Author: User

 

Database field ing

When you see an object attribute, can you immediately determine its corresponding database fields?

_ Languagetranslation. keytext. For example, you can see the attributes of this keytext.

The common practice is to find the bound object and database fields.CodeTo check the database fields associated with this attribute. The code may be as follows:

Public registragetranslationentity readerbind (idatareader datareader)

{

Registragetranslationentity model = new registragetranslationentity ();

Object OJB;

Model. keytext = datareader ["key_text"]. tostring ();

Model. Description = datareader ["Description"]. tostring ();

}

The database field corresponding to the keytext attribute is key_text. however, you need to use F12 to go to the definition to help you view this readerbind method. If you have added interfaces and implementations, you also need to perform additional searches to determine the field source.

One way to improve is to use XML annotations and smart sensing of C #, such

Add XML comments to the generated object file. When you point to an attribute, you can get the following results:

From this image, we can see that the keytext field mapped to the languagetransaction table is very clear.

Modify the code generator used to generate an object and add an XML comment similar to the following to the field to enhance the readability of the Code.

<Remarks> mapped on table field: "languagetranslation". "keytext" <br/>

This is my first enhancement for the code generator.

 

A group of object files

The object name is languagetranslationentity. We usually generate a corresponding languagetranslationentity. CS file.

If you want to modify languagetranslationentity. CS, you may add some methods, F5, and the code runs normally.

If one day you need to add a field to the database table corresponding to the languagetranslationentity object, such as modifying the reviseddate, you need to re-open the code generator and generate the code again, the original code is lost. Because our code generator simply writes the file to the disk. If the original file exists, the default method is to overwrite the original file.

There are two solutions to this problem: Improve the code generator or improve the generated file.

Improve the code generator and put the code we wrote into the specified area in the code file,

For example, add some labels to the generated file. If the original file exists during code generation, extract the string values in the tags, that isProgramUser-Defined code added by members, and then insert these values into the newly generated code file, such as the following tag example

// Code_region_start

The custom code logic is put here

// Code_region_end

The second method is to use the partial feature to generate two files and one deleagetranslationentity. CS, another parameter agetranslationentity. logic. CS, the second file, is generated only once, and will not be regenerated later. All the content in the file is stored in our custom logic code.

 

Table Name and attribute name

The table name is language, the generated Object Name Is languageentity, and entity is added after the table name.

If you prefer to write the table name as tbllanguage, you need to remove the TBL prefix when generating the entity as languageentity.

If you write the table name as language and want to generate languageentity, You need to convert it to a lower case. Upper-case letters and lower-case letters.

If the table name is productioncontrol, you can directly add entity to generate productioncontrolentity.

If the table name is deptgroup, the default object generated is deptgroupentity. For readability, you may need to convert dept to department. The generated Object Name Is departmentgroupentity.

If the table name is deptgrp, the generated deptgrpentity becomes increasingly readable.

In this case, converting dept to department and GRP to group is required.

To improve the object name corresponding to the table name of the compound word, you must create an abbreviation dictionary table and a prefix table to replace the object name corresponding to the table name to enhance readability.

There is another question: how can we determine that deptgroup is a two-word, a short name and a word,

Deptgrp is the abbreviation of two words. What if the abbreviation is exactly a correct English word?

There is no correct way to distinguish words. The compromise is that the table name is written as dept_grp, and words in the table name are separated by underscores. When generating the object name, you only need a simple RegEx. split.

The same principle applies to field naming and generated attribute names.

 

Interface and implementation

The table name is language, and the entity languageentity is generated. The object name is also called languageentity. CS.

How do I name the file name if both interfaces and implementations are added? Provides the following solutions for reference:

Solution 1 interface file name ilanguage. CS, implements the file name languageimplementation. CS

Solution 2: The interface file name is ilanguagedal. CS, and the file name is registrant agedal. CS.

Solution 3 interface file name: ilanguageservice. CS

Solution 4 interface file name: ilanguagemanager. CS

It is very easy to change the methods mentioned above to Write template files using codesmith.

 
If you need to support the WCF Technology for the generated interface, you must add the servicecontract feature to the interface when generating the interface, and add the operationcontract feature to the method. Example
 
[Servicecontract]
 
Public interface iwcfexample
 
{
[Operationcontract]
 
Ientity2 getcustomer (string customerid );
 
 
 
[Operationcontract]
 
Ientitycollection2 getcustomers ();
 
}

If the generated interface supports Web Services, add the webmethod label as shown in the following example.

 
[WebService]
 
Public class customerservice: system. Web. Services. WebService
 
{
 
[Webmethod]
 
Public ientity2 getcustomer (string customerid)
 
{
 
..........
}
 
}

Well, these independent parts have been taken into consideration. codesmith is used to prepare various templates and it is easy to generate code.

However, codesmith has a problem: only one piece of code can be generated at a time. If I need to generate interfaces and implementation files for all the tables (or specified tables) in the database northwind, the dirty method can be used to generate files in the codesmith input table name multiple times, and generate code for one table one by one. In addition, interfaces and managers are generated simultaneously, with 10 tables x 2, the workload is 20 times. For 100 tables, the workload is 100*2,200 times.

One function of codesmith is that it can directly call its API to run code generation, pass in the template file and necessary parameters, and call the code to generate code without running the codesmith software itself.

Along with this idea, I made the following utility:

The purpose of this tool is to overcome the limitation that codesmith cannot work in batches at a time. As shown in, open a server and select database. The table name is automatically listed on the left. Double-click the table name or drag the table name to The ListBox in the middle, the table to be generated by code. In the option area on the right, the template lists all the templates under the specified file, that is, the template to be generated by code, the parameter area lists the parameters of the selected template. I use the = sign to fill the value behind it. The following targetfolder is the directory where the generated file is placed.

Click "OK" and click "gbcurr catr datasets Event History modeldrill modelperspective notifications" for these tables.

Generate all templates listed in the template.

The number of tables is 8, and the template data is 7. After you click OK, 7*8 and 56 files are generated, which is highly efficient.

In fact, when I upgraded. Net remoting to WCF, I used this technique to implement all interface files as standard interfaces of WCF.

 

Text to string

When performing reports or database operations, you usually need to write SQL statements in the query analyzer, as shown below:

Select

Itemid, path, name, parentid, type,

Content,

Intermediate, snapshotdataid, linksourceid,

From DBO. Catalog

Then, use the SQL concatenation technology to write the following format:

String SQL = "select Itemid, path, name, parentid, type," +

"Content, intermediate, snapshotdataid, linksourceid," +

"From DBO. catalog ";

This conversion step is error-prone and boring. If a large number of SQL statements need to be converted to C #, the workload will be quite large.

With the code generation technology, let's take a look at this small program.

Copy the SQL statement to the preceding pane, select the code method (C # Or VB) to be generated, and click Generate code,

The following pane shows the code we need.

This tips can save a lot of time to piece together SQL, and what you need to pay for is to write a C # program with less than 10 lines.

It is undeniable that SQL patchwork is quite useful in the preparation of complex reports. complex reports should be counted and computed from a large number of tables, and then a new dataset is generated, transfer to report. It is obviously not a wise decision to use SQL to piece together the data access layer. I thought.

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.