R – Choosing Database and ORM for a .NET project

databaselinqnetnhibernateorm

I'm working on a .NET application using Silverlight on the client side. Now I've come to the point where I want to throw out my static dummy data on the server side and add a database instead.

For the database I'd love to use one of them ORM's where I can simply tag my model classes and the database tables are built for me. I did some tests with Groovy and Grails earlier, and thought GORM did a smooth job. What's the best way to set up a database in .Net?

The first thing that strikes me is to use nHibernate. I don't really know anything about nHibernate, but I've heard numerous people mention it with enthusiasm. But then I see that ADO .Net also is an ORM, which is built into the framework.. Does nHibernate outbeat ADO? And what's the deal with LINQ? I see that's listed as an ORM too, but I though LINQ was more for the query part? Can I "define" the database through LINQ?

Any comments and recommendations are welcome. I'd also love to hear if you have opinions on what database to use. I assume MS SQL Server is the easiest choice?

Best Answer

NHibernate and Silverlight:

One of the NHibernate contributors, Ayende Rahien, recently posted a blog post about NHibernate and Silverlight:

I got a few questions about NHibernate and Silverlight. That is actually a very easy thing to answer.

Don’t even try. They don’t get along. In fact, they aren’t even going to get along.

Silverlight doesn’t have System.Data.IDbConnection, and you can safely assume that that it somewhat important to NHibernate.

So, running NHibernate inside a Silverlight application, presumably in order to access a local database is out. But I don’t think that this is what most people actually had in mind when they ask about NHibernate and Silverlight. They want to know about NHibernate on the server and Silverlight on the client.

And that is easy enough to answer as well, it is going to work just like any client / server system. All the same rules apply.

So NHibernate should work as long as you don't plan to use it directly from the Silverlight client.

NHibernate:

NHibernate is a great ORM but it has a quite steep learning curve, so you should be prepared to invest some time into learning the framework if you choose NHibernate. If you make that investment you will be rewarded by the flexibility and power that NHibernate provides.

Castle ActiveRecord:

Castle ActiveRecord is a framework that is build on top of NHibernate, and hence is quite similar to NHibernate. It reduces the learning curve a bit, since it adds some additional abstractions. As the name implies it is build for use with the ActiveRecord pattern, and includes a base class that gives you quite a lot of functionality if you don't mind using their ActiveRecord base class.

LINQ to SQL:

LINQ to SQL and the ADO.NET Entity Framework are two ORM:s that are included in the .NET Framework. LINQ to SQL is a smaller and simpler framework than Entity Framework, but it has some nice features, and is quite easy to get started with.

Entity Framework:

Entity Framework is quite easy to get started with as well, but it has a few quite big problems in the current version, since it is still in version 1. However, the next version of Entity Framework will improve and fix many of the current shortcomings.

LINQ and Schema Generation for the Frameworks:

All of these frameworks have support for using LINQ as the querying language. LINQ to SQL and NHibernate can generate a schema for you based on your domain classes and mapping. Entity Framework can not generate a schema in v1, but v2 will add that functionality.