ORM Technology - nHibernate vs Linq
I'm back after some time away from my blog ;).. This time i would like to tell about my favourite "nHibernate" and ORM Technology. Not So Long Ago, i’ve attended a microsoft seminar on Visual Studio 2008 & VSTS. It was a nice presentation and we have had a delicious lunch too;)…Presentation on VSTS was amazing. The presenter, Tejsvi Kumar(Technology specialist from microsoft) , who provided clear idea on how we can handle a big project by Only using VSTS.Then he had shown demo on VSTS how Project manager can assign tasks, view status or create test cases on the fly etc. In between he also mentioned on visual studio 2008 features. i would to like express my appreciation to them for sharing their exp with us. And more than that, they’ve come up with more knowledge by replying our queries. Me too sent a mail regarding some queries on LINQ. I got a very detailed reply on this. I would like to share their reply with everyone since it provides a neat explaination on LINQ n other technologies.
Q :
Can u differentiate between Ado.Net and LINQ
A : ADO.NET is a mechanism to connect to the data
source (like ODBC) whereas LINQ is a query mechanism to
query *any* kind of data not necessarily data from a
database. As an example try the following simple LINQ
program:
{
int[] numbers = { 3, 5, 6, 1 };
orderby n ascending
select n;foreach(var e in exp )
{
Console.WriteLine(e);
}
}
This program demonstrates the following:
- LINQ is a language concept (integrating queries in the
programming language)
- LINQ queries can be quite expressive including joins,
where clauses, grouping etc.
- LINQ has nothing to do with databases in particular –
however you can build LINQ based extensions that enable
you to to query any kind of database using LINQ queries
(e.g. LINQ to XML, LINQ to SQL, LINQ to datasets, LINQ to
Entities, LINQ to Objects)
Q : Is LINQ is nothing but a copy of nHiberante ?
A : I disagree - LINQ is NOT copied from
nHiberbate. The example in point 1 will explain that
nHibernate has nothing similar. However you can definitely
compare LINQ-to-SQL with nHibernate. Now nHibernate itself
is no new technology – both nHibernate and LINQ-to-SQL are
products that make use of the Object Relational Mapping
(ORM) Technology. There are pro and cons of ORM technology
and they are very widely discussed in the technology
circles. You can get an insight into them on the net. The
important thing to remember is that there are definitely
some very important benefits (inspite of some
disadvantages) of ORMs and if as an architect your
analysis proves that ORM wil benefit your project you
should go for it. Generally every technology has its pros
and cons (like any other thing in life) and as a smart
Architect you need to understand your requirements in
nicely and then choose the technology that suits you best.
Q :
LINQ(nHiberante) causes difficulty while debugging the
code. Its very difficult to find which line throws
exception.
A : This statement confirms my comment in point 3.
Pro of LINQ-to-SQL(nHibernate) – faster code development;
Con of LINQ-to-SQL (nHibernate) – possibly more extensive
debugging. However, if you make you of some best practices
for debugging you can reduce the time.
Q :
Its very difficult to make changes according to Database
changes..
A : Actually, with ORMs it becomes easier to
abstract the Database changes from Application changes. So
if your application is architected correctly and there are
DB changes – with LINQ-to-SQL (or nHibernate) you will
need to do NO or almost minimum changes in your code (all
you have to do is change the mapping layer)
Q : Performance is slow compared to ado.Net(i’ve checked
wit nHibernate, not wit LINQ)
A : Please read my blog post on performance
generally:
http://blogs.msdn.com/bsinghal/archive/2007/07/16/there-is-a-performance-problem.aspx.
To compare the performance of LINQ v/s non-LINQ scenario –
you will need to do very thorough testing and make sure
that we compare apples to apples.Regarding performance is
slow with nHibernate or LINQ-to-SQL (ORMs) as compared to
ADO.NET – yes that can be true in some cases even after
doing all the possible optimizations etc because ORMs do
add an extra layer of processing but they provide a lot of
flexibility in return. The point here is that one should
analyse the technology properly and make sure that any
technology they choose addresses their requirements and
needs. So if you are ready to spend 10 times more time in
developing the application in ADO.NET at the cost of
gaining lets say 2% performance improvement and of that is
of more importance for your business then yes using
ADO.NET is better.