Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 6,124 | Comments: 45,482

filter by tags archive

Underused NHibernate Feature: Creating Objects In The Select Clause

time to read 9 min | 1716 words

I got a comment today from Jim Bolla asking about this query:

select new Address(c.Address, c.City, C.Zip) from Customer c

His comment was:

I've been using NHibernate for at least 6 months now and haven't heard of this feature. Does this really work? What are the requirements/limitations? Is this in the documentation? Or even in the example code/tests?

As he didn't leave an email or another way to contant him, I'm going to broadcast the reply to the world, hoping it would get back to him. (As an aside, if you ask a question, I need a way to get back to you, so leave a way to do so).

First, it is a documented feature that you're welcome to use. It's not used much (and sadly I couldn't find a test for it after a quick grepping), mainly because it is obscure. I just gave it a quick test, and it is working correctly. One word of caution. you need ot make sure that you imported the type that you are trying to create. Here is the sample test:

[TestFixture]

public class QueryThatCreatesAnObject

{  

    [Test]

    public void CheckCreatingObjectsInSelectClause()

    {

        ISessionFactory factory = DatabaseTests.CreateSessionFactory();

        ISession session = factory.OpenSession();

   

        session.Delete("from Blog");

        session.Flush();

 

        Blog blog = new Blog("ayende @ blog");

        session.Save(blog);

        session.Flush();

 

        IList list = session.Find(@"select

                    new System.Collections.DictionaryEntry(b.BlogID, b.BlogName)

                    from Blog b");

        Assert.AreEqual(1, list.Count);

 

        DictionaryEntry de = (DictionaryEntry)list[0];

        Assert.AreEqual(de.Key, blog.BlogID);

        Assert.AreEqual(de.Value, blog.BlogName);

    }

}

As you can see, it's pretty slick. I'm not 100% about how you import a class, and I don't have the time to investigate, but the documentation for that is here.


Comments

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB 3.5 whirl wind tour: I’ll find who is taking my I/O bandwidth and they SHALL pay - 11 hours from now
  2. The design of RavenDB 4.0: Physically segregating collections - about one day from now
  3. RavenDB 3.5 Whirlwind tour: I need to be free to explore my data - 2 days from now
  4. RavenDB 3.5 whirl wind tour: I'll have the 3+1 goodies to go, please - 5 days from now
  5. The design of RavenDB 4.0: Voron has a one track mind - 6 days from now

And 12 more posts are pending...

There are posts all the way to May 30, 2016

RECENT SERIES

  1. RavenDB 3.5 whirl wind tour (14):
    02 May 2016 - You want all the data, you can’t handle all the data
  2. The design of RavenDB 4.0 (13):
    03 May 2016 - Making Lucene reliable
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats