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.


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

Also Ayende wrote an article, which out lines the power of how NHibernate to generate the correct SQL

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")

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

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

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

Linq Building an IQueryable Provider part 3 -

re-linq|ishing the Pain: Using re-linq to Implement a Powerful LINQ Provider on the Example of NHibernate

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