Title: Did not end without tail - Project development notes: How many code can be generated!?
Keywords: distributed development C # project division Delphi with C # mixed development code generation tool database scanning
On December 2: Start writing code generation tools, it is a little bit of Wanxu, I don't know why:
The code tool includes code generating tools and two types of tools for code synchronization tools. Of course, you can write the generation and synchronization of the code in the same project. If there is a sufficient time to consider, then our code tools should be included in one project. But in fact, due to the relationship between time and experience, I didn't do the two stuff in one project, but made several different code generation tools and code synchronization tools. For the convenience of explanation, the following discussion is just for the code generation tool. (The code synchronization is relatively small at the beginning, so there are some small problems, this will be described below.)
One thing to emphasize is that there are a lot of ideas to be in the project development process, or they have been thought out after the development is completed, so there are many ideas, but there is no specific implementation J.
Before you begin, you must introduce a book, title: "NET Enterprise App Senior Programming" (Tsinghua University Press), this book tells a simple example of a code generation tool. This simple example core of Dongdong includes two pieces: a piece of scan for a database, takes the structure of the database into a tree in the program; the other is the generation and synchronization of the code. However, this book's code generation and synchronization is the Code DOM that is included using the vs.net itself. So finally generated code is only available in the environment of VS.NET (only C # or VB.NET code files). The requirements of this project are to generate a lot of types of files using the code generation tool, so we just refer to the idea of this book. If there are friends who have completed the use of VS.NET development solutions, it is a complete reference. book.
Code Generation Tools This stuff is very broad in our project. Take a few examples.
1. The corresponding database table and the generation of the data code of the stored procedure;
2. Corresponding to the database table and the stored procedure call from WebService to DataAccess all files;
3. Corresponding to generating server cache file code for all database tables, as well as code in the cache mechanism;
4. Automatically generate the ClientDataSet code that the client Delphi can be used through the server-side C # generated by the server-side C # (manufactured by Delphi's cattle in the project group);
5. Scan the database generated the base stored procedure for generating the database table;
6. ................
Since we made the code generation tool itself is some of the more zero-scattered gadgets, we have not summarized them, huh, huh, every time I want to sum up, I always find an excuse for yourself: I can't generate the code. The tool is more complicated than the program, what is the code to generate a tool? So those classes written above are still separate zero fragile tools. It's a bit of a bit of feelings.
The following is mainly from the brief description of the code generation tool, some of the three aspects of the development process of the code generation tool, as well as some changes in the development process of the code generation tool.
Code generation tool
So, I will tell the principle of the write code generation tool I understand from the code input, generated, and output (if these can be called the principle).
l input
How to find the input is the core of the code generation tool. I think this part is very realistic that the developer's imagination. (A little self-selling.) I think that if you can complete your needs with direct change of code generation tools, then other developers can don't have J. Can you? Of course, it is of course not. But we can find out how many ways, multiple types of inputs, so that our code generation tool generates a less close to the final implementation program. Due to a lot of our level, then there is a problem with how each layer of code is organized. I don't know what kind of rules defined in organizational organizations in the framework of the hierarchy. I have a simple definition of three types of code organization; the DO layer can organize code according to the structure corresponding to the database table and the database store process; in the BusinessRules layer, can be organized as an object in business design; and Facade, The WebService and the UI are usually to organize according to the user-oriented module. For the above three code organization, you can generate at least the foundation file, as well as the organization of the file. However, the input method of various situations is different, and the following will be described below.
N facing the user's interface
The code organization of the interface is to organize the specific situation of interaction design. For example, the system can be organized by the interface code file in accordance with the head office module, branch module, and the like. In this case, if you want to generate a code frame for the user interface, in addition to the first interface of each interface, it is necessary to have a description of the code organization form for the user interface. We couldn't use an Excel file to describe this organizational form, and then read the content through VBA, generate code, but later due to time relationship, there is no truth: (this principle should be a code with ROSE The principle is basically the same.
Correspondingly, the code of the UI and the Web Service layer in this system is generated.
N-object-oriented business
Object-oriented business tissue forms don't say more. It is a very thick particle size in our system design, so we are only divided into some big objects, such as institutions, goods, and documents. There is no specific subdivision. There is also a code to organize our code in this classification. Due to the relationship of time, this part we have no temporary
Correspondingly, it is the code of the FACADE layer and the business rules layer in this system.
N-facing database
It is really a little embarrassing here, and it is basically not practiced. Some part I just wrote some classes that read some data from the Excel file, these are not much. Maybe it will be implemented in the middle of the project, let the subsequent project notes record it.
The code generation of database association is that we have completed, and I do the tool is based on the table structure of the database-based colleagues that has generated the COMMON DATA and DataAccess.base (the two-layer code is dragged by dragging). The way is generated. At the beginning of the generation, it is an organization with the table in the database. The relationship between the simple code table, the table and the operation class, for the main sub-table, is placed in one operation class for processing I generate the corresponding DataAccess.Template with the DataAccess layer code based on his code and the structure of the code. The code class in the DataAccess.Template generated is a subclass of the corresponding parent class inherited in DataAccess.base. The main role is some fixed ways to generate different operations (SELECTALL, INSERT, UPDATA, DELETE method, document forms of the simple table, etc.), and generate the DataAccess.Template layer of inheritance DataAccess.Template All code.
During the generation, the demand scans the database to get the corresponding Key Column, the names of each column, and the SQL system view can be used to obtain the structure of the database. For example, information_schema.tables, information_schema.columns, etc., you can refer to the book mentioned above. In fact, before doing the above process, I have not realized the organization's form of code can be three orientations such as users, object-oriented, relational databases. I am just the simplest thinking as long as it provides the database table and the stored procedure, you can do all the support required to write all interface code (if you don't know the previous development process). Project notes). This part is actually in the development of the encoding process. According to the plan for the development process, I hope to let the developers use the generated code to complete the connection between the interface layer and the database in the second step of the encoded process. Want to implement support for interface programming by the generated code. So I correspond to the code that is not just a DO layer, and the code of the Business rules, the FACADE layer, and the web service layer is organized by the relationship database.
Correspondingly, the code of the Data Access layer and the Data Access. Template layer in this system.
The three categories said above, I want to mainly describe, the beginning of the development process, the part of the project has been designed or implemented in the project, however, corresponds to the encoding process. After the user-oriented interface interactive design is completed, the client's encoding process will be organized; There is still a lot of benefits to developers to understand the project when the project is in the beginning of the project.
Also, I also want to discuss with friends. I don't know how my friends organize the project to manage the project.!
l generation method
The method of generating the code We mainly have three types:
n character write method
This approach is how I use the code generated by the code generated tool. It can be said to be a relatively stupid method. This method is to write a large number of fixed characters in the file as a string in the program. The inputs mentioned above are sandwiched in the middle to form a personalized code. For example, the following code is a code for generating a DataAccess.Template file:
String sztemp = string.empty;
SzTemp = "Using System; / N / N";
SzTemp = "Namespace XXX.Develop.frameworkbuilder.projectbuilder / n";
SzTemp = "{/ N";
SzTemp = "public class" sztablename "template / n"
// The generated DataAccess.Template class is unified with the name of Commondata as the end of the Template.
..................
It can be seen that if you generate code in this way, the most likely problem is that if your generation rules have change, then you have to re-modify your code generating tool source program. This is very trouble ...
n character instead
This is my current method. This method is to generate a template file relative to the input is a rule. After the template file is generated, the code generation tool will use the input to replace the special symbol block in the template during the execution process. The following is the same as an example:
The first is to generate a code template file:
Using system;
Namespace xxx.develop.frameworkbuilder.projectbuilder
{
Public class // ?? Commondata ?? // template {..................
Then take the class name, the class name, replace the special symbol block // ?? COMMONDATA ?? //, which can also generate the same code as the character write method.
You can also use this way to modify project files in VS.NET. For example, the following is a code in the template in StaticData.csproj corresponding to the StaticData layer:
Relpath = "// ?? CommonData ?? // staticdata.cs" Subtype = "code" BuildAction = "compile" /> // ?? -> Incrude> Files> .................. (Note: "Where" "is a special symbol of the beginning of the loop," "is the end of the end of the loop generation," " is the end special symbol that you want to generate in the loop. The code in the middle In this case, you can update the project file at the same time when you regenerate the code (but you may be able to pay attention to Check problems when you use SourceSafe.) N Code Dom Code DOM is the code generation tool provided in VS.NET. This code is used in the book recommended. Code DOM has a good support for C # and VB.NET code generation, but it seems that you can use Code DOM to modify the project file, and do not support the generation of Delphi. So this project does not use this way. l output The output should be a person who writes the code can "utilize" code. "Using" means that it should be possible to perform different levels of jumping development on the basis of generated code, and can be modified to the generated code. For the output I want to say, if you start to make a code generation tool, it is best to make a synchronization of the code with the code of the code, maintenance, and maintenance of the code. (This project does not actually do this, so the maintenance of the generated code is taken.) Some feelings When making a code generation tool, I have always had some ideas, although this idea is still very immature, but I still want to talk to my friends first. Also, several companies I have stayed have such problems, that is, how to share the experience of developers, the project's experience. I remember that I mentioned in the previous project notes, how to leave the experience of the project? How to make the experience of the project become the property of the company, not personal property? For this issue, my previous ideas are very simple, that is, project documentation, strengthen the management of project documentation. However, there will be a problem directly, even if I complete a project left 100,000 project documents, when I come, who has time to carefully experience your 10,000 articles? I also talk about the preservation of project experience with a friend who was previously a state-owned enterprise. He told me that from his experience, the method of preservation and transmission of project experience in state-owned enterprises is "with apprentices." The teacher pays through the apprentice to pass the experienced generation one generation. However, in today's development of new technologies, the maintenance of project experience is only made by this way, it has not been able to keep up with the era. My thoughts are that this form is actually available in the experience of saving projects. The above has been written, and our engineering generated code is nothing to do with specific development languages. And one but define those special symbols used to replace, and what happens to be able to see your input. A rich man to write a template file, this template file, or this template project (combination of template files) will be extended as experience. In the new project, you can generate all support files for the previous project template files, developers to focus more in the business. If this method can be, then there will be no such auxiliary process to modify the development process, and the improvement of all the personnel in the project team will be implemented? (This is just an idea, I hope to discuss with friends) Reconstruction of development process The benefits of using the code generation tool I want to say here, I don't have much to say, nothing to improve the development efficiency. What I want to emphasize here is that after we use the code generation tool (actually my most of the situation or with the database developer). Our normative nature, naming specifies have improved a new level. Previously your file name, database table, and stored procedure names can be freely named under rule licenses, but this time file name, and where the database established by the database will be directly generated by the code generation tool. The code is related. That is, it is actually further strengthened the control of the database, coding rules (if you can't generate a code, the buddies responsible for the database must have written a lot of procedures.) For example: Since we put in a common table in a commondata, we define the name "main table name Detail" that must be clear, this, can generate the code less to determine some conditions less . There is also a strengthening of this rule. The three parts of the code generation tool will be written here, if there is time, I will use the specific process of writing a code generation tool with the database contact section. (Ha, colleagues responsible for the database in the project also began to make a small build tool to generate a relatively unified stored procedure code, plus a record, delete record, and segmentation data, huh, huh, the colleagues in the project I feel really happy.) (There are some problems and advantages of we use layered development methods in these days in the project. However, there is still no summary. Time, write it again.)