What ORM should I use for a ASP.Net MVC project? - asp.net-mvc

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!

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.

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.

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.)

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).

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.

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.

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.

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.

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.

Related

How to do deal with an evolutionary design of a web-site database?

I am about to start my first project on an ASP.NET MVC 3 web-site. This site will have data storage and I am wondering what's the best way to deal with the evolutionary design of the database behind the web-site - after the site launches, it will be extended and it will keep on growning thus database refactoring will be an issue. What's the best way to deal with this "problem"? I was thinking of using an ORM like NHibernate or Entity Framework, but neither of them support evolutionary database design.
Any help is appreciated.
Are you looking for migrations support for .NET primarily?
https://stackoverflow.com/questions/313/net-migrations-engine
https://stackoverflow.com/questions/8033/database-migration-library-for-net
.NET migrations: Setup and migrate multiple databases at runtime
I can assure you that developers are doing evolutionary database design with both NHibernate and Entity Framework, so it's entirely possible. Maybe you want a highly tooled, automated process though?

Performance reprogramming website in MVC3 with Entity Framework

At the moment I have a fairly big website with about 10k visitors a day.
This is a community website with news/blogs/videos and a big forum.
This all runs on a self made PHP5 application which performance faily well, has good performance. The database is a MySQL5.1 database.
Now I am getting fedup with PHP and the loose typed framework, lack of namespacing and a proper MVC setup, so I am thinking of rewriting the site in MVC3 ASP.NET.
Now I have experience with this, but not in the MVC framework yet, and I have a few questions about the performance, especially the Entity Framework:
Is it even worth using the entity framework? Will it cost alot of overhead and performance degration? I am not sure yet if I should switch to MSSQL.
Entity Framework was not performance optimized as far as i have worked with it. That is not it's primary goal by design. Performance is important of course, but EF is at most easy to use, and start with. It provides an fast way integrate with existing database, or create database really, really fast. Performance is not the primary goal, but improving indeed.
Here is an good chart with some benchmarks from the Microsoft team, that can be a ground for making the decision.
As you can see, there is a dramatic performance improvement from .NET 4.0 to 4.5 as far as it comes to "LINQ to Entities". It still seems twice slower than direct SQL though. LINQ to SQL is the slowest operation of course.
ASP.NET MVC is an good direction, especially if you want to try something different. EF is not the best you can do if you pursue performance. Writing the business layer on your own, including the SQL stored procedures would be better, but will require a lot more time.
When using an ORM like EF or NHibernate you always have to live with the trade-off between performance and convenience. If you can live with a relatively bad performance (I think it should be possible to run your site w/ an ORM) NHibernate should your first choice, from my point of view it is more mature while EF is still lacking provider support and has some shortcomings with respect to the develevopment workflow (which you possibly expect when using NHibernate but not when using a MS tool).
If you switch from PHP to .NET you should consider switching from MySQL to MSSQL, just because it fits perfectly into the MS ecosystem (and performance/scalability should be improved too, this could possibly outweight the performance degradation you expierence when using an ERM).
You could also take a look at LINQ which could be an alternative between a classic ORM and hard coded SQL commands (also with respect to the performance, LINQ to SQL is pretty fast and you can also use LINQ to Entitiy Framework when using EF (thats pretty slow)). LINQ would fit your needs if you want some level of abstraction wihtout the need of endless configuration and if you like RAD (who doesn't?).
In general the performance of ASP.NET MVC3 is quite good, but you should know that you need some experience to tweak your application and avoid (common) pitfalls. Cutting a long story short: You should be easily able to write an ASP.NET application that has a better performance than a scripted PHP page (by design)
But you should also know, if you decide to commit yourself to the MS ecosystem (.NET, MSSQL w/ LINQ/EF) its hard to break out and providers for LINQ and EF for non-MS RDBMS might cost some bucks (check www.devart.com).
Hope this gives you some guidance
Further reading:
ORM wars: Comparing nHibernate, LINQ To SQL & the Entity Framework
NHibernate vs Entity Framework: a performance test
NHibernate vs. Entity Framework 4.0
This is way too much of a general question, and there's no good answer to it. I highly recommend that you perform some tests using the Entity Framework as well as MVC3 and see that it meets your needs.
Also, and not to be condescending, but 10K visitors a day is not that much compared to other sites out there that are succesfully running ASP.NET MVC and Entity Framework.
In the end, I'd say it most definitely will meet your needs, but as with any project that scales, you will have to be aware of bottlenecks in your particular app and come up with solutions to address those bottlenecks.
This is 100% dependent on the queries.
I've worked on sites with 22 million a month with Nhibernate and about 10 million a month with Linq to Sql.
The queries that performed the slowest were always those weird aggregate quintuple join monsters. ORMs get you 95% of the way there. The rest you'll have to optimize. No ORM gets SELECT * FROM table wrong. Its the outliers that matter.
This will probably get closed as subjective/argumentative in the next five minutes, but I'll give it a shot:
You'll hear a lot of differing views regarding EF and other ORMs, but my personal view is that they cause more problems than they solve. Queries should be kept in the database, where they belong. Proper separation of concerns (SQL code in the database, app code in the project) saves you a lot of problems long-term.
Think of it this way: if your queries were all in stored procedures, you wouldn't even be thinking about migrating/rewriting them at this stage. You could focus on implementing the web side of your app instead of worrying about how to transition your SQL code.
So, skip EF for now, focus on what you know.

What to choose (in ASP.NET MVC) for a new webshop project?

What to choose for the foundation of a new webshop project? I have narrowed down my options into using ASP.NET MVC, so that is a must. That leaves me with:
Rob Conery's Kona (not being updated anymore but all in all seems a good foundation, though with a huge amount of features)
MVC MusicStore by Jon Galloway (very simple, in fact it could be easier to start from scratch and take in consideration all the special details we need)
Start from scratch
Anything else?
I've worked with the MVC for the last year so I could start from scratch and have the basic functionality developed in no time but I like Rob's extended functionality he added.
It is just that I am unsure how difficult it is to tailor Kona to our individual needs - anyone tried it yet? For instance, use EF instead of NHibernate?
To be honest, half the fun with Rob Conerys series was being "there" during the process he went through and the trials and tribulations he encountered. If you already have a foundation of using MVC for a year i'm not sure if there'd be too much more to take from it.
The most fundamental thing about the MVC pattern and the .NET tools that come into their own with using it (extension methods baby!), is that there are so many ways of solving the problem, some good, some better and occasionally some that make you go BAZINGA!.
If you have the time, i'd highly recommend just hacking away and see what you come up with.
After you've got a working prototype it's always good to then compare to what else is out there, for example with something like this post
You could also consider using Rob's new starter site: "Tekpub MVC 2.0 Starter Site". It's inspired loosely on his experiences with Rails, and has a bunch of nice utilities built in from the get-go. It's built as a result of his MVC 2.0 course on TekPub, which he recorded with Steven Sanderson.

ASP.NET MVC with Postgres; ORM recommendations?

I'm project managing an intranet application being developed at work. We're in the early planning stages. I've previously done all my development in Python using Django, but as we're a windows shop we're probably going to go with ASP.NET MVC.
We won't really be able to afford a SQLServer license though, so we were perhaps looking into using Postgresql. However I can't seem to find many examples or guides for people who want to utilise a third party ORM - or at least, an ORM with similar usage as Django - that works with Postgresql.
Ultimately we'd like to handle authentication via Active Directory [including groups], but store actual content within the db.
There have been previous questions of a similar nature, but most of them are over a year old when MVC was stil in Beta.
Any ideas?
NHibernate by a country mile.
It also supports MySql and should you want to change it has the main commmercial ones too. Haven't switched between db vendors but if you don't do much bespoke t-sql and say use fluent nhibernate you could almost plug and play between database platforms.
The support and community behind NHibernate when it comes to Mvc is second to none. It is categorically the ORM of choice.
You can try DataObjects.Net - open source ORM with GPLv3 or commercial licenses. It also supports Postrges.
NHibernate supports PostgreSQL. See http://vampirebasic.blogspot.com/2009/02/nhibernate-with-postgresql.html for some advice on how to integrate the two.
Also worth mentioning is that NHibernate now supports LINQ syntax. See http://ayende.com/Blog/archive/2009/07/26/nhibernate-linq-1.0-released.aspx for more details.
I'd recommend Mindscape LightSpeed. It supports PostgreSQL and has a visual designer baked into Visual Studio with full database round tripping to PostgreSQL.
When there was no add-in for Visual Studio to support PostgreSQL from the Server Explorer the guys wrote a free visual studio add-in for supporting it.
Solid O/R Mapper with LINQ mixed with first class visual model development against PostgreSQL. It is a commercial product however there is a free edition for small databases.
Mindscape LightSpeed O/R Mapper
Try Devart LinqConnect - http://www.devart.com/linqconnect/. This Framework supports PostgreSQL, Oracle, MySQL, SQLIte.
Unfortunately, most answers you get on a question like this are going to be based on the responder's opinion and experience and not based on yours.
Most of the suggestions here are good... however... if you are looking for a lightweight/fast ORM that is similar to Django, JackD has the right solution (LinqConnect)...
I've used most of the solutions listed including Django, and find that I usually pick LinqConnect if I'm looking for fast, lightweight and easy. For heavier (read larger) projects I would use something more robust like NHibernate.
But to answer your question correctly... the closest match and least learning curve for someone using Django would definitely be LinqConnect.

Why ASP.NET MVC favorites Linq to SQL over LINQ to Entities?

The question is in the title.
It doesn't favor one over the other at all. It's just common to use LINQ to SQL examples because they are simpler to setup and deploy, so it's easier to digest the sample code without getting distracted by something which deserves its own learning path.
I agree that it does not favor one over the other. I always assumed that Linq to SQL tended to be used in examples because it was released about a year earlier. Therefore, book writers were more familiar with Linq to SQL and/or felt it was more stable.
I agree with Rex in that it makes more sense, when giving a tutorial about ASP.NET MVC, to keep other technology decisions simple. Since either DAL implementation can be used, it is easiest to teach MVC by using Linq to SQL (the simpler of the two). Linq to SQL is also widely considered to be more light-weight.
I must admit, it would be nice to have more open-source examples of projects using ASP.NET MVC along with Entity Framework. I can tell you that it works fine, because I am using it on one project. However, it can be a bit more difficult to figure out some of the ideosyncrasies. Here is another question that shows some links to examples.
I think this tendency to use the path of least resistance in example is a diservice to new developers. How many times have you seen an example, with the caveat that it is not production worthy code, with no reason as to why it is not appropriate, or good direction on how to find what is best? Personally, I appreciate longer examples that actually lead me to discover how something should be used are more helpful.
In this particular case, using Linq to Entities would be much more useful, as it is seemingly the future.
In my opinion, it doesn't favor it. It's what you see in most examples, because Linq to Sql is the fastest way to get examples up and running. Rails follows the same convention of many examples using features (scaffolding for example) that you would rarely see used in a production site.
As all the other posters have said - L2S samples are just a lot easier to put together hence you'll see them quoted more. In reality your MVC models may not use L2S directly - they could be hooking up to a separate services tier or some data transfer objects exposed by another system entirely.

Resources