Bringing it all together: Even More Magic,

So, I had done a couple of fairly cool stuff recently, but it is all in seperated pieces, let us try to bring them together and see what we get, shall we?

ProjectionQuery<User> query =

       new ProjectionQuery<User>(Where.User.Name.Like("Ayende", MatchMode.Start),

                                 ProjectBy.User.Name && ProjectBy.User.Email);

foreach (object[] objects in query.Execute())

{

       Console.WriteLine("Name: {0}, Email: {1}", objects[0], objects[1]);

}

That is nice, isn't it? Again, we can use the strongly typed version to be type safe and avoid the use of arrays as DTO:

ProjectionQuery<User, NameAndEmail> query2 =new ProjectionQuery<User, NameAndEmail>(

       Where.User.Name.Like("Ayende", MatchMode.Start),

       ProjectBy.User.Name && ProjectBy.User.Email);

foreach (NameAndEmail nameAndEmail in query2.Execute())

{

       Console.WriteLine("Name: {0}, Email: {1}", nameAndEmail.Name, nameAndEmail.Email);

}

And yes, this is filed under Linq as well.

Print | posted on Monday, October 30, 2006 7:11 AM

Feedback


Gravatar

#  10/30/2006 1:41 PM dru

Will Anonymous Types give us a strongly typed DTO?


Gravatar

#  10/30/2006 10:13 PM Adam Tybor

You can get strongly typed DTO's with C# 2.0, it does not require anonymous types. However, I am sure it can be adapted to use anonymous types....

The only advantage of anonymous types is the ability to get a strongly typed dto without defining it. Pretty cool, but could open a can of worms. Instead define your DTO and the code from above uses an NHibernate transformation behind the scense to transform the result set of object[][] to your DTO. Now you have an easy to create, strongly typed, well documented DTO to use in views throughout your application.


Gravatar

#  10/30/2006 10:48 PM Ayende Rahien

@Dru,
I haven't looked at the C# 3.0 spec well enough to say, but that is not going to be the approach that I will take when the time comes to create Linq for NHibernate.
Anonymous types are a curiousity to me, since I can't figure out why you would want a &quot;type&quot; that can only be used in a single method. My maintainability level is usually a class, not a method, so I don't see a problem with a mehtod using untyped result set, or indexing into it.


Gravatar

#  10/31/2006 7:47 PM Bas

Ayende,

Is this projection extension working with your Repository&lt;T&gt; class?
Can you give me an advice? I'm working on a project and use your Repository class a lot. Would it be wise to switch to Active Records?

Bas


Gravatar

#  11/1/2006 1:40 AM Ayende Rahien

The shoemaker walks barefoot, know that saying?
Repository&lt;T&gt; doesn't support it, since I currently do not use it. The minute I will, it will support it.
I wouldn't recommend to switch to AR just because of this.
Adding support for this is really a very simple matter, I just don't have the time for it.


Gravatar

#  11/1/2006 9:25 AM Bas

I know the saying only in Dutch it is the shoemakers son who walks barefooted.
Actually I just wanted to know when to switch/use to AR. What are pro's cons?
Anyway I better start by checking the AR website and read some.

Bas

Comments have been closed on this topic.