2

I was reading this SO question but still I am not clear about one specific thing.

If I use NHibernate, why do I need LINQ?

The question in my mind becomes more aggravated when I knew that NHibernate also included LINQ support.

LINQ to NHibernate?

WTF!

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452

5 Answers5

6

LINQ is a query language. It allows you to express queries in a way that is not tied in to your persistence layer.

You may be thinking about the LINQ 2 SQL ORM.

Using LINQ in naming the two is causes unfortunate confusions like yours.

nHibernate, EF, LINQ2XML are all LINQ providers - they all allow you to query a data source using the LINQ syntax.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

First of all LINQ alone is not an ORM. It is a DSL to query the objects irrespective of the source it came from.

So it makes perfect sense that you can use LINQ with Nhibernate too

I believe you misunderstood the LINQ to SQL with plain LINQ.

RameshVel
  • 64,778
  • 30
  • 169
  • 213
3

Well, you don't need Linq, you can always do without it, but you might want it.

Linq provides a way to express operations that behave on sets of data that can be queried and where we can then perform other operations based on the state of that data. It's deliberately written so as to be as agnostic as possible whether that data is in-memory collections, XML, database, etc. Ultimately it's always operating on some sort of in-memory object, with some means of converting between in-memory and the ultimate source, though some bindings go further than others in pushing some of the operations down to a different layer. E.g. calling .Count() can end up looking at a Count property, spinning through a collection and keeping a tally, sending a Count(*) query to a database or maybe something else.

ORMs provide a way to have in-memory objects and database rows reflect each other, with changes to one being reflected by changes to the other.

That fits nicely into the "some means of converting" bit above. Hence Linq2SQL, EF and Linq2NHibernate all fulfil both the ORM role and the Linq provider role.

Considering that Linq can work on collections you'd have to be pretty perverse to create an ORM that couldn't support Linq at all (you'd have to design your collections to not implement IEnumerable<T> and hence not work with foreach). More directly supporting it though means you can offer better support. At the very least it should make for more efficient queries. For example if an ORM gave us a means to get a Users object that reflected all rows in a users table, then we would always be able to do:

  int uID = (from u in Users where u.Username == "Alice" select u.ID).FirstOrDefault();

Without direct support for Linq by making Users implement IQueryable<User>, then this would become:

SELECT * FROM Users

Followed by:

while(dataReader.Read())
  yield return ConstructUser(dataReader);

Followed by:

foreach(var user in Users)
  if(user.Username == "Alice")
    return user.ID;
return 0;

Actually, it'd be just slightly worse than that. With direct support the SQL query produced would be:

SELECT TOP 1 id FROM Users WHERE username = 'Alice'

Then the C# becomes equivalent to

return dataReader.Read() ? dataReader.GetInt32(0) : 0;

It should be pretty clear how the greater built-in Linq support of a Linq provider should lead to better operation.

Linq is an in-language feature of C# and VB.NET and can also be used by any .NET language though not always with that same in-language syntax. As such, ever .NET developer should know it, and every C# and VB.NET developer should particularly know it (or they don't know C# or VB.NET) and that's the group NHibernate is designed to be used by, so they can depend on not needing to explain a whole bunch of operations by just implementing them the Linq way. Not supporting it in a .NET library that represents queryable data should be considered a lack of completeness at best; the whole point of an ORM is to make manipulating a database as close to non-DB related operations in the programming language in use, as possible. In .NET that means Linq supprt.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
2

Common sense?

There is a difference between an ORM like NHibernate and compiler integrated way to express queries which is use full in many more scenarios.

Or: Usage of LINQ (not LINQ to SQL etc. - the langauge, which is what you talk about though I am not sure you meant what you said) means you dont have to deal with Nhibernate special query syntax.

Or: Anyone NOT using LINQ - regardless of NHibernate or not - de qualifies without good explanation.

Surjit Samra
  • 4,614
  • 1
  • 26
  • 36
TomTom
  • 61,059
  • 10
  • 88
  • 148
1

You don't need it, but you might find it useful. Bear in mind that Linq, as others have said, is not the same thing as Linq to SQL. Where I work, we write our own SQL queries to retrieve data, but it's quite common for us to use Linq to manipulate that data in order to serve a specific need. For instance, you might have a data access method that allows you to retrieve all dogs owned by Dave:

new DogOwnerDal().GetForOwner(id);

If you're only interested in Dave's daschunds for one specific need, and performance isn't that much of an issue, you can use Linq to filter the response for all of Dave's dogs down to the specific data that you need:

new DogOwnerDal().GetForOwner(id).Where(d => d.Breed == DogBreeds.Daschund);

If performance was crucial, you might want to write a specific data access method to retrieve dogs by owner and breed, but in many cases the effort expended to create the new data access method doesn't increase efficiency enough to be worth doing.

In your example, you might want to use NHibernate to retrieve a lump of data, and then use Linq to break that data into lots of individual subsets for some form of processing. It might well be cheaper to get the data once and use Linq to split it up, instead of repeatedly interrogating the database for different mixtures of the same data.

Ade Stringer
  • 2,611
  • 17
  • 28