Ayende @ Rahien

It's a girl

NHibernate: Why do we need to specify the query type twice?

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>();

Comments

Krzysztof Kozmic
05/25/2009 07:16 PM by
Krzysztof Kozmic

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

JW
05/25/2009 07:24 PM by
JW

duh.

Shawn Wildermuth
05/25/2009 10:46 PM by
Shawn Wildermuth

It is also useful in polymorphic situations.

Andrey Shchekin
05/25/2009 11:41 PM by
Andrey Shchekin

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

Andrey Shchekin
05/25/2009 11:41 PM by
Andrey Shchekin

Well, why not Criteria with SetProjection changing T?

Josh N
05/26/2009 03:21 AM by
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
05/26/2009 03:22 AM by
Josh N

code got mangled, here it is again:

public class Foo <t {

public T List() {

return List

<t();

}

public B List ****() {

...

}

}

Jason Meckley
05/26/2009 02:53 PM by
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
05/26/2009 02:57 PM by
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
05/26/2009 04:30 PM by
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
05/26/2009 04:32 PM by
Dmitry

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

Comments have been closed on this topic.