Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code generator vs ORM

I wrote a code generator which generates POCOs and repositories for a given SQL Server/CE database. Nothing fancy, only simple CRUD procedures using classic ADO.Net. My question is, why should I use an ORM like L2S/EF4 over custom code generator? Every 2 or 3 years Microsoft ships some new data access framework/technology and I know quite a few developers who can't always be in touch with the latest technologies, but every one of them knows the classic ADO.Net, how to modify the existing code and how to develop a new functionalities. Do ORM tools bring something which is a must nowdays, or I can stick with a classic ADO.Net?

Thank you!

like image 951
šljaker Avatar asked Jan 24 '11 23:01

šljaker


3 Answers

I went to web programming, with its newer languages that lack proper data handling (which leads to the perceived need for ORM) at the same time I built my be-all-end-all code generator. Have never looked back.

One reason I would never consider an ORM is because I actually know how databases work, very well thank you. I don't something to try to make it look like I'm not using a Relational database, I want something to get me the power of the database with as little work as possible -- and that will never be an ORM because that is not what they are about.

In my experience a good dictionary-based generator is the truest D-R-Y programming (Don't repeat yourself), it can free me from the nit-and-pick of working with the DB and allow me to concentrate on what matters, getting good biz logic written on top of a solid table design.

EDIT: Two more points:

1) Going a non-ORM route is, if nothing else, lonely, inasmuch as ORM is so much the rage it is hard to find the people who never needed it and don't see the point. But let your technical judgement guide you.

2) A couple of years ago I wrote a blog entry, "Why I do not use ORM", which I will not link to because it was too inflammatory. After some time I tried again to capture the sense of why it is possible to look at ORM objectively and see no value, without being inflammatory, and that link is: http://database-programmer.blogspot.com/2010/12/historical-perspective-of-orm-and.html

like image 132
Ken Downs Avatar answered Nov 19 '22 22:11

Ken Downs


Depends; do you like doing useless busywork with the database, or would you prefer to have it all generated?

Seriously though, for me, there is absolutely no question. Every knew project should start with an ORM, and for me, LLBLGen is the best. It's not free though. But it saves so much time in developing the data layer, and provides a nice structure to work with.

Really, it's a matter of deciding how you want to spend time. If you see value in working in the data layer, because of some series of reasons you can justify, when weighed against something like LLBLGen, then do it. But for me, I do not.

Of course, I agree with you, having to constantly change ORM's is not ideal. So I suggest you spend a bit of time (perhaps a few weeks) determining which one is the best for the style you like to develop in, and the way you structure your projects, and then go for it. Most of the main ways are very well supported these days anyway, so you couldn't be faulted to choosing one of them and standardising it.

like image 27
Noon Silk Avatar answered Nov 20 '22 00:11

Noon Silk


One point in favor of an ORM is compile time checking. I'll use the entity framework as an example, since that's what I use as an ORM.

If you need to make a database schema change during development (remove a column, table, etc.), you update your model from the database and then compile. Everywhere that column or table was referenced now shows up as a compile time error, making it easy to catch bugs that would likely only be noticed at runtime with standard ado.net.

Another thing to think about is ad-hoc queries - what if you want to pull back just a few columns of data from a table? With generated code, you need to add a new query, class to fill with data, etc. With an ORM, you can do something like this, where an anonymous class is generated for you so you don't have to go through the busywork of creating one yourself.

Essentially, an ORM will handle all the edge cases that a home grown solution generally doesn't.

var q = from c in ctx.contact
        where c.contactId < 4
        select c.firstName, c.lastName;


foreach(var temp in q){
   string fullname = temp.firstName + " " + temp.LastName;
}
like image 1
Neil Avatar answered Nov 19 '22 22:11

Neil