Archive for the ‘Code Generation’ Category


June 9, 2006

Ahhhh – DLinq if you haven't read about this or had a play with it then DO IT NOW! But be careful – you may love it as much as me and just wish it was out there now 🙂 I had a play with it way back at the first cut, liked it (but then kind of got busy on other stuff) –  and then havent got back to it until a couple of weeks ago. Finally after looking at the msi for over a month I decided that it was about time to install that puppy and see where it was at…

If your just starting then make sure you look at the help files – probably the "DLinq Overview for CSharp Developers.doc" in the Docs folder of the install. Theres a few quirks with the install but you'll get the drift and be praying for its final arrival too 🙂 I for one couldn't get the templates in – damn – yep I ran the vbs file to install it but still no luck – the only thing I really achieved was breaking the information tab guy in the ide (see msdn for an article on the reg key to edit on how to fix that) but no matter what they wouldnt go in. I ran the various scripts people had posted as fixes but still no love. Finally it was down to editing the project file 😀

If you edit the project file (in something like notepad) – just change the following line from this: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

to this: <Import Project="$(ProgramFiles)\LINQ Preview\Misc\Linq.targets" />

 (this was with a console app)

Good times! You should be able to compile it but you may not get any ide support. Its not the "best" but it is a beta and you'll at least be able to write your own little apps for a bit of a play :-)… The other thing to check out is the SqlMetal.exe in the Bin folder of the install – this will reverse engineer the class's for you from a database. It seems to work and is the first step to getting some DLinq love! Some inital cool things I noticed was that it uses nullable types and generic collections – and does seem to work (although my database wasnt that complicated). Something I would like added though is when you do a 1-1 table mapping is a constructor on each class that takes in each of the "non-nullable" arguments for that table… this in my opinion is one of the most helpful things out and is something I have had to put into a number of different generators/generator extensions – it would be something pretty cool to get straight out of the generation box 🙂 (maybe I'll write a template and chuck that up… – man – so much to do 🙂 )

Things I picked up on were: attaching/detaching objects is easy, the model to work against is very intuative, it works (always a good one), it supports both lazy and greedy (?terminology) fetches by specifying the mode in the query – pretty cool! – the rest you will see when you play with it 😉

Some things that I'm not in love with are: the ide support (it will come 🙂 ) and var – I know all about var but the thing I hate is that its not a type when you look at it in your code – with your eyes – you have to go fishing. Its the same kind of thing with moving from ArrayList to List of T – you immediatelly look at a typed list and understand it. var to me feels like the ArrayList of C#3.0… just my thought in terms of looking at code – not around compilation etc but just around looking at it…

Anyway – check out DLinq – its awesome! 😀



June 9, 2006

Hmmm – time for a few blog posts I think… well some short ones just on a few random bits and pieces I've played with (in amongst the busy but boring times at work lately 🙂 ). One product I had a bit of a play with about a month ago is Vanatec's OpenAccess. While I don't agree with some of there statements regarding NHibernate I did think it had a few pretty good features (they just need to be finished/extended). If your playing with NHibernate then this maybe worth a look at too just as a comparison.

One thing that is pretty cool is how smoothly it integrates into Visual Studio along with the "in the box" reverse engineering option – no need for bamming out your own generator 😉 – basically from inside Visual Studio you can point it at a database and you get your initial cut of classes to work with… thats pretty cool as you can try something pretty quickly – im not sure how good/robust it is for use in a real world scenario though – Ill leave that up to you 🙂 . Theres also a few other bits like web service support etc (its in there but to me still looks to need some work) which is nice to see but is not really as complete as I would like it to be – no doubt it will continue to be extended 🙂

The model seems pretty easy to work with and it does seem to work but one thing I didnt like was debugging it – sometimes it wasnt that intuative (and there seems to be some j# / java going on under the scenes (its been a while since I fired it up) – but its not that cool to see .java.lang… throwing out errors when your running on .net 🙂 )

 Anyways – not really here nor there on it but it is worth a bit of a play with if your looking around at O/R mappers and have been investigating NHibernate. They have a free trial download so install it, read the help, look at the samples and see what you think 🙂

Castle Project

April 10, 2006

I’ve been meaning to post about this stuff for weeks but I can’t seem to find the time so just go here and check it out for yourself 🙂 It’s definitely worth the effort! In particular I was loving the Active Record pattern/generator they have built to sit over NHibernate. I could’nt get the generator gui to work (seemed to be broken) but I may have just been having “one of those moments” at the time 🙂

It seems to be getting better and better – lots of examples in the solution and it seems to be pretty self explanitory at how the basics work – if you have written an NHibernate solution then there’s no doubt you will love this – if you havent then it’s probibly not a bad idea just to grab this guy straight off the bat 🙂

NHibernate – Generics

March 4, 2006

 Following on from my last post here’s what I found and how I used the package at for generics in NHibernate 1.0.2 and 1-many type relationship mappings:

Your variables need to follow a particular naming notation (otherwise it will complain that it cant find it). This can be overridden but I found it was just easier to make sure that your classes followed the default expected notation which is: underscore followed by a small letter followed by anything else eg: _oTeam, _riders etc would be valid member variables – _OTeam, m_riders etc would be invalid. Pretty simple since thats probibly the notation most people use anyway but it is something to watch out for 🙂 There are two classes you should know about to just get something running with generics at the very minimum. EntityRef of T and EntityList of T.  At one end of the relationship you will have the collection (in the example this is in the Team class and the collection is of type Rider), In the team class we have something like:

private EntityList _riders;

and in the default constructor you would have:

public Team()


_riders = new EntityList(

delegate(Rider o) { o.OTeam = this; },

delegate(Rider o) { o.OTeam = null; });


The first part of the delegate is what happens when a rider is added to the teams riders collection (links it up) while the second part is when the rider is removed from the teams riders collection (breaks the link). So now you dont need to do any special linking code in other methods – you can get rid of those .AddX methods 🙂 (I hadnt thought about using this so I was like – oh yeah – good thinking 🙂 )

You need to use delegates in the default constructor to setup the adding / removing functionality for the collections – once you start using them you get to understand what is going on but at the start you may be like – say what? If you have any other constructors then just use a : this() to make sure the default constructor is called and all these delegates are setup each time 🙂

The attribute markup for the collection would be (note the Access attribute and lack of setter):

[NHMA.Bag(0, Lazy = true,
                     Inverse = true,
                     Cascade = NHMA.CascadeStyle.AllDeleteOrphan,
                     Access = “NHibernate.Generics.GenericAccessor, NHibernate.Generics”,
                     Name = “Riders”)]
[NHMA.Key(1, Column = “TeamID”)]
[NHMA.OneToMany(2, ClassType = typeof(Rider))]
public IList Riders
       get { return _riders; }

In the riders class we need the other part of the link. This is where the EntityRef comes in.

private EntityRef _oTeam;

In the Rider constructor we also need to setup some delegates (note the Add/Remove):

public Rider()

           _oTeam = new EntityRef(
                     delegate(Team o) { o.Riders.Add(this); },
                     delegate(Team o) { o.Riders.Remove(this); }

and then finally the attribute markup for the team would be something like the following (note the .Value, the type  – Team not EntityRef, and the Access attribute):

[NHMA.ManyToOne( Name = “OTeam”,
                Access = “NHibernate.Generics.GenericAccessor, NHibernate.Generics”,
                Column = “TeamID”,
                NotNull = true,
                ClassType = typeof(Team) )]
  public Team OTeam
           get { return _oTeam.Value; }
           set { _oTeam.Value = value; }

So as you can see – a few tricks to get this one working but it does work – just reference the dll and away you go. Should keep fill the gap while the NHibernate 2.0 effort is underway 🙂 The source code and the tests/examples at the svc source are a good place to start to see what is available and how something is done. If you havent got an svc client then a good one can be found here : (thanks JD!)

NHibernate – Generation, Attributes, Nullables, and Generics

March 4, 2006

Over the past few months I’ve been working on a little generator which will generate a thin layer (classes, data access objects, configuration etc) that will sit over NHibernate from a database schema and allow you to work against a particular database. Its pretty simple at the moment – Just point and click go. As I’ve learnt a bit more about it and found out some of the little tricks here and there I’ve gradually extended the template to a point where it is pretty solid (as always theres still some ironing out to do). The things I’ve been integrating into the generation template so far have been:

1) Using classes decorated with attributes (this attribute package is found in the NHibernate Contrib package) rather than the xml mapping files. I decided that the xml mapping files where just something extra to look after in the solution – I thought the less the better. The attributes on the other hand do make these base classes a bit messy but I figured I should never have to look at the generated code – The classes should just be there to use. Although generating the mapping files from the database/inserting attributes like this is really chucking out a whole heap of functionality that NHibernate will allow, I figured I could go back at a later time and extend the generator to provide a richer model. At the moment I just purely want a single class to table mapping and it should just all work.

2) Adding support for Nullable types (I’m using a dll I found called NHibernate.Nullables2.dll – not the one in the NHibernate Contrib package. This can be found here: This is a very cool package as it allows us to use the new .Net 2.0 nullable type notation eg. bool? _flag; in our code/base classes but in the attribute or mapping fies declare it as a special type like: NullableBooleanType 😀 Whats cool here is that from our program side of things these types are transparent while from the NHibernate side of things it understands what we are talking about. And using it is so easy – just reference the dll and change the type in the mapping file :-D. Here’s at example of using NHibernate.Nullables2 with the attribute package:

[NHMA.Property( Name = “RiderNumber”,Column = “RiderNumber”,NotNull = false,TypeType = typeof(NullableInt32Type) )]public int? RiderNumber{get { return _riderNumber; }set { _riderNumber = value; }}

 On the other hand when you use the Nullable package from the contrib you have to use the special types throughout your program code – not very nice 😦

3) Adding in the support for generic collections. This has been what I’ve been working on for the last week or so after work (when I can), and after playing around a bit I have it working – not as nicely as I would have liked but working none the less. The package I used to provide support for generics can be found here: (this is a cool blog too – add it to your feeds if you have one 🙂 ).

The reason putting generics in there was so important for me was that I’m working in .Net 2.0 and then suddenly with NHibernate only under 1.1. I had to go back to using IList – as the developer you have to remember what is in that collection when you come to use it. Generics on the otherhand provide a strongly typed collection IList of T so when you come to use it you know exactly what it is! No casting exceptions for me :-). To get this to work is not the easiest (not hard – but more a case of looking at an example and then just copying it) and then fixing the errors that come up when you run the app up. I’ll give an example of these bits and pieces in another post since this ones getting pretty long …

4) Working on adding enumeration suppport to the generation process – this is working but I’ll keep you posted once I have tested it and used it a couple of times in some app’s I’m working on at the moment 🙂