Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,026 | Comments: 44,842

filter by tags archive

Code Generation and the Open Close Principal

time to read 1 min | 83 words

Vladan Strigo made a really inspiring comment about code generation in the NH Users list.

I didn't want to use codesmith for that because it would de-OCP-fy me in my future efforts :)

That makes a lot of sense, and manages to touch on what bothers me the most with code gen as an architectural approach. It gives up a very important concept, and that affect a lot of the stuff that is dependant on that.


Mark Lindell

I agree that Passive code gen can be abused.

I particularly don't like passive code gen other than a template starting point to save you some typing. Come on, snippets are passive code gen and no one would deny the benefit of those, right?

On the other hand, I do believe that active codegen can be used appropriately to solve the right problems. Infrastructure that are driven by metadata can benefit from active code generation. (Proxy generation?)

After all, isn't Reflection.Emit a form of active codegen?

I understand your dislike of code gen as an architectural solution when good design would solve the same problem without violating OCP. (yeah you know me -- sorry music joke)

Alon Fliess

So JIT is a violation of OCP, it does code generation from IL to assembler. Also any runtime implementation of proxy, interception layer, data accessors, etc. are bad.

I don't think that code generation has anything to do with OCP, its the design of your system that has. Code Generation may be based on the same good OO patterns. I have implemented a system were all my code generators are discovered in runtime. They registered themselves to a pub/sub mechanisms that calls them whenever they need to emit their code. I can remove or add a generator without touching any other generator...

Any generator can derived from existing one and change the code it emits.

This is an OO & OCP based design...


Ayende Rahien

You missed this part: "code gen as an architectural approach"

I wasn't talking about emitting IL to do something at runtime. I was talking about code gen as an architectural approach, that is, we will define the application using (DB, XML, pretty pictures) and generate the code from there

Lars Wilhelmsen

Not to be annoying or anything - but isn't there a slight difference between "Principal" & "Principle"?


Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Technical observations from my wife (3):
    13 Nov 2015 - Production issues
  2. Production postmortem (13):
    13 Nov 2015 - The case of the “it is slow on that machine (only)”
  3. Speaking (5):
    09 Nov 2015 - Community talk in Kiev, Ukraine–What does it take to be a good developer
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats