Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Often, when writing your application, you will come across large areas of boilerplate code in which the same code pattern is repeated over and over. Whenever you find yourself repeating the same mindless task, you should consider automating the process. The good news is other people have considered this problem before you and have written code generation tools. Code generation is where software-- rather than a human--is used to write program components automatically. A number of commercial and free code generators are available in the market. Visual Studio uses generators in many of the UI designers.
They have the drag-and-drop designers generate the XML or code. Another popular use of code generators is in object relational mapping tools, or ORM. ORM tools take the tables and other constructs inside a database and turn them into language objects. There is an active community of third- party vendors making code generation tools. Here is a list of some of the code generators on the market. The most common pattern I see is for code generators is the following: You create a template file, which contains your portions of your code.
The template file also contains tokens. You take your template file and you pass the template file to a generator. Optionally you pass other external parameters to the generator--say a database connection string. Here is an example, on the bottom of this slide, of a token from CodeSmith. This has an angle bracket, a percent, a word called current time, and then the closed percent and close angle bracket. This will tell the code generator to calculate the current time and place it in your code template at this point.
When you run the generator, you'll pass in parameters. Some of the things you might pass in are connection strings to open the database, evaluate database schemas, find files in your hard drive, or read information from your UI tools. Then the code generator will replace the tokens with the runtime information. Visual Studio has a code generator, known as T4. I would like to show you how that works, and it's the topic of the next movie in this chapter.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97348 Viewers
61 Video lessons · 84593 Viewers
71 Video lessons · 68801 Viewers
56 Video lessons · 101276 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.