Why should you use an ORM?

DatabaseOrm

Database Problem Overview


If you are motivate to the "pros" of an ORM and why would you use an ORM to management/client, what are those reasons would be?

Try and keep one reason per answer so that we can see which one gets voted up as the best reason.

Database Solutions


Solution 1 - Database

The most important reason to use an ORM is so that you can have a rich, object oriented business model and still be able to store it and write effective queries quickly against a relational database. From my viewpoint, I don't see any real advantages that a good ORM gives you when compared with other generated DAL's other than the advanced types of queries you can write.

One type of query I am thinking of is a polymorphic query. A simple ORM query might select all shapes in your database. You get a collection of shapes back. But each instance is a square, circle or rectangle according to its discriminator.

Another type of query would be one that eagerly fetches an object and one or more related objects or collections in a single database call. e.g. Each shape object is returned with its vertex and side collections populated.

I'm sorry to disagree with so many others here, but I don't think that code generation is a good enough reason by itself to go with an ORM. You can write or find many good DAL templates for code generators that do not have the conceptual or performance overhead that ORM's do.

Or, if you think that you don't need to know how to write good SQL to use an ORM, again, I disagree. It might be true that from the perspective of writing single queries, relying on an ORM is easier. But, with ORM's it is far too easy to create poor performing routines when developers don't understand how their queries work with the ORM and the SQL they translate into.

Having a data layer that works against multiple databases can be a benefit. It's not one that I have had to rely on that often though.

In the end, I have to reiterate that in my experience, if you are not using the more advanced query features of your ORM, there are other options that solve the remaining problems with less learning and fewer CPU cycles.

Oh yeah, some developers do find working with ORM's to be fun so ORM's are also good from the keep-your-developers-happy perspective. =)

Solution 2 - Database

Speeding development. For example, eliminating repetitive code like mapping query result fields to object members and vice-versa.

Solution 3 - Database

Making data access more abstract and portable. ORM implementation classes know how to write vendor-specific SQL, so you don't have to.

Solution 4 - Database

Supporting OO encapsulation of business rules in your data access layer. You can write (and debug) business rules in your application language of preference, instead of clunky trigger and stored procedure languages.

Solution 5 - Database

Generating boilerplate code for basic CRUD operations. Some ORM frameworks can inspect database metadata directly, read metadata mapping files, or use declarative class properties.

Solution 6 - Database

You can move to different database software easily because you are developing to an abstraction.

Solution 7 - Database

Development happiness, IMO. ORM abstracts away a lot of the bare-metal stuff you have to do in SQL. It keeps your code base simple: fewer source files to manage and schema changes don't require hours of upkeep.

I'm currently using an ORM and it has sped up my development.

Solution 8 - Database

So that your object model and persistence model match.

Solution 9 - Database

To minimise duplication of simple SQL queries.

Solution 10 - Database

The reason I'm looking into it is to avoid the generated code from VS2005's DAL tools (schema mapping, TableAdapters).

The DAL/BLL i created over a year ago was working fine (for what I had built it for) until someone else started using it to take advantage of some of the generated functions (which I had no idea were there)

It looks like it will provide a much more intuitive and cleaner solution than the DAL/BLL solution from http://wwww.asp.net

I was thinking about created my own SQL Command C# DAL code generator, but the ORM looks like a more elegant solution

Solution 11 - Database

I think there are a lot of good points here (portability, ease of development/maintenance, focus on OO business modeling etc), but when trying to convince your client or management, it all boils down to how much money you will save by using an ORM.

Do some estimations for typical tasks (or even larger projects that might be coming up) and you'll (hopefully!) get a few arguments for switching that are hard to ignore.

Solution 12 - Database

Compilation and testing of queries.

As the tooling for ORM's improves, it is easier to determine the correctness of your queries faster through compile time errors and tests.

Compiling your queries helps helps developers find errors faster. Right? Right. This compilation is made possible because developers are now writing queries in code using their business objects or models instead of just strings of SQL or SQL like statements.

If using the correct data access patterns in .NET it is easy to unit test your query logic against in memory collections. This speeds the execution of your tests because you don't need to access the database, set up data in the database or even spin up a full blown data context.[EDIT]This isn't as true as I thought it was as unit testing in memory can present difficult challenges to overcome. But I still find these integration tests easier to write than in previous years.[/EDIT]

This is definitely more relevant today than a few years ago when the question was asked, but that may only be the case for Visual Studio and Entity Framework where my experience lies. Plugin your own environment if possible.

Solution 13 - Database

Abstract the sql away 95% of the time so not everyone on the team needs to know how to write super efficient database specific queries.

Solution 14 - Database

.net tiers using code smith templates

http://nettiers.com/default.aspx?AspxAutoDetectCookieSupport=1

Why code something that can be generated just as well.

Solution 15 - Database

convince them how much time / money you will save when changes come in and you don't have to rewrite your SQL since the ORM tool will do that for you

Solution 16 - Database

I think one cons is that ORM will need some updation in your POJO. mainly related to schema, relation and query. so scenario where you are not suppose to make changes in model objects, might be because it is shared among more that on project or b/w client and server. so in such cases you will need to split it in two levels, which will require additional efforts .

i am an android developer and as you know mobile apps are usually not huge in size, so this additional effort to segregate pure-model and orm-affected-model does not seems worth full.

i understand that question is generic one. but mobile apps are also come inside generic umbrella.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimon MunroView Question on Stackoverflow
Solution 1 - DatabaseChuckView Answer on Stackoverflow
Solution 2 - DatabaseBill KarwinView Answer on Stackoverflow
Solution 3 - DatabaseBill KarwinView Answer on Stackoverflow
Solution 4 - DatabaseBill KarwinView Answer on Stackoverflow
Solution 5 - DatabaseBill KarwinView Answer on Stackoverflow
Solution 6 - DatabaseMatt FenelonView Answer on Stackoverflow
Solution 7 - DatabasecuriousdorkView Answer on Stackoverflow
Solution 8 - DatabasecletusView Answer on Stackoverflow
Solution 9 - DatabaseTim WardleView Answer on Stackoverflow
Solution 10 - DatabaseDan McClainView Answer on Stackoverflow
Solution 11 - DatabaseAnders FjeldstadView Answer on Stackoverflow
Solution 12 - DatabaseChuckView Answer on Stackoverflow
Solution 13 - DatabaseJaredView Answer on Stackoverflow
Solution 14 - DatabaseJoboView Answer on Stackoverflow
Solution 15 - DatabasezmfView Answer on Stackoverflow
Solution 16 - DatabaseShailendra Singh RajawatView Answer on Stackoverflow