views:

901

answers:

9

I am a little ashamed to say that I have never used an ORM; as you may recall most of my career experience is hacking around with Classic ASP and the little .NET I do tends to be maintenance only.

For my own career as well as in preparation for a new project at work (done in .NET finally!) I'm looking at adding an ORM to my skillset - being the masochist I am I'm also going to look at TDD while I'm at it for this project.

Anyways, I am using .NET 3.5 for the project (or plan to, anyways) so I am trying to decide what ORM I want to choose; like I said I am not really familiar with them but I know generally what they are supposed to do and how they do it. My choices are basically down to NHibernate or LINQ/Entity Framework. I haven't used any, but I've seen some demos of LINQ2SQL and of NHibernate.

NHibernate seems like it's more robust overall (Linq2Sql isn't even a "true" ORM) as well as heavily used in the ALT.NET community, which I want to explore more. However, EF is Microsoft's offering and, as such, will be heavily used by the majority of development teams out there who just use whatever Microsoft comes out with and don't look at any alternatives.

I've heard both good and bad things about EF, but I want to pick something that will give me a good foundation as well as look good to other employers if/when I find a better job in a company with a real development team.

EDIT: I should add that, if it matters any in picking an ORM, the application will have to deal with an existing database.

EDIT (02/15/2009 6:48 PM): The only database it will use is SQL Server 2005 Standard, running on Windows Server 2003. Given that this is for a project for work, my goal is to pick something that won't take me a very long time to get up to speed with (as I'm going to need to be productive and "Learning a new tool" won't look good to managemetn) but something that will be better than rolling my own DAL. At the time I haven't decided if the application will be a smart client or an ASP.NET application, but one part of it is more than likely going to be done with ASP.NET MVC.

+11  A: 

I would recommend nhibernate - since it is open source and has a very active community help is easy to get. A great starting point is The NHibernate FAQ, it ranges from post for beginners to very advanced concepts. Hit the ground running by reading and following Prepare your system for NHibernate and Your first NHibernate based application.

All employers that I worked for used NHibernate for ORM in .NET and Hibernate for ORM in Java - so you should benefit greatly from learning NHibernate.

Tobias Hertkorn
+1  A: 

Since you are using .NET 3.5, I recommend LINQ.
LINQ reuires only that IEnumerable and IQueryable interfaces are implemented.
That means it is not limited to relational data.
Alse see the article ORM in .NET 3.5

Kb
+2  A: 

Many discussions on this topic on Stack Overflow:

What object mapper solution would you recommend for .net?

What is the best data access paradigm for scalability?

Persistence framework?

Both EF and Nhibernate are considered useful for large projects. Some of the main concerns expressed about EF are lack of lazy loading/persistence ignorance. Nhibernate has a steeper learning curve and requires more manual configuration.

Turnkey
+1  A: 

For the free and open source route, NHibernate is pretty hard to beat. It's a mature ORM with a very active community and plenty of support. There is an initial learning curve there with NHibernate, however, I think it's well worth the effort you might invest in it.

If you want to stay within Micrsoft's .NET world, LINQ is a great technology to learn, and not just LINQ2SQL (which as you say, isn't a full-blown ORM anyway) as LINQ can be applied to objects, XML and basically anything that's IEnumerable/IQueryable.

For the future, I'd recommend Entity Framework (again, within the Microsoft world). There has been a few scary sounding announcements from the LINQ2SQL team within Microsoft which seem to suggest that all future development effort will be put into Entity Framework rather than LINQ2SQL. There is a certain amount of overlap of those technologies anyway. Certainly, I don't believe that LINQ (speaking generally) is going away any time soon, but I'd be careful of investing for the long term in LINQ2SQL. Entity Framework is still relatively new, and under active development, so expect to see EF mature in the coming months/years.

So, right at this moment, LINQ2SQL is great and you can get up and running with it quite quickly without necessarily having to dive into the inner workings of it (i.e. expression trees etc.) but for the future, I'd recommend Entity Framework/NHibernate as I believe these two ORM's will be the ones in majority use over the next few years by most .NET dev teams.

Of course, that said, there are a myriad of different ORM solutions out there, and I doubt any one of them will be a one-size-fits-all solution, so you really have to examine each of them and see which is most suitable for the specific project that you're working on.

CraigTP
+3  A: 

Why limit yourself to just one while learning? Why not dabble with all of them and see which ones feel best to you? Then you can begin a deep dive in those technologies and get more out of them.

Personally, I would start out with LINQ to SQL because it's so drop-dead easy. Play around with it just to get used to the idea of how an ORM can be used. You'll be surprised at how simple it is and how easy it is to get started, and then you'll probably come across many of the pain points which lead some people on to Entity Framework.

Once you use Entity Framework a little bit, you'll either be satisfied with it or you'll wonder what the big deal is with nHibernate and some of the others mentioned in related questions.

It's at that point, in my opinion, that you'll be most primed to be successful with something like nHibernate, which may be a tough to learn as a first foray into ORM-land.

Either way, I don't prescribe to the "which one should I try" school of thought. Try them all, I say.

Eric King
+2  A: 

I would say go with NHibernate. It is widely used, has a number of tutorials and learning resources and an active contrib group. It is similar to Hibernate which means any skills you learn on the .NET NHibernate are portable to Java. Win-win-win-etc. :)

Jeffrey Cameron
+4  A: 

SubSonic from Rob Connery is a "Swiss Army knife" ORM based on ActiveRecord. You can be productive with it in 30 minutes. Be warned, it is NOT at the level of NHibernate in terms configuration options, etc. It is hugely productive, and the code is very clean. We use it on most of our projects.

David Robbins
I use it and like it. Havent used NHibernate though, so maybe my frame of reference is screwed up.
geofftnz
+2  A: 

I second David Robbins's mention of subsonic. One downfall that I will point out though is that the documentation is less than perfect. It's really pretty simple to work with, you just have to know how to do it.

I suppose one other thing that's worth mentioning: what databases do you need to support? If you need to work with anything other than SQL Server, I wouldn't use LINQ. I could be wrong on this, but last time I checked, there isn't any good support for databases other than SQL Server.

The last thing that I would mention is that you don't have to choose just one. I've used nHibernate, LINQ to SQL, and Subsonic in the same project with decent success (although I'd advise to avoid that if at all possible). My advice is to spend a little bit of time with all of them and learn what's best about each one.

Here's what I would say about each one:

  • LINQ to SQL - very easy to get up and running with. But if you want to use something other than SQL Server, you're pretty much SOL.
  • NHibernate - This is probably the most powerful out of all the ORMs and it supports the most RDBMSes, but it can be tedious to work with as there aren't a lot of really good free code generators to work with (can't speak for the non-free ones though).
  • SubSonic - A good compromise between NHibernate and LINQ to SQL. It doesn't support as many RDBMSes as NHibernate, but it works with most of the ones you'd encounter in the business world.

TL;DR: NHibernate if you need to have a lot of control over the database or need to support something obscure. SubSonic if you need to work with something other than SQL Server. LINQ to SQL if you just want to run against SQL Server.

Jason Baker
Nice job detailing the pro's and con's.
David Robbins
+1  A: 

Checkout XmlDataMapper a simple free ORM (LGPL Licence) which leaves a low memory footprint compared to the other giants.
The sample project provided should be good enough to get started.

To integrate XmlDataMapper all you need to do is 4 little steps

  1. Create a Business Entity / DTO for the tables
  2. Create an XML File with the mapping information between the table and the DTO.
  3. Specify the DTO and xml file in the configuration.
  4. Just call the DTOConverter.Convert(dataReader) and other such methods to convert your database record to DTO / Business Entity
Binoj Antony