CodeGen

We'll just code-gen our way out of here

First, we have the implementation of the SendPropertyChanged method, can you spot the bug? protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } Then, you have: public string CustomerID { get { return this._CustomerID; } set { if ((this._CustomerID != value)) { this.OnCustomerIDChanging(value); this.SendPropertyChanging(); this._CustomerID = value; this.SendPropertyChanged("CustomerID"); this.OnCustomerIDChanged(); } } } And this: public Customer Customer { get { return this._Customer.Entity; } set { Customer previousValue = this._Customer.Entity; if (((previousValue != value) || (this._Customer.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._Customer.Entity = null; previousValue.Orders.Remove(this); } this._Customer.Entity = value; if ((value != null)) { value.Orders.Add(this); this._CustomerID = value.CustomerID; } else { this._CustomerID = default(string); } this.SendPropertyChanged("Customer"); } } } Both properties show exactly why I despise code generation as an architectural approach. Instead of taking the appropriate route, and actually solving the problem at hand, they just threw code at it until it looked like it worked.

posted @ Sunday, December 16, 2007 10:54 AM | Feedback (31)

RunSharp: Reflection Emit for Mere Mortals

I have been dealing with IL generation using Reflection Emit for about two years. I believe that I have enough experience with it to get a feeling for how it goes, and it usually goes slowly and painfully. There are wrappers around it (Castle Dynamic Proxy has a AST for this), but they are fairly specialized, and anyone somehow I always find myself needing to fix the code that build the AST, mostly because I have users that throws weird curve balls at Rhino Mocks. Nevertheless, IL generation is a powerful technique, just very cumbersome to deal with. RunSharp is a...

posted @ Saturday, November 03, 2007 5:50 AM | Feedback (4)

Dynamic Methods

I don't hear it talked about, but the CLR has a very efficient way to generate code at runtime. Probably this is because this code generation stuff is something that is accessible through IL generation only, and that is not for the faint of heart. Nevertheless, there are some very useful uses for this. NHibernate is utilizing this approach to avoid the costs of reflection, for instance. Let us take a look about a simple scenario, we want to translate any delegate type with two parameters to a call to an instance method on our class: public class Program { private static...

posted @ Monday, October 29, 2007 10:07 PM | Feedback (12)

Working with ANTLR: HQL Grammar

I am currently trying to build an ANTLR grammar for HQL. There is already an existing one for Hibernate 3, but that one is based on ANLTR 2.x and supports quite a bit more than NHibernate does at the moment (DML statements, for instance). After several failed attempts to port the grammar to ANTLR 3 and generate C# code out of it, I gave up and started building my own. I have read the ANTLR book not that long ago, so I ought to have a known what was in store for me. I didn't. I found out that this...

posted @ Saturday, August 11, 2007 6:54 AM | Feedback (2)

Observations on Learning

Here is an observation on learning. When I was at high school, I was thought Pascal, and I couldn't for the life of me understand dynamic memory allocation. I had little problem with everything else, but dynamic memory allocation (better known as pointers) was a mystery wrapped in an enigma stashed inside a headache. About a year later, I was learning C++, and was one of the first in the class that grasped pointers and their usages. I remember trying to explain ***pppHead (sparse matrix) to another student, and he drew blank after the first level of indirection. I don't think that the...

posted @ Saturday, June 30, 2007 11:11 PM | Feedback (5)

What kind of persistence would Ayende write without OR/M?

Rodrigo thinks that I am ignoring the obvious when I think about persistence only in terms of RDBMS. He is probably right. I want to point out that the last time I wanted to have a non-DB persistence, I used C# as the file format. That was fairly successful, although I will probably won't do it again.

posted @ Wednesday, June 06, 2007 9:56 PM | Feedback (1)

NHibernate Query Generator - More Collections Magic

I wouldn't have expected it to be this hard*, but it is alive! Here is the query: 1: User one = User.FindOne( 2: ...

posted @ Tuesday, June 05, 2007 11:53 PM | Feedback (10)

On the advantages of one off tools

I don't like general purpose code generation, but I am fan of special purpose ones. The main reason that I don't like the general purpose ones is in my experience they have invariably fell into the "Generate code that I don't like" or "Requires too much futzing to get to work the way I want it." Part of it is related to the fact that I consider generated code as a maintainability concern as well, and I am very nervous about trying to do that. Nevertheless, I do like code generation, I tend to write simple code-generators for a lot...

posted @ Tuesday, June 05, 2007 1:54 AM | Feedback (1)

Dynamic Language Runtime on top of the CLR: This Is BIG

Check this out. Even though that Jim says that it is on the IronPython site, I can't find it, but I am still loving it. Making the CLR more friendly to dynamic languages is a Good Thing in general, but the thing that excites me is the possiblity that it can be leveraged from my code as well. I paid my IL taxes, and while Linq expressions are nice, I would like to get solid support for runtime code generation without having to do it in the assembly level.

posted @ Monday, April 30, 2007 11:33 PM | Feedback (1)

Quick & Dirty CodeGen

I needed to get some code that would map an XML file to a database table. Not being particularily fond of doing it by hand, I whipped out this statement: select  '       if node.SelectSingleNode("' + column_name + '/text()") is not null: ...

posted @ Sunday, April 15, 2007 9:11 AM | Feedback (3)

NHibernate code generation

Bobby Diaz has sent me the results of running the NHibernate Plugin for Visual Studio 2005 over the Northwind database, and I must say that they are good. Not perfect, but good. Another template that was recommended to me by Steve was MyGeneration "NHibernate lujan99 - .08" template, which I need to check.

posted @ Tuesday, March 20, 2007 9:58 AM | Feedback (4)

More on Emitting Multi Dimentional Arrays

Here is a small code sample that demonstrate the problem. This doesn't work, but changing the Run method parameter to anything but a multi dimentional array does work. public interface WithMatrix { ...

posted @ Saturday, February 17, 2007 7:06 PM | Feedback (3)

Emitting Multi Dimentional Arrays

Given the following type defination: public virtual void Do2(int[,] args)  This is what the C# compiler will output: .method public hidebysig newslot virtual instance void Do2(int32[0...,0...] args) cil managed Notice the 0...,0... ? This means that this is a two dimentional array whose indexes starts with 0. The problem is that there is simply no way that I could find to generate an overload to this method from Reflection.Emit. Even when I pass it the correct Type (hard coded in C# to use int[,], for instnace), it still generates the following: .method...

posted @ Saturday, February 17, 2007 12:44 AM | Feedback (6)

Did you know? SharpDevelop in under LGPL!

And appernatly has been this way for quite a while. I was still under the impression that it was under GPL, which meant that I couldn't make use of its components in my application, but it being LGPL, it is making stuff much easier. For those of you whom the last sentence means nothing, here is a Great OSS Licensing Debate in a two sentences: GPL is viral, LGPL is not. GPL means no traditional commercial use, LGPL allows it*. SharpDevelop was under GPL for its 1.0 version, and I continued to think...

posted @ Friday, February 02, 2007 11:41 AM | Feedback (1)