Project Description

This project is to see how I could achieve DML operations using Linq with a fluent type interface.

It has support for Update and Delete operations, this is exposed by extension methods applied to the Session.

please note this is not production code, more POC level

Overview - more information on how to use this.

Background

NHibernate is an awesome framework which supports set based commands out the box. However I could only see a HQL implementation

There is more information here in the online manual http://nhforge.org/doc/nh/en/index.html#batch-direct

Also Ayende wrote an article, which out lines the power of how NHibernate to generate the correct SQL http://ayende.com/blog/4037/nhibernate-executable-dml

I could not find a fluent DML project for NHibernate ( also I wanted to see what would be required to create one ).

Example usage

To update all Person objects where their name is James to have an age of 21

using HQL
int updatedEntities = session.CreateQuery( "update Person as x set x.Age = :p1 where x.FirstName = :p2" )
    .SetParameter( "p1", 21)
    .SetParameter( "p2", "James")
    .ExecuteUpdate();

using a Linq Func
int executeCommand = session.Update<Person>(x => x.FirstName == "James")
    .Set(x => x.Age, 123)
    .ExecuteCommand();

How it works

by taking the Func<T, bool>, the code visits the expression tree and converts it to the required HQL

for the above example (the update James to have an age of 21) the following HQL is produced:

update FluentDml.Tests.TestClasses.Person as x set x.Age = :p1 where (x.FirstName = :p2)

This is then executed against NHibernate as a HQL Query

Other Interesting resources

If you would like a Fluent DML library there is also FluentDML here https://github.com/jasondentler/FluentDML/wiki

During this exercise I found the following very helpful, (borrowing they code)

Linq Building an IQueryable Provider part 3 -
http://blogs.msdn.com/b/mattwar/archive/2007/08/01/linq-building-an-iqueryable-provider-part-iii.aspx

re-linq|ishing the Pain: Using re-linq to Implement a Powerful LINQ Provider on the Example of NHibernate
http://www.codeproject.com/KB/linq/relinqish_the_pain.aspx

Last edited Oct 1, 2011 at 1:42 PM by dbones, version 7