Ease Development With Code Generation

Developers often avoid code generation because they think their apps are too complicated. But executed properly, code generation can help you implement sophisticated architectures quickly.

p>Code generation helps you implement sophisticated architectures quickly, and it allows your applications to weather the storms of change. Your code becomes agile during development and easier to maintain. Code generation doesn't work alone—it leverages existing skills and partners with other techniques such as integrated testing. That's the good news. The bad news is that few people use it effectively.

Effective code generation requires control over your templates. If you can't change your templates, you're speeding toward a brick wall. You're responsible for the code; you need responsibility for the templates.

Application code generation consists of a series of tasks, generally involving metadata extraction from a database or object model, Object-Relational Mapping, generation of five kinds of stored procedures, generation of six or more kinds of business objects, and ancillary tasks. You need to perform these tasks consistently, so orchestrate them with a script engine or harness. You and later programmers can perform these tasks in exactly the same way with a single click.

Code generation relies on metadata describing your application. Creating metadata as an XML file lets you do complex metadata extraction and morphing processes up front, which simplifies your templates. Extracting from your database directly is a bad idea because you need control over how your business objects reflect later database changes. A simple Object-Relational Mapping layer provides abstraction, which is especially valuable for legacy data with bizarre naming or when business objects and data lack symmetry. If you're considering UML for your metadata source, remember that you need valid metadata for the life of your application. Do you really think you'll be using the same brand of UML in three to five years?

Some developers avoid code generation because they have lots of custom code. The key isn't avoiding custom code, but knowing precisely where to put it. Isolating handcrafted custom code from the plumbing goop is code generation's single biggest benefit. Code generation splits your application into technology-driven templates, business-defining metadata, and custom business algorithms. These three elements evolve on different timescales; treating them independently lets you weather massive technology changes or use templates to reuse the same technology to solve totally different problems.

You must claim a new view of objects in order to isolate these three elements. You already have two views of objects: the flattened hierarchy seen by the code using your object, and the UML model that describes the business. You don't need to mimic either of these views, although you must implement both effectively. It's your choice to program your class to make coding itself easier. You can split each class into two files: a generated base class with predictable stuff and a derived handcrafted class for custom code. This isolates the custom code you need to focus on. The generated base class contains plumbing, such as properties built from application-specific metadata.

You can't split your classes using "partial classes." Partial classes are great for tightly coupled cases such as WinForms, but application code generation requires more flexibility than partial classes offer. The dual class model uses OOP—refined over 30 years to have just the right levels of control and flexibility.

Handcrafted code requires protection. Generating over a manually edited file hurts the project more than the individual who ignores the warning text. Avoid this problem and add a new dimension to code generation with file hashing. If the file changes, the hash changes. You can categorize generation as "always" or "until edited." Always tells code generation to raise an error if the file's been edited. The guilty wear a flowered beanie or whatever you do for a broken build. Until Edited says you anticipate editing. It's a great approach with stored procedures where you don't have OOP to handle customizations.

Code generation is unlikely to take away your job. Software development is perceived as mission-critical and driven by need, but in reality, it's driven by available funding. If there's funding, there's work. The key to your long-term success is providing outstanding value. Creating applications with attention to code-generation details offers outstanding value through speed, reusability, agility, and consistency.

About the Author

Kathleen is a consultant, author, trainer and speaker. She’s been a Microsoft MVP for 10 years and is an active member of the INETA Speaker’s Bureau where she receives high marks for her talks. She wrote "Code Generation in Microsoft .NET" (Apress) and often speaks at industry conferences and local user groups around the U.S. Kathleen is the founder and principal of GenDotNet and continues to research code generation and metadata as well as leveraging new technologies springing forth in .NET 3.5. Her passion is helping programmers be smarter in how they develop and consume the range of new technologies, but at the end of the day, she’s a coder writing applications just like you. Reach her at kathleen@mvps.org.

comments powered by Disqus

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.