Geeks With Blogs

News Google

Nick Harrison Blog<Nick>.Next()

I recently say a couple of articles on CodeProject that showcased wrappers to simplify using the CodeDom

I am in favor of anything that encourages use of CodeDom and tries to flatten the learning curve for using the CodeDom.

Unfortunately, most examples of using the CodeDom seem a little contrived.    This makes using the CodeDom seem like over kill.

Consider some of the following applications:

  • Using the CodeDom to pro grammatically generate proxy methods for binding parameters to stored procedures
  • Using the CodeDom to generate web services for all of the methods in a specified class
  • Using the CodeDom to generate code to exercise class libraries by calling every method in the class.

Some advantages of such an approach includes:

  • Rich object model makes it easy to structure code generation code.   
  • As best practices evolve, generated code is easier to maintain.   Instead of having to change every web service when a new WSE is available, you only have to modify the generator and then regenerate all of your code.   This is much better than having to rewrite every web service.
  • Code generated through CodeDom can be rendered as any language that has a CodeProvider.    This helps ensure that you will continue to get retrains on your investment in building a generator.
  • As new versions of the framework are released,  Microsoft will update the implementations of the CodeDom objects to properly support the new version.   Code generators that you write will continue to work with new versions of the framework.

There are a couple of things to also keep in mind with maintaining generated code.   Never modify generated code.    You want to protect your ability to regenerate as needed.   Partial classes in version 2.0 of the framework make this easy.    Make any customizations in a separate file and then you can easily regenerate the original file without loosing your changes.  

In any version of the framework, you can also use inheritance to protect the generation layer.    Generate you code and make any code changes in a class derived from the generated class.   Not being able to regenerate generated code makes people scared of using code generators.     Good object oriented design solves this problem.     Code generation does not necessarily lead to bad object oriented design.   

Many people frown on the use of code generators because they often produce “ugly“ code, lead to bad object oriented design, or it is unmaintainable.    I believe that using the CodeDom avoids these pitfalls and effective use requires good object oriented design rather than precluding it.

Is anyone else using the CodeDom?  

Posted on Sunday, November 20, 2005 8:02 PM Code Generation | Back to top

Comments on this post: Why use the CodeDom

# re: Why use the CodeDom
Requesting Gravatar...
following points force to use of Codedom

1)Good for doing repetative task like data
access logic.

2) its a Model driven architecture.
3) force to desclipine and standard.
Left by Dinesh Tawar on Feb 10, 2006 2:56 AM

# re: Why use the CodeDom
Requesting Gravatar...
I believe the main reason to use the CodeDom is to create class modules based on your meda data in the database. I created this code generator once that read my tables in an access database and created a class module with 8 methods to interface my DB layer (ADO). All I had to do was tie the Gui to my class modules that were generated and write a little custom code and I had a 45 screen application in less then 11 months.

I'm thinking of doing the same thing with the CodeDOM and other databases. I might even create a code generator that gens my gui. (both winforms and webforms).

Thanks Nick for getting me started on CodeGen again... :)

Left by JerryM on Apr 21, 2006 6:40 AM

# re: Why use the CodeDom
Requesting Gravatar...
Oh, ya...

The code was generated in less then 60 seconds for 60 plus tables and created an average of 100 private variables and properties for each class. each class represents on table in the database.

Left by JerryM on Apr 21, 2006 6:43 AM

# re: Why use the CodeDom
Requesting Gravatar...
maybe you can consider using CodeDOM for making compilers
i discovered codeDOM when reading an article about writing a compiler in .Net
if you understand french, check out this link
Left by koufdell on Dec 27, 2008 8:23 AM

comments powered by Disqus

Copyright © Nick Harrison | Powered by: