6

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?

devXen
  • 2,812
  • 3
  • 29
  • 43

3 Answers3

4

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.

Tomáš Fejfar
  • 10,505
  • 8
  • 48
  • 77
JoshBerke
  • 62,464
  • 23
  • 120
  • 162
1

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

Blake Niemyjski
  • 3,192
  • 3
  • 23
  • 40
  • While I agree that MyGeneration hasn't been updated in ages, in the purpose of full disclosure it's worth noting that you're an active developer on CodeSmith. – furtive Mar 08 '16 at 16:46
  • Yes, this is true. I've updated most of my posts to indicate that. – Blake Niemyjski Mar 09 '16 at 22:35
1

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.

MrTelly
  • 14,219
  • 1
  • 44
  • 77
  • last project I was on that used code gen'ed turned off the code gene halfway through when we couldn't keep updating the generator fast enough.... – JoshBerke Mar 25 '09 at 01:40
  • In your opinion, besides being free, is MyGeneration a better code-gen? – devXen Mar 25 '09 at 01:47
  • Don't have enough experience with CodeSmith or others, I find it quirky but good. It's now open source, and the templates are written in C# - all those are pluses. – MrTelly Mar 25 '09 at 02:19
  • They both generate code, but CodeSmith is easier to learn and cleaner, but MyGeneration gets the job done just as well, just takes a few to learn the interface and multiple windows. – rball Apr 30 '09 at 22:45