Free Programming E-Books
Free download ebooks on computer and programming | |||
Free Ebook "Code Generation in Microsoft .NET" Sample Chapter
Code Generation in Microsoft .NET
Download chapter
Free download Chapter 1: Introducing Code Generation Code generation has the potential to revolutionize application development. Rather than handcrafting each piece of code, developers are increasingly turning to code generation based on templates and applications of business logic to automatically perform a variety of tasks. This book teaches the technical details of code generation in .NET through a coherent series of steps that will help you to incorporate code generation into your own development efforts. Veteran author Kathleen Dollard teaches code generation as a scripted repeatable process using templates you control, so you're not tied to a particular framework or style. Because you can regenerate code at any time, you can incorporate changes, including database changes, throughout the life of your application. The templates are flexible and designed to work smoothly with the handcrafted code you'll use to customize your application. The underlying fundamentals are explained along with three specific techniques: outputting code to a stream, using the Code DOM, and using XSLT-based code generation. In addition to the text, the tools in the book (downloadable in both VB .NET and C#) include a mechanism to extract information from SQL Server; a tool for editing and running code-generation scripts; a simple, flexible ORM tool that relates your database structure to your runtime class model; and a set of templates you can use as the starting point for your adventures in code generation. Generating repetitive sections of code frees you to focus on the features that make your application unique. Code generation will turbo-charge your development cycles by offering speed, reusability, agility, and consistency. Go forth and generate! Introducing Code Generation
CODE GENERATION IS CODE THAT WRITES CODE. By letting you automate the creation of a large portion of your application, code generation offers a radical shift in development. In addition to the obvious benefit of turbo-charging your development, code generation lets you maintain consistent code quality and allows your code to evolve quickly in response to metadata changes (including database changes). Code generation lets you extend reuse to include code with similar patterns, as well as code with identical code segments. The result is increased speed, reusability, agility, and consistency in your application development. In the end, code generation can be as simple as supplying an Extensible Markup Language (XML) file with a few pieces of information and clicking a button, and-voilą!-the repetitive portions of your application (stored procedures, middle tier, and user interface) are complete and ready for you to extend with your own customization. The purpose of this book is to teach you the process that lies behind that click. It'll seem complex at times, but don't lose sight that you rarely deal with the complexity of the underlying tools and only occasionally deal with the complexity of the templates themselves. Code generation is about building these once and reusing them many, many times-within and between applications. NOTE Both code generation and object-oriented inheritance allow you to reuse common functionality. They differ in that inheritance only allows you to reuse specific code segments that you can then override or modify in the derived class. Code generation allows you to reuse patterns in your code as well as code segments. Code generation and object inheritance are very effective when used together. Generating code isn't new. What's new is that it actually works in the real world. In the past, I haven't been excited about using code generation supplied with frameworks and templates because they couldn't provide the control and flexibility I demanded. I felt like I was in a whitewater rapid without a paddle, rushing along quickly but quite uncertain which boulder I might hit. I even know one shop that abandoned its multiyear commitment to stored procedures, replacing them with dynamic Structured Query Language (SQL) to accommodate the limitations of a purchased framework. Sure, we could have done our own code generation three or even ten years ago, but code generation used to be hard work, and we didn't have enough collective knowledge to recognize patterns easily. A number of recent developments, including XML support, inheritance features in .NET, XSL Transformations (XSLT) processing, and wide-scale pattern recognition, make the time ripe for code generation to become part of your development process. I'll show you how to incorporate your own code generation, meaning you stay in control of the architecture you're implementing. I'll provide tools that support whatever techniques and patterns you use, and I'll provide samples you can use to generate applications as soon as you understand the underlying techniques. I'll share those techniques, along with guidelines for making code generation a valuable part of your development strategies. Those guidelines will include steps and principles that keep your code generation effective and high quality. You can use several mechanisms for code generation. In this chapter, I introduce the three most important approaches for application code generation in .NET, along with the benefits and drawbacks of each. This lets you compare them and decide which fits you best. Chapter 3 has further details of using each mechanism. Continuing to walk through all three would be too cumbersome, and I want to focus on the underlying process common to them all. So, in later parts of the book, I'll focus on one mechanism. In addition to showing the mechanisms, this chapter presents the five steps you'll use to make code generation a coherent process. Chapters 2-5 cover these steps in more detail. But, before I go any further in describing the five steps to code generation, I want you to see what code generation looks like. Generating a "Hello World" application in each of the three mechanisms illustrates code generation in its barest form. | |||