Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM: why should i use it? [duplicate]

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.

like image 367
Cris Avatar asked Dec 17 '25 18:12

Cris


1 Answers

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.

like image 108
Abel Avatar answered Dec 20 '25 11:12

Abel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!