In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to quickly develop a code generator in asp.net mvc4. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
I. Preface
As a programmer for so many years, I have been thinking about how to improve our coding efficiency. I have some experience on how to improve our coding efficiency.
1. Clear project structure and focus on where the code is written
2. The code to achieve the same function is small and easy to understand.
3. Repetitive or regular code should be generated automatically
Here I will discuss the problem of code generation.
Second, about the code generator
When I first graduated, I was also very superstitious about code generators. I like to find some code generators and related source code on the Internet. I like to discuss with netizens which generator works well, but in fact, I seldom use these things to develop projects for a simple reason:
1. The generated code is not the code we want.
2, the generated code and then modified, in fact, not as fast as my ctrl+c and ctrl+v.
3, the generation is basically entity classes and sql splicing code, now directly use linq or some easy-to-use orm is more convenient, who also uses SQLHelper plus sql splicing?
4. No generator can generate UI layer code and front-end interactive js code very well, even if it can generate a simple page.
So, I advise you not to blindly believe in code generators. It does improve our efficiency, but it's not just for you to find a generator online. Code generator it is only a template engine, the most important thing is not the code generator itself, but a kind of function or a kind of page code specification, to refine their own code, to extract a general template.
For example, our common query page, input page, etc., we only need to refine a standard query page code, including foreground html, foreground js, background controller, background data service. Then write it as a template, and then use the template engine to generate the code we need.
The code generator itself is a template engine, so I think the code generator is not the software that can generate three-tier architecture code spread on the Internet, but Microsoft's razor engine, which is very simple and easy to understand, and friends who have worked on asp.net mvc projects should also be very familiar with it. Personally, I think this is an engine for code generation.
III. Page template
We still want to develop quickly. For example, after I have selected some settings, I can directly generate the code I want, including html and js, which can be copied into the project and run directly. After running, I will see the page I want, with basic functions. Of course, the rapid development I'm talking about here is based on my template for refining the functionality of the page. In fact, I refined three templates:
1. Query the page
This template can solve most of the query report business functions.
2. Edit the page
This editing template can solve almost all entry functions, because it includes master table and multiple slave tables (1 N or 1:1), and can be saved in the same transaction at one time. And define a lot of events before and after triggering to write some business processing, and to achieve differential updates.
3, query the editing page, you can query or edit directly in grid, this page is used to do some simple documents or some basic data pages.
IV. Principle of code generation
Make the above page code into razor template, razor template + setting options = = razor engine = > page code
There are actually several ways to use the razor engine:
1. Directly utilize the view output function of mvc. The following is the key code.
Var stringWriter = new StringWriter (); var viewContext = new ViewContext (controllerContext, view, viewData, TempData, stringWriter); view.Render (viewContext, stringWriter); var result = stringWriter.ToString ()
The advantage of this method is that it does not need to introduce a third-party class library, but directly calls the Render method of mvc view to generate, and it is very efficient, but the disadvantage is that the construction and acquisition of controllerContext and view objects is very complex. This method is suitable for neat code farmers. I belong to this kind.
2. Using the output of the third-party class library RazorEngine, the following is the key code
Var template = "Hello @ Model.Name! Welcome to Razor!"; var viewData = new {Name = "World"}); var result = Razor.Parse (template, viewData)
The code is clean and clear, except that the RazorEngine class library is introduced, and it is not as efficient as the former.
5. Code generation user interface
We have the template ready and the engine ready, so we also need a data input viewData, and the purpose of our user interface is to better define the viewData.
In this user interface, we still need to separate the definitions of the three pages:
1. Query page generation
The first step is to select the code category search (query page), select the database, select the business master table, and then check the field to achieve the setting of the query condition department, and achieve the drag-and-drop sorting function. You can compare it with the query template.
The second step is to select the columns to be displayed in grid, set properties, format, etc.
The third step is to set some global settings, mainly based on these parameters to determine the namespace and generate file names and other information.
Click the generate button, press the settings to generate the code, and then pop up the folder after generation, which has generated three layers of MVC code respectively.
Mms_receive.cs
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.