Why should I use a code generator for a database program?

Source: Internet
Author: User

The previous articles mentioned tools to improve work efficiency, which are more or less helpful for our daily development. Some netizens are interested in code generators, and some tools may change the idea and mode of developing database software. Here I also come to ridicule the code generator.

Code Generator Growth Process
1. Each class must be written one by one, which is very laborious. In the daily development of databases, most of the daily work is to add, query, modify, and delete data tables, and most of the Code is similar.
2. Write more data into classes. I will pay attention to the names of each class. For example, if the table name is pubs, my object class name is pubsentity, the data entity class name is pubsdal, and the interface layer class name is pubsservice. In this way, a unified mode is followed, development and maintenance are also easy to understand. The naming of methods should also be uniform. For example, adding a piece of data cannot be called "add" or "insert". This is not harmonious.
Standardize and unify as much as possible.
Three types of design and method design after a period of training, gradually standardized. They also need to be layered into different namespaces and assemblies. For example, the data entity at the Entity layer is called yihu. IPR. businessEntity, the data access layer is called yihu. IPR. dal, the interface layer is called yihu. IPR. service, the interface layer is placed in yihu. IPR. in the Web namespace,
Yihu is the company name and IPR is the system module name.
Then place these classes in different assemblyinfo. CS, all projects use the same assemblyinfo. CS, consistent assembly version, culture, signature file (Sn. key.
4. As a result, all details are standardized. However, the problem arises again. You need to press the keyboard manually. When the system has a lot of entity layer and data access code, the system often gets wrong and misses characters. So let's get a code generator and generate code directly according to the standardized structure, which is not prone to errors. Even if it is wrong, it is also a code generator bug and must be correctly debugged.
5. When the code generator is ready, there will be more days to get lazy. You can do less than two hours of work every day. When others are still typing on the keyboard, you can easily open your code generator, create a database connection, and then click the execute button, all the code is displayed in front of you. After a few months of repetition, I feel that the program is not very technical. As a result, I began to study the architecture, how to integrate the tested architectures into your development, and turn program development into a kind of smart physical labor. Finally, I learned how to study the code architecture of dumwash, petshop, and dinnernow. But the problem is that I still need to write code from the beginning, so that I have to repeat it one by one and return to the primitive society. So I began to study how to combine the latest research architecture with the code generator. After a period of research, I found that to change the code generator, I should extract some code and put it into a public framework. In order to make the new research architecture run, I had to do so.
6. After a period of time, the code generator can finally be combined with various architectures, so project templates are generated one by one. Different project templates represent different architecture solutions. I also made templates to facilitate the development of new projects. Such days can last for a long time.
7. There are new discoveries, So you constantly improve your code generator. When WCF is required, [datacontract] is automatically added to each object class, and [servicecontract] is added to the interface layer class. Some people like to piece together SQL statements, while others like to use parameters, therefore, the code generator is provided with two modes of code generation at the same time. Some projects are written in C #, but maintenance personnel only use VB, so codedom technology is used to upgrade the code generator, it can generate both VB and C # code at the same time. Sometimes it is difficult to open the Code Generator separately, and it is very slow, and the interface is ugly, so we study Visual Studio SDK, integrate the Code Generator directly into Visual Studio, so that you can open a tool (Visual Studio) to complete all the work. Sometimes you find that the generated code cannot be changed, it is difficult to identify the code, so we add the partical tag to the generated code, put it in a folder, and put the code written by ourselves into another file, because CLR is JIT in the unit of method, the code generated by the code generator that is no longer used can be ignored or To remove it when appropriate, you only need to use a key (right-click the method name and check the reference. If this method has been used, the window at the bottom will call the called place. If it has not been called, you can safely remove it. This refactoring function is much more powerful than the search function)
As a result, work becomes very interesting and cannot be separated from code generators.

Composition of code generators
1. SQL statement generation. Automatically generate the crud script code for SQL statements. You also need to generate stored procedures if you prefer stored procedures.
Class 2 generators. Generate entity classes, Data Handler classes, interface classes, and interface binding code against database tables.
To support different types of databases, You need to generate different types of database types. For example, for SQL Server, the parameter is sqlparamater, and for Access series databases, the parameter is oledbparameter. The solution of my code generator is to generate unified code. Since the code I generated is based on enterprise libiary, the Enterprise Library itself supports access to multiple types of databases, the code I generate naturally supports multiple databases. However, one SQL Server is enough, and I doubt the reality of supporting multiple databases.
After purchasing the SQL server license, I think it is very unlikely that your boss will agree to submit oracle. As small as many employees complain that the company has not yet issued a year-end award, in fact, more companies do not have a year-end award.
3. generate a basic interface control. For example, if the table name is "employee" and there is a field name, a table is generated and the ID is "employee", and a text box txtname is generated, the data entity and the code bound to the interface are also generated at the same time to ensure productivity. You need to replace what is needed in the interface with dropdownlist or datetimepicker. Most of the interface code can still be used directly.
As a result, code generators become complex and professional.

Objection
I have read some comments about the code generator, which makes sense.
1) It makes sense to generate code according to the template. Otherwise, it is a string generator, so codesmith is moved out for discussion.
2) Some say that the code generator generates a lot of non-quality spam code. What is the purpose. Code Generation is only a string generator. The output code is of poor quality and inflexible.
3) if the boss knows this stuff, it will shorten the project's deadline, and it will be better if it doesn't exist.
Therefore, the code generation technology is not popular. Although Visual Studio is also a good code generator, it is not recognized.

Data Access Mode
In this book, the author mentioned three data access modes.
Transaction Script: we often use it. This mode is used in my code generator. It is simple, but lacks flexibility. To put it bluntly, it lacks object-oriented design and cannot withstand the test of changes, it's just a pile of systems.

Orm: Object Entity Framework. With this layer, we can completely forget the existence of the database. The ORM will automatically generate the native database SQL script and automatically process the relationship before the object.
For example, nhibrnate and nbear have been involved in the previous two frameworks. They have actually used some small projects and made related gadgets to assist in development. Currently, tools supporting nhib.pdf can be directly integrated into Visual Studio, and corresponding code generation tools are also popular in various forums. The only problem is that if this project is not self-led, you do not have the right to choose nhibrnate. In addition, this framework will keep you away from ADO. net, you can't see the friendly sqlconnection there, instead there are a lot of sessions, sessionfactory, and the learning cost suddenly increases; in a team, some will nhibrnate, some will not, is it unfair to some employees to give them time to study? In an ERP project that I have experienced, when the project is in the middle of the year, people are greatly improved, for nearly half of developers who change the call (or take the lead), they will not start with nhibrnate. You have no solution at all. The deadline of the Project is urgent, so they will not start with nhibrnate and use ADO directly. net, using Application Block.
However, if you do. NET development, you may not even find a job in ADO. net. Learning knowledge in too many fields, such as nhibrnate, is risky, difficult to learn, and risky to use. You are not sure which day you will have the right to use it, even if you get started, you should be familiar with nhibrnate's details.

Domain mode: I do not have a deep understanding of this mode. You probably need to design a software in the pure OOP mode, instead of targeting the database from the very beginning, starting from data as scripts.

As a result, the code generator has been evolved and improved. Although some of the improvements are like the "Wuyun transformation law" in the Qing Dynasty, they have a set of theoretical knowledge, but they are not easy to do. They integrate various ideas into daily development work and tools, it is a slow and long evolution process.
I have also studied these three modes before I came into contact with code generators. At least I have studied these three modes and it is not too bad to make things.
There are also many code generators that are popular in major forums, and I have also used them. From the perspective of improving work efficiency, they are all very good.

In this article, I did not dare to show a picture. I understand the feelings of developers opposed to code generators.
Every tool is generated to improve our work efficiency. If a tool or method cannot improve your work efficiency, you should not hesitate to remove it. After several years of database software and one year of reports, I have poor development capabilities, and the code generator I am tossing is also very bad. I often die, there are some features that I want to add to the code generator without having time to do it, and I am not afraid to do it.
With the help of code generation, I can spend more time on the details of the system, try to make a professional system, the interface should be professional, the error prompt should be professional, the details should listen to the user's ideas, until it replaces the sacred excel in mind.
Because of code generation, my system design capabilities have not grown. There are fixed templates, fixed code production methods, and various aspects that may be considered (paging, main interface, background, basic framework, general user permission roles) have been taken into consideration, so it takes more time to analyze and understand customer needs and communicate with the customer. Take the initiative to take care of the project before, after, and after, and perhaps this is a signal: turning around

The program is almost thirty years old. I am not confused, and the technology is not very bad. I have accumulated more or less various technical solutions;
I have experienced some projects, large and small, and sometimes I really doubt myself. Is my character suitable for project? How many programmers are lucky to be transferred to management? Not many.

If you have accumulated habits, you may have your own code generator for a long time and can customize and modify it. If not, it will not be too late from now on.
Take the initiative to think about how to improve work efficiency every three months or six months. After a few years, your work efficiency will multiply.
I will give you a direction where you can work hard. Maybe I am not strong enough to mislead you;
To believe it or not, it's up to you.

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.