views:

476

answers:

5

We're about to embark on some ASP.NET MVC development and have been using our own entity framework for years. However we need to support more than our entity framework is capable of and so I'd like to get some opinions about using MVC with a more robust framework. We have narrowed down or choices to either NHibernate (with the Fluent APIs) or LINQ to SQL.

Which framework lends itself best to MVC style development (I know SO uses LINQ to SQL)?

If we want to support SQL Server, Oracle, MySQL - does that exclude LINQ to SQL?

+1  A: 

LINQ to SQL is for SQL Server. Entity Framework supports some other databases as well.

NHibernate is good choice. You may use Castle ActiveRecord (it's built on top of NH) if you are doing data based application or Sharp Architecture for project guidance.

Marek Tihkan
+1  A: 

Entity Framework integrates nicely with MVC and supports other databases.

Jeffrey Hines
+3  A: 

I've had great success using Fluent NHibernate and dependency injection (Ninject, in my case) with MVC.

It seems to me though that any mature ORM should work well with MVC. Since the nature of MVC (Model/View/Controller) separates the three concerns, any ORM should fit quite nicely into the "Model" role.

Stuart Childs
That's the stack that I'm currently leaning toward. Where there any gotchas that I should be aware of?
Paul Alexander
None that immediately come to mind, or at least that are not now solved. I originally implemented my own module for d-injecting the MVC controllers, but there is now an official Ninject implementation: http://github.com/enkari/ninject.web.mvc/tree/master I'm sure that now MVC is gainly popularity quickly, the other DI frameworks have similar modules if you don't go with Ninject. Depending on how complex your NH config is, you might have some extra work getting the session factories injecting but FNH plays very well with DI by the nature of its DSL interface.
Stuart Childs
Stuart - Why is one type of data access framework/technology preferred over another in ASP.NET MVC development? I thought that ASP.NET MVC makes no assumptions (nor has any biases) about the type of data access used? Since the data access is happening somewhere in the Model I could use traditional ADO.NET if I had solid data access code that I'd already written and wanted to reuse.
Matt
Well, that was exactly my point. Because MVC separates the concerns quite well, it doesn't really matter how you design your model as long as it works for you. I mentioned NHibernate and ORMs in general because Paul mentioned he was already using an ORM ("entity framework") and, in fact, asked whether he should use LINQ to SQL or NHibernate. As for why one would choose an ORM in the first place, Bill Karwin has a good blog post discussing the pros and cons (with a link back to the related question on SO): http://karwin.blogspot.com/2009/01/why-should-you-use-orm.html
Stuart Childs
+1  A: 

The short (and not so helpful) answer is that both of the ORMs you've mentioned will work with MVC. A longer answer is that you should think about how you want to work with your model objects. For example, do you want to do domain object first development (ala a Domain Driven Design approach), or are you implementing a "forms over data" type application where you might want to generate a data access layer from an existing db? What is your preference for specifying mappings? Do you want to use a fluent interface or are you happy with mapping files (or attributes on your domain objects)?

These are the type of questions you need to investigate when choosing an ORM -- and they're mostly independent of whether you're using MVC or Winforms.

Rob Scott
+4  A: 

As someone who has just switched from Linq to Sql to (Fluent) nHibernate here are a few things I've noticed.

1-Linq to Sql took so long to figure out how to do the equivalent of a join-subclass. After many modifications, I read somewhere that it is not possible. It can only map inheritance if ALL the columns are in that same table. this is great if there are a few columns, but there are tons in my case and sub classes are parents to other sub classes and so on. Why should I put them all in one table for the sake of my ORM?

2- nHibernate from experience has been robust ( sometimes too much for small quick projects ) and although familiar with it through small projects, I felt it might be too much and went the route of linq to Sql since I could generate a dbml file and be going within minutes.

3- Fluent nHibernate. Takes the best of both worlds (in my case). I can map the way I want to and have my database the way I want and not have to compromise in my domain or data models. Also one word: Automapping... icing on the cake.

I would have had to go with another ORM once I found limitations and hit a few road bumps with Linq2Sql, but Fluent nHibernate made this choice easy and I don't think I'll leave it unless something comes around that does the job even better.

So, like Rob Scott said, the question is how are you abstracting you domain => data model? and are you starting with a domain or a database? How complex are the relationships? If you have any inheritance at all I'd say just go with a richer ORM framework and save yourself the grief.

Fluent NHibernate has some of the best docs I've ever found and there is so much support / notes / blogs and resources it's self-hate to do anything less... IMO! I was up and running in < 24 hours

Oh and if your'e new to nHibernate pick up the nHibernate in Action book to help grease the wheels although there is a lot of help for that framework as well.

The best indication that a tool isn't working is when you have to WORK the tool... Linq to SQL I was customizing, reading white papers all sorts of madness and it refused to generate appropriate queries, right when I was tempted to modify my table and domain, I said let me give Fluent a whirl and I'm happy I did.

Good luck to ya.. sorry for the long response this has all been in the pas 5 or so days so I guess I'm still caught up :-)

5x1llz
This is exactly the kind of experience I was looking to learn from. I tend to think in the domain model - not db. The ORM tool that we built internally handles inheritance in multiple tables well so I'm accustomed to having that available. Looks like my choice to pursue fluent nHibernate was the right one.
Paul Alexander
5x1llz - Why is one type of data access framework/technology preferred over another in ASP.NET MVC development? I thought that ASP.NET MVC makes no assumptions (nor has any biases) about the type of data access used? Since the data access is happening somewhere in the Model I could use traditional ADO.NET if I had solid data access code that I'd already written and wanted to reuse.
Matt
@5x1llz, I also found the Fluent to be the best available solution currently. :) @Matt, MVC really makes no assumptios, but still, one ORM can be more convenient than another.
Venemo