4

Possible Duplicates:
Are there good reasons not to use an ORM?
Why should you use an ORM?

Hello, i develop since many years in SQL (many different sql servers like Oracle, MSSql, Mysql) and i always been happy in doing that. I've recently seen a lot of stuff around ORMs and i'm asking why should i move toward this world. Can you give me some good points for spending time in learning a different way to do what i already do today? Thanks in advance c.

Community
  • 1
  • 1
Cris
  • 91
  • 1
  • 3
  • 1
    because all the cool programmers do it :) – Bozho Jun 03 '10 at 16:06
  • 6
    at least 4 duplicates: http://stackoverflow.com/questions/448684/why-should-you-use-an-orm http://stackoverflow.com/questions/398134/what-are-the-advantages-of-using-an-orm http://stackoverflow.com/questions/494816/using-an-orm-or-plain-sql http://stackoverflow.com/questions/194147/are-there-good-reasons-not-to-use-an-orm – Bozho Jun 03 '10 at 16:07
  • http://stackoverflow.com/questions/494816/using-an-orm-or-plain-sql ? – dzen Jun 03 '10 at 16:08
  • Come on...even giving you the benefit of a doubt, when you created a question a several similar questions were suggested including [Why should you use an ORM?](http://stackoverflow.com/questions/448684/why-should-you-use-an-orm) (as Bozho pointed out). – Roman Jun 03 '10 at 16:11
  • 1
    How do questions like this get upvoted. – Matt Mitchell Jul 20 '10 at 01:17

2 Answers2

6

If you write things like this, all these years:

string query = "SELECT * FROM myUsers WHERE Name = '" + aName + "'";
ResultSet result = sql.Execute(query);
UserList users = TransformResultSetToUsers();

then you it's time to find yourself a proper ORM (and to get very afraid of SQL injection attacks).

What an ORM does for you is hide the methods, security, performance, caching, mapping etc that otherwise would cost a tremendous amount of time to program yourself. What about doing this:

User currentUser = DaoUsers.GetUserByName(aName);
currentUser.Name = "new name";
DaoUsers.UpdateUser(currentUser);

which covers all querying, updating etc for you. And if you test at home with MySQL, and at work with SQL Server, all you need to do is change the connection string. Oh, and it provides caching (in cases like NHibernate and others), which makes this lightning fast, without even touching the database if it's not needed.

All-in-all: using an ORM is imply easier. No worries anymore. No more typos. Let the ORM engine take care of everything. Design the database from objects, not from "CREATE TABLE" statements. Focus on programming logic, instead of worry about SQL diversities.

And above all: type safety if you use an ORM with a type safe OO language, which will let the compiler catch your errors before you even start to debug for them.

Abel
  • 56,041
  • 24
  • 146
  • 247
4

The advantages I can see, from doing a few database projects (without ORM) are:

  1. A large chunk of code that does CRUD (Create, Read, Update, Delete) can be simply conjured up by writing a mere XML file or instantiating a couple of objects. This can give you a huge head-start.

  2. You can define your schema in a single location, ie, the XML file or couple of objects. This is fantastic, as it implements the DRY principle!

  3. A nice, clean abstraction of objects is given to your object-oriented application to access data, as opposed to maybe some hackish objects you created yourself (depending on your programming skill).

The disadvantages:

  1. The ORM's abstraction can leak, and give you headaches in trying to figure out the magic it is doing in the backend when it doesn't work.

  2. ORMs aren't available for many languages, or are in alpha stage (C++, I'm looking at you!).

NB: I must admit I haven't used an ORM yet, but I plan on using one for my next project.

J. Polfer
  • 12,251
  • 10
  • 54
  • 83