Elementary Introduction to code generation

Source: Internet
Author: User
Tags html page interface window xmlns domain tomcat visual studio

What is code generation?

Code generation, that is, Generation, presumably we are not unfamiliar. Many of the code frameworks are generated automatically when we use development tools such as Visual Studio. The initial impression is that code generation can reduce duplication of effort. However, if you look more deeply, you will see that not only is the code framework automatically generated, even the compiler can be considered a code generator-from high-level to machine language (the process is so natural that we don't think it is generated by code). So, there are different levels of code generation, the lowest level is the IDE's function framework, followed by MFC, such as the Engineering Wizard, and then the more advanced point should be IDL, such as interface definition language, and then to Microsoft proposed DSL (Domain specific Language).

Why should there be code generation?

The level of code generation that reflects the level of abstraction. Because we have a very abstract domain model, but there is a long process between the code that can be executed. Without code generation, we need to fill this gap manually. In order to improve the efficiency and speed of our development, we must be able to quickly and effectively transform the domain model into execution code, and code generation came into being.

Code generation can have a number of levels, because an abstraction layer may not be in place at the bottom of a sudden, so need to transition to the middle layer, at such a level, code generation is in a layer.

Recently thought for a long time JSP and XAML to hand-tested a hand, the concept of code generation and in-depth understanding of a lot.

Code generation for JSP

1. There is a simple HTML page hello.htm, which reads as follows:

Hello world!

This is nothing strange, with IE browsing on a "Hello world!" of words.

2. What if I change the name of Hello.htm to hello.jsp?

The answer is, use IE to browse the time, or out "Hello world!" The text, no change!

3. But looking at the problem can not see the surface ah, we note that Tomcat work of the N-level subdirectory with Hello_jsp.java and hello_jsp.class two files. Open a look:

...
Public final class Hello_jsp extends Org.apache.jasper.runtime.HttpJspBase
Implements Org.apache.jasper.runtime.JspSourceDependent {
...
Out.write ("Hello world!");
...

Now JSP has been compiled, the servlet way to run! After each modification of the. jsp file, Tomcat regenerates the. java file and compiles it into. Class and then runs. The next time you visit, the JSP is not changed and runs directly. Class. This improves the speed of the operation and is no longer interpreted as executing! The server-side script is also characterized by simple writing and fast running!

What if you embed a Java snippet in a. jsp file? That's easy, just insert the code snippet at a location after Out.write ()! The original code snippets are running in this code environment! You finally understand what the object and method can be used!

Previously seen ASP.net code, and JSP is similar to the principle of code generation.

Code generation for XAML

Microsoft's WinFX Avalon, now called Windows Presentation Foundation, uses XAML to describe the interface layout, and uses code-behide technology to separate pages and code. And so on, XAML can be used not only to describe the interface layout, but also to define the framework of the program.

A simple window description (mypage.xml)

<window xmlns= "http://schemas.microsoft.com/winfx/avalon/2005"
      xmlns:x= " http://schemas.microsoft.com/winfx/xaml/2005
      x:class= "Myspace.mypage"
      text= "MyPage"
/>

Compiled code generation (Mypage.g.cs)
namespace MySpace {
Public partial class MyPage:System.Windows.Window, ... {
...

because you have the code behide technology, you can write it in another file, unlike the JSP that mixes with HTML (JSP can be separated by Jsp:usebean, for sure).

Code behide codes (Mypage.xml.cs)
namespace MySpace {
Public partial class MyPage
...

You see, the same class is defined in two files! In the past, this could be a problem! But now that Microsoft has modified the C # Language specification for this, the concept of partial class has been introduced, allowing a class to be defined in two different files! As the

just said, XAML is not just a description of the interface layout, it can also describe the application. For example,

defines an application (Myapp.xml)

<navigationapplication xmlns= "http://schemas.microsoft.com/winfx/avalon/2005"
                        xmlns:x= "http://schemas.microsoft.com/winfx/xaml/2005"
                        x:class= "Myspace.myapp"
                        startupuri= "A.xaml"
                startingup= "Appstartingup"
</navigationapplication>

Generated code (Myapp.g.cs)

Namespace MySpace {
Public partial class MyApp:System.Windows.Navigation.NavigationApplication {
...

Code Behide (Myapp.xml.cs)
Namespace myspace{
public partial class MYAPP
...

You can see that XAML is not just an interface description language, but a new program model, the way to write software declaratively, this is the meaning of abstraction!



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.