The object-relational mapper for .NET

T4 hbm2net alpha 2

You can download from here a new version of the hbm2net T4 enabled. This version has a better command line parsing/error handling, and the configuration file to provide ( optionally ) is validate in order to report some meaningful error messages when needed. The utility is still command line, I suggest to use as a pre-build step in Visual Studio  to generate your entities, or other code artifacts you need.

Unzip the files in a folder, and launch hbm2net:


As you can see, the configuration file is now optional. If omitted, the default internal template is used, that will generate the code artifacts for the entities. You can specify an output directory for the files ( if omitted will be the current directory ) and a flag, –ct, to avoid generating files if target files are newer than sources. This is to suppress the reload message box in Visual Studio every time you compile if you forget an entity file opened. Then you can specify hbm files, with wildcards too. So, the simplest command line to generate mappings is:


If you want to use more generation step, you need to edit the configuration file, and inform hbm2net:

hbm2net –config:myconfig.xml *.hbm.xml

the configuration file is in the following format:


The generator nodes can be multiple: each different generator can point to a different template in order to produce different code artifacts.The second parameter, output, is the file name you will generate. This is parsed internally by the T4 engine itself, so you can play your strategy in combining the file name having the clazz object, an instance of ClassMapping representing the current hbm fragment. The presented parameter produces a file called <classname>.generated.cs, and is the default behavior. Please note the template is specified as res://xxxx: this means to lookup the template internally. You can specify a template with a regular path as well, plus, you can use ~/xxx/yyy/mytemplate.tt to specify a path relative to the location where hbm2net is. A copy of hbm2net.tt is included in the zip however, so you can use as a starting point to create your generators.

Some notes about T4.

You will find the entire bible on that argument by starting here or here. I will show a snapshot of the hbm2net.tt of the current version:


So, as you can see, very similar to the old asp stuff, not so different from NVelocity. The basic difference from other such engine is that we have reachable from inside the template any .NET type we can need. Furthermore, we can organize our generation with helpers by using the <#+ #> tags. Whit these tags we create a method inside the transformation class usable by the template itself. As an example, the <#=GetCommentForClass(clazz,1).TrimEnd()#> line call an helper function. In this case the body of this function is contained in the res://NHibernate.Tool.hbm2net.templates.common.tt file, but can be inlined with the template itself. Currently an object of type ClassMapping ( an internal hbm2net object used to represent a mapping fragment ) is passed to the template, as the field clazz. Sorry for the strange naming convention, I left something from the  old original version, this maybe will change in future drops.

Posted Sat, 12 December 2009 12:12:00 PM by felicepollano
Filed under: hbm2net T4

comments powered by Disqus
© NHibernate Community 2016