The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,901,384
since: 19 Jan 2005

ADO.NET Data Services Projections Makes Sliced Bread Jealous

posted Fri 09 Oct 09

The other day I ran into a blog entry from the Astoria team discussing the projections feature of the 1.5 CTP2 version of the product. If you're not familiar with ADO.NET Data Services (formerly codenamed Astoria), it's basically a layer that you can put on top of an Entity Data Model and it will expose that model as a RESTful service. The URL format for this RESTful service is quite flexible, allowing you to select individual rows, perform filters, sorts, and many other things.

One of the new things that you can now do server-side via the new CTP2 URL syntax is projections. Projections actually allow you to control the shape of the output coming back. You can specifically choose which properties on the entity you want. Even more awesome is that this can be controller hierarchically. So if you bring back an Order entity and you include all of the OrderItem entities for that order, you can tell the server that you only want the customer for the Order and you only want Quantity and Price for the order items.

To perform a projection on the URL, you just use the $select parameter, like this:

blah.svc/Orders?$select=OrderID,Quantity,Price

And to control the shape of hierarchical data:

blah.svc/Orders?$select=OrderID,Quantity,OrderItems/Price,OrderItems/Quantity&$expand=OrderItems

At this point when I saw this I started having convulsions of pure joy. The main reasons being that every ADO.NET Data Services URL query will output either AtomPub or JSON. This means I can get only the columns I need and give them to my Ajax calls. Then I noticed that support for the new projections is actually in the Astoria client library as well, allowing me to write the following query:

var q = from order in ctx.Orders
        where order.Price > 300
        orderby order.Price descending
        select new { Price = order.Price, Quantity = order.Quantity };

This will translate into an Astoria query that filters, sorts, AND projects all on the server side, leaving me with a network footprint that only transmits the information I want and nothing else. This is a godsend if you have entities with huge amounts of columns but each individual query might only need to use 1 or 2 of those columns at a time.

And now, if this wasn't ridiculous enough, you can actually perform updates using the projected objects, AND those updates will ONLY transmit the information necessary. For example, if I only want to change an order's price and I got the price from a projection, I don't need to carry the entire order payload across the wire in order to commit the change:

order = q.First();
order.Quantity = order.Quantity + 42;
svc.UpdateObject(order);
svc.SaveChanges();

This will figure out that the only thing changing is the Quantity field and it will ONLY send that information. After discovering the combination of projections and the efficient round-trips of Astoria, this is when my head exploded.

If you have a multi-tier scenario and you're using an Entity Data Model (EDM), then you should definitely look into using ADO.NET Data Services to expose that model via services because now with projections, you can really do some unbelievable stuff. All that garbage code you used to have to generate to convert between DTOs and ViewModels and Entities and back again? You can delete ALL of that crap.

I've said it before but the folks working on ADO.NET Data Services deserve a medal. If you see a member of that team, buy them a beer. You have no idea how ridiculously complicated and difficult it is to write code that supports arbitrary projections like this. I'm pretty sure every time you run an Astoria query with projections, an Angel gets its wings.

tags:            

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Christian Weyer left...
Sat 10 Oct 09 1:42 pm :: http://blogs.thinktecture.com/cweyer/

Hi Kevin,

interesting piece of yours and feature of Astoria v.Next... hm... thinking about it for some minutes now... One of the sentences in your blog post I had to read more than twice is this one:

"If you have a multi-tier scenario and you're using an Entity Data Model (EDM), then you should definitely look into using ADO.NET Data Services to expose that model via services because now with projections, you can really do some unbelievable stuff. All that garbage code you used to have to generate to convert between DTOs and ViewModels and Entities and back again? You can delete ALL of that crap."

Well, call me old-school. Call me stubborn. OK, call me idiot. But why in the world would I want to let the calling/client application let decide what data I (as a servivce) am going to provide to the client? This seems to defeat the original sense of services. I see a service as a modeling artefact that gives me explicit boundaries and shielded access to a business functionality. Oh wait! We are not talking about business services here, right? You are rather referring to data services, raw data pumps, correct? Because I can see only very few (to no) reasons why I would want to directly expose my EDM directly other than in the typical data services cases (maybe this is the reason they dubbed Astoria 'Data Services', eh?). And I can already see the overuse and misuse of data services, sigh...

Bash me, c'mon on :)

Cheers, -Christian


2. Kevin Hoffman left...
Sat 10 Oct 09 3:28 pm

I am referring to data services (hence the name of the product, ADO.NET Data Services). You can use interceptions to control business logic to filter and prevent people from accessing data that they should not be accessing. In addition, you can also choose to have some of the entity sets visible, some invisible, some read-only, some write-only, etc. So, this is a little bit more than a raw data pump - otherwise we'd just be hitting SQL server directly. This is more like a data pump with validation and a thin veneer of business rules on it.

An ADO.NET Data Service is NOT a business service when you're simply exposing an underlying EDM. HOWEVER - if you take it a step further and you slap an ADO.NET Data Service on top of your own business model and do much tighter integration with the data service itself, then you can get closer to what you're calling a business service. I'm not going to flame you because your points are all valid - ADO.NET Data Services is designed specifically for exposing -SOME- model as a raw pump. That model could be a near 1:1 map of the database tables, in which case you're just doing a "dumb" data service. On the other hand, that model could be an abstraction layer for your entire suite of business logic (think about taking your existing business model and wrapping a data service around the top level hierarchy). So, to make a long story short - you can make your data service as raw or as business-y as you choose, it all depends on your specific needs.


3. Christian Weyer left...
Sat 10 Oct 09 4:35 pm :: http://blogs.thinktecture.com/cweyer/

OK, seems we agree then. BTW: did you ever try to wrap Astoria around your business model? Yikes... IUpdateable... too much work in v1, IMO.


Tag Related Posts

SSDS loses an "S" and gains some awesome

Wed 11 Mar 09 11:42 A GMT-05
tags:              

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

ASP.NET 3.5 Extensions Preview Released

Mon 10 Dec 07 2:10 P GMT-05

Astoria and the Semantic Web

Mon 16 Jul 07 3:47 P GMT-05

Silverlight and Astoria - First Impressions

Mon 04 Jun 07 1:40 A GMT-05
tags:    

ASP.NET vs Ruby on Rails : Round 2 (Agility)

Thu 05 Oct 06 11:02 A GMT-05
tags:                      

ASP.NET vs Ruby on Rails : Round 1

Wed 04 Oct 06 1:37 P GMT-05
tags:                

ADO.NET Entity Framework Announced Today!

Wed 16 Aug 06 11:08 A GMT-05

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags: