Saturday, December 15, 2007

ORM Hunting (in the .Net world)

I have been hunting around for some code generation tools to play with. I"m really looking for something very simple like the Ruby on Rails generator. I've come to the conclusion that no one want anything easy in the .Net world.

I've also been looking at some DALs, ORMs, etc. Really, most of the DALs and ORMs offered in the mainstream are just WAY too much. I really have to think that if you need some of the more advanced features of these systems you may want to relook your design. Heck you may want to read up on doing "The Simplest Thing that Could Possibly Work."

Anyway, I use Castle's great framework called ActiveRecord. It's so simple that you really don't need code generation. As long as you keep things simple when you begin using it, you can slowly move towards the advanced stuff (if you really need it). So what was I REALLY hunting for. I'm really looking for a database schema upgrade (or migration) that's as simple as activeRecord.

ActiveRecord has a nifty CreateSchema method. This method scans all the ActiveRecord classes in your model and creates a database schema for persisting your objects. Of course since ActiveRecord is also an ORM, it provides you nifty methods for instantiating and persisting objects (to the newly created schema). The real sticking point is this....CreateSchema deletes the database everytime! THis means all of your data is lost!

For a while we've used a very kewl in-house migration library. Whenever you modify or extend your database schema you just add another migration. It versions each migration and the database, allowing for a very painless upgrade everytime the software is run. Before our migrations tool, we kept large repositories of bloated DDL/SQL scripts...yuck.

Now, back to my hunt. It turns out that I was really looking for a tool that would look at my model and generate (or upgrade) the database schema based on the model. ActiveRecord already does the what I need is an upgrade option.

I've heard that Hibernate has such a far nHibernate (ActiveRecord wraps nHibernate) does not have this support. Maybe someday. Maybe I'll write something. What boggles me are the number of generators that focus on generating the model based on an existing database schema. Now, I do understand that many databases with lots of info are out in the field...and generating a model based on that schema might make sense. The ones that seem silly to me are the ones that generate a new set of model classes everytime the schema changes...instamagically!

It's a challenging thing to create these sorts of frameworks. I guess. I'm not sure I'm comfortable with a framework that generates and regenerates the model in such a willy-nilly way. Wouldn't this be a refactoring nightmare? Sure, you don't want to constantly refactor your database schema either. Other applications may depend on that schema. But, by generating the schema from the model your schema is less likely to change often or drastically.

Here are some of the ORMs and DALs I found:
  • Castle ActiveRecord (.Net)
    Castle's port of the ActiveRecord pattern wraps the nHibernate library. ActiveRecord makes it very simple to create a model and persist it to a database. You don't need any special tools. You create a simple XML configuration (connection strings etc.). To make things really simple your classes inherit from an ActiveRecordBase. This base class provide all the methods you need for creating updating and searching for business objects.
  • Hibernate (Java)
    I haven't used hibernate yet.
  • nHibernate (.Net)
    I've used nHibernate briefly. Unlike ActiveRecord you have to create complex XML mappings between your model and the database. I'd recommend going with ActiveRecord.
  • ActiveRecord (Ruby)
  • doodads (.Net)
Kroon had a nice article on several of these. Also check the Server Side's article on ORMs. It has a pretty complete list.


Glyn Mooney said...

I'm with you on that one! Why can you get amazingly simple ORMs in Python (with Django), Ruby and PHP but ASP.NET can only offer clunky, complex and messy alternatives that barely resemble an ORM at all.

salma said...

نقل عفش

نقل عفش من الرياض الى مصر شركة شحن من الرياض الى مصرمن الباب للباب
شركة تركيب مكيفات سبليت بالرياض افضل شركة تركيب مكيفات بالرياض