Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

A Look at CodeDOM

Before we get to know what actually "CodeDOM" is, lets dissect this word and understand what DOM (Document Object Model) is. DOM (a relatively old concept) is a specification provided by the World Wide Web Consortium (W3C). As of MSDN, this is what DOM is: "The W3C DOM is a platform and language-neutral interface that permits script to access and update the content, structure, and style of a document. The W3C DOM includes a model for how a standard set of objects representing HTML and Extensible Markup Language (XML) documents are combined, and an interface for accessing and manipulating them."

The important parts of this definition are "language-nuetral" and the representation of HTML/XML as a set of objects. The definition of CodeDOM is just as similar. It is just the programmatic representation of code in a language-nuetral manner. The essence of CodeDOM is to represent the source code as an object graph and use that as the basis of generating code in the language of your choice. With the help some services provided by your language compiler, you can even go to the extent of generating assemblies from this representation. Therefore the notion of dynamic code generation makes CodeDOM a very powerful concept. There are numerous areas where CodeDOM has been effectively applied :

1. First and foremost, the code-behind concept provided by ASP.NET is nothing but CodeDOM magic. At runtime, ASP.NET would essentially generate new types whose definitions are provided in the .aspx and .aspx.cs files. An instance of this generated type would be used to handle the final rendering of that page. One thing to remember here is that, to use a language in ASP.NET development, the language should (imperatively) implement a CodeDOMProvider.

2. Somewhat similar in concept to ASP.NET code-behind is the concept of compiling code represented (maybe partially) as XAML in LongHorn, the next generation Windows OS. I stumbled across this good post at longhorn blogs explaining this in detail -

3. Tools like wsdl.exe and xsd.exe use CodeDOM to generate source code from XML representations.

4. A lot of code generator tools in the market today would use CodeDOM to generate application source code from sources like Database schema , UML diagrams, XML documents and so on. So, CodeDOM can be a handsome option in your application, if you need to generate code at design time or at runtime, given disparate sources like XML or even free language text. The best part - you need not even know a language you generate code in.

I did a bit of googling myself and these are some of the good write-ups on CodeDOM:

1. A Definition of the CodeDom Abstract Language 

2. Using the CodeDOM

3. Bring the Power of Templates to Your .NET Applications with the CodeDOM Namespace

4. Microsoft .NET CodeDom Technology - Part 1

Posted: May 22 2004, 03:16 PM by Manoj G | with no comments
Filed under:
Leave a Comment

(required) 

(required) 

(optional)

(required)