NHibernateWhy do we need to specify the query type twice?
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:
- (19 Nov 2010) Complex relationships
- (27 Jun 2010) Streaming large result sets
- (25 May 2009) Why do we need to specify the query type twice?
- (20 Mar 2009) Avoid identity generator when possible
- (26 Mar 2007) Nullable DateTime Issues
- (08 Jan 2007) Fetching multiply collections in one roundtrip
Comments
This could be "fixed" by using strongly typed criteria (with lambdas and stuff) in 80% of cases.
duh.
It is also useful in polymorphic situations.
Well, why not Criteria <t with SetProjection changing T?
Well, why not Criteria<T> with SetProjection changing T?
why not have 2 implementation of the List() function. One that is function type parameterized and one that is class type parameterized?
<t {
<t ();
**** () {
code got mangled, here it is again:
public class Foo <t {
public T List() {
<t();
}
public B List ****() {
}
}
new API?
var blogs = s.CreateCriteria <blog()
var blogs = s.CreateProjection <myvalueobject()
<blog()
I know, I know... patch :)
the angle brakcets where lost with the last post.
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();
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.
It should have been s.CreateCriteria<Blog>().. The blog swallowed the tag.
@Dmitry
http://code.google.com/p/nhlambdaextensions/
www.lostechies.com/.../...nh-contrib-is-alive.aspx
Comment preview