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: 17 | Comments: 44

filter by tags archive

NHibernateWhy do we need to specify the query type twice?

time to read 1 min | 171 words

Why do we have to do something like that?
var blogs = s.CreateCriteria<Blog>()
    .Add(Restrictions.Eq("Title", "Ayende @ Rahien"))
    .List<Blog>();

We have to specify Blog twice, isn’t that redundant?

Yes and no. The problem is that we can’t assume that we are going to return instances of Blog. For example:

var blogs = s.CreateCriteria<Blog>()
    .Add(Restrictions.Eq("Title", "Ayende @ Rahien"))
    .SetProjection(Projections.Id())
    .List<int>();

More posts in "NHibernate" series:

  1. (19 Nov 2010) Complex relationships
  2. (27 Jun 2010) Streaming large result sets
  3. (25 May 2009) Why do we need to specify the query type twice?
  4. (20 Mar 2009) Avoid identity generator when possible
  5. (26 Mar 2007) Nullable DateTime Issues
  6. (08 Jan 2007) Fetching multiply collections in one roundtrip

Comments

Krzysztof Kozmic

This could be "fixed" by using strongly typed criteria (with lambdas and stuff) in 80% of cases.

JW
JW

duh.

Shawn Wildermuth

It is also useful in polymorphic situations.

Andrey Shchekin

Well, why not Criteria <t with SetProjection changing T?

Andrey Shchekin

Well, why not Criteria with SetProjection changing T?

Josh N

why not have 2 implementation of the List() function. One that is function type parameterized and one that is class type parameterized?

public class Foo

<t {

  public T List() {

    return List

<t ();

  }

  public B List

**** () {

    ...

  }

}
Josh N

code got mangled, here it is again:

public class Foo <t {

public T List() {

return List

<t();

}

public B List ****() {

...

}

}

Jason Meckley

new API?

var blogs = s.CreateCriteria <blog()

.Add(Restrictions.Eq("Title", "Ayende @ Rahien"))

.List();

var blogs = s.CreateProjection <myvalueobject()

.StartingWith

<blog()

.Add(Restrictions.Eq("Title", "Ayende @ Rahien"))

.List();

I know, I know... patch :)

Jason Meckley

the angle brakcets where lost with the last post.

var blogs = s.CreateCriteria()

.Add(Restrictions.Eq("Title", "Ayende @ Rahien"))

.List();

var blogs = s.CreateProjection()

.StartingWith()

.Add(Restrictions.Eq("Title", "Ayende @ Rahien"))

.List();

Dmitry

Why not have overloads like this?

var blogs = s.CreateCriteria <blog().AddRestriction(b => b.Title == "Ayende @ Rahien").ToList();

var blogs = s.CreateCriteria <blog().AddRestriction(b => b.Title == "Ayende @ Rahien").Project(b => b.Id).ToList();

This way LINQ provider would not be as imporant.

Dmitry

It should have been s.CreateCriteria().. The blog swallowed the tag.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB 3.0 New Stable Release - 18 hours from now
  2. Production postmortem: The case of the lying configuration file - about one day from now
  3. Production postmortem: The industry at large - 3 days from now
  4. The insidious cost of allocations - 4 days from now
  5. Buffer allocation strategies: A possible solution - 7 days from now

And 4 more posts are pending...

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    31 Aug 2015 - The case of the memory eater and high load
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats