views:

1693

answers:

10

I have been playing with some LINQ ORM (LINQ directly to SQL) and I have to admit I like its expressive powers . For small utility-like apps, It also works quite fast: dropping a SQL server on some surface and you're set to linq away.

For larger apps however, the DAL never was that big of an issue to me to setup, nor maintain, and more often than not, once it was set, all the programming was not happening there anyway...

My, honest - I am an ORM newbie - question : what is the big advantage of ORM over writing a decent DAL by hand?

(seems like a double, couldn't find it though)

UPDATE : OK its a double :-) I found it myself eventually :

ORM vs Handcoded Data Access Layer

+13  A: 
  • Strong-typing
  • No need to write the DAL yourself => time savings
  • No need to write SQL code yourself => less error-prone
Thomas Levesque
+4  A: 

I've used Hibernate in the past to dynamically create quite complex queries. The logic involved to create the appropriate SQL would have been very time-consuming to implement, compared with the logic to build the appropriate Criteria. Additionally, Hibernate knew how to work with various different databases, so I didn't need to put any of that logic in our code. We had to test against different databases of course, and I needed to write an extension to handle "like" queries appropriately, but then it ran against SQL Server, Oracle and HSqldb (for testing) with no issues.

There's also the fact that it's more code you don't have to write, which is always a nice thing :) I can't say I've used LINQ to SQL in anything big, but where I've used it for a "quick and dirty" web-site (very small, rarely updated, little benefit from full layer abstraction) it was lovely.

Jon Skeet
Jon, tx, was it for java or dotnet? Would you recommand NHibernate over linq?
Peter
@Peter i would :)
Arnis L.
@Peter: This was in Java. I don't have much experience of ORM on .NET for production applications, to be honest.
Jon Skeet
@Jon so thát's the field you haven't got much experience in ;-)
Peter
@amis : you have tried both than? And you lose linq than i suppose, don't you miss it?
Peter
@Peter: There's LINQ to NHibernate as well...
Jon Skeet
@Jon Yes, I was just reading about it. Its labeled immature though, among other not nice things
Peter
A: 

Easy to persist a complex object graph to/from database.

Ian Nelson
A: 

Easy to implement lazy-loading.

Ian Nelson
+2  A: 

Portability between different db vendors.

Blake Pettersson
+2  A: 

My, honest - i am an ORM newbie - question : what is the big advance of ORM over writing a decent DAL by hand?

Not all programmers are willing or even capable of writing "a decent DAL". Those who can't or get scared from the mere thought of it, find LINQ or any other ORM a blessing.

I personally use LINQ to manipulate collections in the code because of its expressiveness. It offers a very compact and transparent way to perform some common tasks on collections directly in code.

LINQ will stop being useful to you when you will want to create very specific and optimized queries by hand. Then you are likely to get a mixture of LINQ queries intermingled with custom stored procedures wired into it. Because of this considerations, I decided against LINQ to SQL in my current project (since I have a decent (imho) DAL layer). But I'm sure LINW will do just fine for simple sites like maybe your blog (or SO for that matter).

With LINQ/ORM there may also be a consideration of lagging for high traffic sites (since each incoming query will have to be compiled all over again). Though I have to admit I do not see any performance issues on SO.

You can also consider waiting for the Entity Framework v2. It should be more powerful than LINQ (and hopefully not that bad as v1 (according to some people)).

User
"Each incoming query will have to be compiled all over again" - I'd certainly hope not! A prepared statement cache should eradicate that problem.
Jon Skeet
You can also wire in SP in your linq. So that you can first create your DAL through Linq and later on optimize where needed by calling a SP.
borisCallens
Your compiling of queries can be optimized by so called Compiled Queries. You can statically create them so they get compiled only once. Don't know if this is what Jon is refering to, but I thought I'd just drop the term so ppl can easely google it.
borisCallens
+1  A: 

Transparent persistence - changes get saved (and cascaded) without you having to call Save(). At first glance this seems like a nightmare, but once you get used to working with it rather than against it, your domain code can be freed of persistence concerns almost completely. I don't know of any ORM other than Hibernate / NHibernate that does this, though there might be some...

DotNetGuy
+3  A: 

Well, for me it is a lot about not having to reinvent/recreate the wheel each time I need to implement a new domain model. It is simply a lot more efficient to use for instance nHibernate (my ORM of choice) for creating, using and maintaining the data access layer.

You don't specify exactly how you build your DAL, but for me I used to spend quite some time doing the same stuff over and over again. I used to start with the database model and work my way up from there, creating stored procedures etc. Even if I sometimes used little tools to generate parts of the setup, it was a lot of repetitive coding.

Nowadays I start with the domain. I model it in UML, and for most of the time I'm able to generate everything from that model, including the database schema. It need a few tweaks here and there, but with my current setup I get 95% of the job with the data access done in no time at all. The time I save I can use to fine tune the parts that need tuning. I seldom need to write any SQL statements.

That's my two cents. :-)

tmatuschek
A: 

I have used Linq, I found it very useful. I saves a lot of your time writing data access code. But for large applications you need more than DAL, for them you can easily extent classes created by it. Believe me, it really improves your productivity.

Sharique
A: 

The best way to answer the question is to understand exactly what libraries like Hibernate are actually accomplishing on your behalf. Most of the time abstractions exist for a reason, often to make certain problems less complex, or in the case Hibernate is almost a DSL for expression certain persistance concepts in a simple terse manner.

  • One can easily change the fetch strategy for collections by changing an annotation rather than writing up lots of code.

  • Hibernate and Linq are proven and tested by many, there is little chance you can achieve this quality without lots of work.

  • Hibernate addresses many features that would take you months and years to code.

mP