These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Microsoft LINQ + NHibernate
PostPosted: Mon Apr 17, 2006 7:50 pm 
Newbie

Joined: Fri Mar 24, 2006 3:45 pm
Posts: 18
I would like to submit this idea to the NHibernate community:

LINQ (Language INtegrated Query) is a feature that will be available in the next version of C# and VB (to be released in 2007). Basically it allows the programmer to write static typed, intellisense enabled, compiler checked queries in C# or VB.

Code:
var expr = from s in names
                where s.Length == 5
                orderby s
                select s.ToUpper();


Info about LINQ:

The homepage: http://msdn.microsoft.com/netframework/future/linq/
The forum: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=123&SiteID=1

Internally the queries are represented as an expression tree, used by Microsoft to create, for example DLINQ (Database LINQ), an ORM that translates the expression tree into SQL code. I've evaluated the Community Technical Preview of LINQ and DLINQ and found that:

1. DLINQ is (like all Microsoft products) focused on mapping or generating (poorly designed) classes to an existing database (database oriented), and not mapping a database to a good fine grained domain model (like NHibernate does). This I consider is the main problem with DLINQ.
- DLINQ does not (yet?) support all inheritance mappings. The forum says that the next preview will have support for "one table per class hierarchy".
- DLINQ still misses many other things already in Nhibernate that make it the best ORM for rich object oriented domain models available (in .NET).

SO:

We could start a sourceforge project called "NHiberLINQ", using LINQ expression trees to generate HQL.

Expected benefits:
- type safe queries with intellisense!
- all the power of NHibernate
- If you need to further process a collection of objects already fetched from the database, we could use LINQ to filter, sort, page or project the collection.
- In collections inside classes: if collection is initialized it could use in memory LINQ query functions to execute code in memory and avoid writing common code, if not initialized NHiberLINQ would create the HQL and use internally Session.CreateFilter("hql expression") to execute the query)

for example consider this (sorry C# guys):

Code:
Class MegaProject

...
private _projects as EntitySet(of Project) ' Using Ayende's generics
...

Function GetProjectsWithBudgetBetween(hi as decimal, lo as decimal) as ICollection(of Project)
   Dim innerSet As ISet = ReflectionHelper.GetInnerSet(_projects)
   If Not NHibernateUtil.IsInitialized(innerSet) Then
      ' Do it from database
      Dim query As IQuery = NHibernateHelper.GetCurrentSession. _
           CreateFilter(innerSet, "where this.Budget between :lo and :hi order by this.Budget"). _
           SetDecimal("lo", lo). _
           SetDecimal("hi", hi)
      Return Algorithms.TypedAs(Of Project)(query.List())
   Else
      ' Do it in memory for speed (boilerplate code I write all the time)
      Dim result As New List(Of Project)
      For Each aProject As Project In _projects
         If aProject.Budget >= lo And aProject.Budget <= hi Then
            result.Add(aProject)
         End If
      Next
      Return result
   End If
End Function


would turn into not writing the code until needed by the client:

Code:
VB 9.0:
dim result = Select p _
                   From myMegaProject.Projects As p _
                   Where p.Budget >= 20000 and p.Budget <= 40000

C# 3.0:
var result = From p In myMegaProject.Projects
                  Where p.Budget >= 20000 and p.Budget <= 40000
                  Select p;


First steps:

- Create the sourceforge project
- Download the latest LINQ CPT
- Dissasembly the DLINQ code with Reflector to see how it works, as I've seen its allot like how the NHibernate ICriteria API works.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 3:11 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
plus added benefit that it would enable dlinq programmers to switch to nh when they find out that dlinq is not powerful enough.

Seems like a great idea although I bet the nh/h-experts will be able to tell you that the hql language is more expressive then linq.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 5:29 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
LINQ is actually more like compiler-supported criteria queries than like HQL. This is something that I have been thinking about and will do in the future, so I fully support this idea.

hectorjcruz, if you are serious about starting this project, I'm willing to give you commit rights to the SVN repository and create a LINQ-branch so that you can work directly on NH code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 10:18 am 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
Disassembling with Reflector will get you in big trouble - ask the Mono developers trying to re-create WinForms and ASP.NET for Mono. Any code you write after viewing disassembled software violates ... something (not a lawyer).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 11:10 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I've created my own "HQL Query Analyzer" that supports some Intellisense (auto-complete textbox "dropdown" of mapped entities appears at appropriate times after FROM and JOIN keywords; similar auto-complete dropdown of mapped properties appears after typing a '.' in the SELECT, JOIN, WHERE, HAVING and ORDER BY clauses). It makes a huge difference when writing queries. I'm also working on a IComponent wrapper for NHibernate.IQuery and an NHibernateObjectDataSource with a corresponding ObjectDataSourceDesigner that brings up this "HQL Query Analyzer" from within Visual Studio. The goal is to provide RAD for HQL-driven web pages.

With tools like these, why bother with trying to integrate NHibernate with DLINQ? Admittedly, my implementations are incomplete and not clean enough to go into NHibernate Contrib (yet), but as a community we should work on this stuff together. If they are decent, it should minimize DLINQ's allure ...


Last edited by Nels_P_Olsen on Tue Apr 18, 2006 3:20 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 1:29 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Whoa...I want to see that Query Analyzer! ;)

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 3:05 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Send me an email and I'll get you a "beta". I'd be interested in getting feedback on it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 3:22 pm 
Newbie

Joined: Fri Mar 24, 2006 3:45 pm
Posts: 18
Reply to all:

Thanks for your interest. I think MS guys need a brain transplant and a lot of work to make DLINQ something like NHibernate (support rich fine grained object oriented domain models, instead of DB table oriented stuff).

Reply to TheShark:

Quote:
hql language is more expressive then linq


Maybe, but to be shure I'll start a proof of concept test to see if all HQL queries in chapter 11 of NHibernate documentation can be written as LINQ query comprehensions. I'll post the result here when I'm done.

Reply to Sergey:

First let me do some tests, I wan't to have a more clear idea of how it can be done. After that, when I have some code to start with I will gratefully accept your offer.

Reply to k-dub:

Thanks for the warning, but I've already tried Reflector. DLINQ code is very complicated to go that way. So I'm going to start translating expression trees from lambda expressions that represent filterings, projections, orderings and groupings to HQL.

Reply to Nels_P_Olsen:

I think you got the idea a little wrong. The idea is not trying to
Quote:
... integrate NHibernate to DLINQ

but to use LINQ (not DLINQ) to create type safe queries that translate to HQL. DLINQ => SQL, NHiberLINQ => HQL. The results would be similar to what you are doing in your HQL Query Analyzer but more "Language Integrated". The most interesting part you're doing is the NHibernateObjectDataSource, I also would like to see it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 1:42 pm 
Newbie

Joined: Fri Mar 24, 2006 3:45 pm
Posts: 18
Updates on NHLinq project:

What do you think about the name? :-)

I've succesfully implemented some code to translate a few easy LINQ queries with select and where clauses to HQL. I'm now working to extract the parameters in the original query to insert them in the IQuery with SetParameter(i,obj) to execute the queries.

I've noted an added benefit: in DLINQ, Table<T> acts like a generic DAO, and NHLinq needs a NHTable<T> for LINQ queries to work, so everybody would get automatic typed DAO's for each entity type.

Another thought: NHLinq potentially could be better than DLINQ, supporting most .NET Standard Query Operators, including some than DLINQ doesn't, for example, the SelectMany operator; but maybe new operators would be needed to support all HQL features.

Hector Cruz.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 4:39 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
hectorjcruz: what about translating to criteria queries instead of HQL? Can you list things that you miss to be able to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 8:40 pm 
Newbie

Joined: Fri Mar 24, 2006 3:45 pm
Posts: 18
Sergey:

LINQ supports very well projection and grouping (aggregation is still available only for one property), so I chose HQL instead of ICriteria, to support all the power of LINQ and hopefully all cababilities of HQL. NHibernate ICriteria first needs to updated to Hibernate 3.0 features (adding projections, aggregation and grouping).

Hector


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 24, 2006 7:19 am 
Newbie

Joined: Sat Jun 24, 2006 4:58 am
Posts: 1
Hi Hector!

Are there any updates you can give us on the status of the project? I would be interested. ;)

Cheers,
Tobi


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 24, 2006 11:35 pm 
Newbie

Joined: Wed Jun 21, 2006 2:45 pm
Posts: 16
Great stuff!

I really like NHibernate, and I'm curious about LINQ, so to see this is just great!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 25, 2006 4:53 pm 
Newbie

Joined: Wed Jan 04, 2006 2:29 pm
Posts: 8
Am I wrong to interpret this video http://channel9.msdn.com/Showpost.aspx?postid=202138 and to say that IQueryable is the interface to target in order to integrate hibernate and LINQ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 6:52 am 
Newbie

Joined: Wed Nov 08, 2006 6:17 am
Posts: 5
Ok, this is yet another really interesting topic that bleeded to dead :) Is there any mentionworthy update regarding NHiberLINQ, or was it just a nice concept?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.