Extracting Metadata
(Page 1 of 22 )
This chapter looks at the second step of code generation, extracting metadata, how to extract it from a variety of sources, especially focusing on SQL Server metadata extraction. Also introduces the intimate merge of metadata that's used to incorporate additional manual metadata information. (
Code Generation in Microsoft .NET, by Kathleen Dollard, 2004, Apress, ISBN: 1590591372.)
Extracting Metadata Principle #2: Collect metadata as a separate, distinct step that can independently evolve.
Extracting metadata means reaching into available sources, dragging out the metadata, and stacking it up neatly so other processes can exploit it. Exploiting the metadata you’ve stacked up will be the next big jump in programming. It’ll look like code generation, but that’s just one way to exploit metadata. Metadata is the key that unlocks the door to faster application development.
It’s not easy to define metadata concisely. Metadata is an Extensible Markup Language (XML) description of your application in terms of databases and/or assemblies, tables and/or objects, columns and/or properties, and so on. The bulk of metadata describes details of the database your application uses, but it can also include details such as user interface information. For instance, if you’re building a bookstore application and your database has a table containing all the books you sell, with columns for their prices and availability, a simplified version of your metadata might look like this:
<dbs:DataStructures xmlns:dbs="http://kadgen/DatabaseStructure">
<dbs:DataStructure Name="pubs">
<dbs:Tables>
<dbs:Table Name="Titles">
<dbs:TableColumns>
<dbs:TableColumn Name="title_id” AllowNulls="False" Type="int" />
<dbs:TableColumn Name="title" AllowNulls="False" Type="int" />
<dbs:TableColumn Name="price AllowNulls="False" Type="int" />
<dbs:TableColumn Name="available" AllowNulls="False" Type="int" />
</dbs:TableColumn>
</dbs:Table>
</dbs:DataStructure>
Metadata includes everything you use as input for your templates. Chapter 1 included templates you could apply to any data table by supplying the appropriate XML metadata input file. This chapter is about acquiring XML metadata—automatically extracting it where possible. The easiest format for metadata is XML. This XML file defines the application you’re going to build in a way that simplifies code generation. It defines the structure of your application, including its objects, properties, and methods.
NOTE: Metadata won’t define all of your classes because your application includes utility and handcrafted classes that you won’t generate. Furthermore, it won’t define all of your methods because you’ll define additional housekeeping methods and properties within your templates. |
Metadata fits into your code generation because it’s part of this magic incantation:
< metadata > + < template mechanism > = < generated code >
Metadata comes from one or more sources, often one such as a database that maps directly to your requirements. Metadata extraction takes you from some source to metadata usable for code generation. This process reaches into the source and creates an XML document containing the supplied metadata. Figure 2-1 illustrates the role of this extraction in code generation. You may use more than one metadata source in creating your application. Once you’ve got your metadata, it serves as the input for the templates that generate source code for your application. Throughout this chapter, you’ll see mechanics of metadata extraction. Chapter 6 merges this extracted metadata with an Object Relational Mapping (ORM) view of your database. Chapter 3 shows how to exploit metadata to generate code for your application.
NOTE: You may have expected that the hard part of code generation would be creating the templates that produce the code. Actually, the hardest part is figuring out your architecture, followed by collecting the metadata. By the time you know what you’re going to build and have the metadata ready, the process of building templates is straightforward. Therefore, this chapter covers the toughest stuff in the book. So be patient if this chapter becomes difficult. |
You can handle metadata in a couple of ways. You can just grab metadata whenever you need it, or you can make metadata extraction a distinct process. Grabbing metadata whenever you need it means simultaneously extracting metadata and outputting code, which makes things more difficult than they need to be. Combining these processes makes it difficult to debug and hard to reuse your extraction mechanism. You lose the metadata/template independence that’s a key benefit of code generation. It’s important to make metadata extraction a distinct step you run and debug it on its own. In other words, instead of going directly to the database when you need to know the columns in a table, retrieve it from an already-created XML file.
Although your database is probably your core metadata source, you also have access to a variety of other metadata sources—anything that has information that could contribute to your application. The problem of extraction is rarely finding the metadata sources; it’s getting the information stacked up neatly ready for your templates. To do that, you have to understand how the metadata source exposes information and what format is easy for code templates to consume. Much of this chapter talks about how selected sources expose data. I’ll focus on XML Schema Definitions (XSDs) and the SQL-92 standard as primary metadata sources. Understanding these sources will help you work with them and clarify how you retrieve metadata from any source. Extracting data from an XSD illustrates how to extract data from any XML source using XML Transformations (XSLT). Extracting metadata from a SQL-92 database illustrates how to use .NET code to extract metadata from a source with a programmatic interface.
This is from Code Generation in Microsoft .NET, by Kathleen Dollard (Apress, ISBN 1590591372). Check it out at your favorite bookstore today. Buy this book now. |
Next: Introducing the Process >>
More Database Articles
More By Apress Publishing