Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NHibernate with Codesmith to generate ORM

Just started learning NHiberate. It seems rather tedious to write proper hbm.xml config files for each database table.

The question is can NHibernate read into tables and automatically retrieve relationships and needed config file as well as database mapper classes for you? It seems that the programmer still has to do a lot of ground work initially. Then I came across Codesmith which got templates for NHiberate? Does it fill the gap?

like image 973
devXen Avatar asked Mar 25 '09 01:03

devXen


3 Answers

You can use code templates, but this still creates a pain IMHO. The best way is to use Fluent nHibernate. They have a new auto-mapper feature, that uses configuration by convention. No mapping files, it just makes assumptions that you use similar names and can define rules of how to convert an entity name to a table name.

like image 107
JoshBerke Avatar answered Oct 23 '22 04:10

JoshBerke


Code generation can fill that gap really well, I started using CodeSmith, but then they made that a pay item so I moved to MyGeneration. My approach is to attack the DB first, code gen the Mapping files, and gen classes if they don't already exist. That gets something going really quickly, great for a prototype and creating test data.

The mapping files, classes and DB are often then changed by hand. In some instances I've tried getting the code generation to work as part of CI - but it's proven hard to do that, unless you're disciplined about where you part you're hand crafted code changes. Partial classes were a help, but still a prob to get the whole team on the same page, no-one likes to have their code overwritten by a tool, even if you can then rollback and merge.

like image 26
MrTelly Avatar answered Oct 23 '22 04:10

MrTelly


I work for CodeSmith... Please keep in mind that MyGeneration hasn't been updated in forever which means it lacks support too. CodeSmith fully supports its products and templates. This means any template bugs will also be fixed and verified by Codesmith.

Thanks

-Blake Niemyjski

like image 40
Blake Niemyjski Avatar answered Oct 23 '22 03:10

Blake Niemyjski