views:

770

answers:

11

I am starting a new ASP.Net MVC project and was wondering what is the best Model approach to take.

Most demos recommend LinqToSQL but I know Microsoft are not really enhancing this product and are focusing more on the Entity Framework.

I like the Subsonic approach but I thought this was going to be built in with MVC with version 3 but there has been no news about the development stage of this project so I am a bit wary of using version 2 if there is to be a new release soon.

I have heard of NHibernate and Castle Record but have not had any experience at all with these and have heard numerous pros/cons for both.

Any help would be highly recommended!

+11  A: 

nHibernate learning curve is higher but worth it. Use Fluent nHibernate to ease the pain a bit. nHibernate is less visual that some ORMs but that is really only an issue when you are beginning and once you have experience with it the lack of visual designer is a benefit IMO.

Craig
I've just had a brief look at NHibernate and it looks like I have to create the database classes myself by typing all columns etc into a class. If that's right, I don't want to do that for loads of tables.
Jon
You're already doing things backwards, by starting with the database tables instead of starting with the domain-model classes. The NHibernate way is to start by writing the domain-model classes before you even think about the database tables you will need later on.
Justice
(in supplement to the comment by Justice:) - and then you can let NHibernate generate and even migrate the schema for you!
mookid8000
Jon, there are plenty of code generation tools (ie MyGeneration) that can generate the classes for you. But like the other comments say, nHibernate works best with Domain Driven Design where you design the classes first.
Craig
Can you explain what you mean by domain model classes? Do you mean classes with methods such as AddProduct(Product product)?
Jon
Have a look at http://stackoverflow.com/questions/123886/where-can-i-find-good-domain-driven-design-resources
Craig
A: 

I worked with a product called netTiers (http://www.nettiers.com/) on a project and that worked very well. It's a CodeSmith script that generates business objects from your database tables, views and stored procedures.

Martin Randall
+2  A: 

I use linq2sql and I think it works absolutely great if you use MSSQL as your database (it works on other databases too, but it requires some 3rd party tools). Yes, Microsoft has 'discontinued' its development, but it is mature enough for the average project. I doubt you will run into any walls. If you're starting a big project that you need to support for years to come, then the Entity Framework might be more suitable.

I personally haven't used NHibernate and Subsonic before, but like Craig says, the learning curve of NHibernate is a bit more steep, though it is supposed to be a great framework (everything from what I heard or read).

Razzie
Microsoft has not discontinued it's development. It currently has 5 developers working on it, ASP.NET MVC only has 3, just to put it into pespective.
Chad Moran
Though I placed discontinued between quotes, I do believe that it is close to death, see: http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspxIf you have a source for what you say, I'd be happy to read it though :)
Razzie
I talked to Scott Hanselman about it over dinner at MIX09 2 weeks ago, he said it has 5 devs currently on it.
Chad Moran
Ah ok, I'll take your word for that ;-) That is good news though, since I like linq2sql.
Razzie
+4  A: 

LLBLGenPro works great for me. It is the best .net ORM out there. I'd avoid Linq to Sql because you'll have problems once the project grows and it is rather weak in features (besides the obvious downsides, like works only with SQL Server, etc.)

Miha Markic
A: 

If it's not a huge project, I'd use Linq To SQL. If you need all the bells and whistles, bone up on nHibernate and have at it. I don't think you'd run in to any serious issues one way or the other.

Justin Niessner
+2  A: 

If you're willing to spend some money or have a smaller project I'd at least try out LightSpeed. I found pain points withe very ORM I've ever tried and I'm currently using LINQ to SQL myself.

LightSpeed is pretty feature rich though the LINQ support is missing a few features nothing major that can't be worked around. It's pretty close to being the same as LINQ to SQL and LINQ to Entities but has a few more features like Full-text Search.

Chad Moran
+4  A: 

An article about SubSonic, LinqToSql, and NHibernate: ASP.NET MVC: Choosing Your Data Access Method

alexandrul
Despite the title of the blog post, the choice of data access method actually has nothing to do with ASP.NET MVC.
Matt
@Matt: that's because ASP.NET MVC doesn't impose any restrictions on the ORM used, it's up to you to choose one.
alexandrul
+1  A: 

Castle ActiveRecord works really well for small-to-medium projects based on MVC - I'm using it on a couple of projects at the moment and finding it a very good fit with ASP.NET MVC.

It's an implementation of the active record pattern that uses NHibernate under the hood - in other words, as long as you're happy having one class per database table, it'll do most of the NHibernate configuration for you, leaving you free to write code like:

Customer bob = Customer.Find(/* customer Id goes here */);
bob.FirstName = "Robert";
bob.Save();

Invoice newInvoice = new Invoice();
newInvoice.Products.Add(Product.Find(/* product ID goes here */);
bob.Invoices.Add(newInvoice);
bob.Save();

with very little explicit NHibernate configuration (assuming you have Customer, Invoice and Product tables in your DB, of course)

Because it's NHibernate under the hood, migrating to NHibernate if you need to should be straightforward - you'll already have all the necessary references and libraries. There are a few aspects of NHibernate behaviour that you can't ignore (like session scopes and flushing) - but it's not too hard to work out what's going on with these, and using them in ActiveRecord will give you a head start if/when you need to move to using NHibernate directly.

Dylan Beattie
Does Castle ActiveRecord generate classes for you based on a database you point it at?
Jon
No - although it will do the opposite (generate your DB schema based on annotated classes)There's also ActiveWriter - http://using.castleproject.org/display/Contrib/ActiveWriter - that offers a visual design tool (drag'n'drop from your DB to create objects) as a Visual Studio plugin.
Dylan Beattie
+1  A: 

We're using Fluent + NHibernate here, it works pretty well. Some alternatives that mesh well are SubSonic, Entity Framework, and Linq to SQL. I think straight NHibernate is a bit more cumbersome than it is worth, but is a great underlying layer with Castle ActiveRecord, and with Fluent.

Tracker1
+5  A: 

For SubSonic, Rob just release an new MVC template which you can use when starting a new project that bakes a lot of good stuff into a new MVC project including a pre-release of SubSonic 3. Detais are available on Rob Conery's blog.

Pervez Choudhury
Have just seen that, will take a look!
Jon