Galin Iliev's blog

Software Architecture & Development

Using Microsoft ADO.NET Data Services

Mike Flasko (ADO.NET Data Services, Program Manager) at Microsoft Corp. wrote an extensive article about recently released ADO.NET Data Services called Using Microsoft ADO.NET Data Services. It is full of examples and it is exactly the type of articles developers prefer to read :) - although slightly long.

The examples included are:

  • Example 1: Basic data service in C#
  • Example 2: ADO.NET Data Service exposing an in-memory data source
  • Example 3: Response for the root of a data service
  • Example 4: Listing of the contents of an entity set, in Atom/APP format
  • Example 5: Response for a single-entity URL
  • Example 6: A single-entity response from the data service
  • Example 7: A response that contains multiple entities
  • Example 8: Response with nested related entities using the "expand" option
  • Example 9: Atom service document as returned from an ADO.NET Data Service
  • Example 10: JSON response from a data service for a single 'Customer' entity
  • Example 11: A hierarchical result containing a Customer and its related Sales Orders, in JSON format
  • Example 12: Payloads for creating a new Category entity using Atom and JSON
  • Example 13: Response from the data service after processing a POST request for creating a Category, in Atom and JSON formats
  • Example 14: Payload used to modify an existing Category entity through an HTTP PUT request, Atom and JSON formats
  • Example 15: Payload used to modify an existing Category entity through an HTTP PUT request, Atom and JSON formats
  • Example 16: Payload to create a new Territory entity that includes an association to a Region entity
  • Example 17: Payload to update a Territory so it is associated with a different Region entity
  • Example 18: Keys-only payload format used for inserting an association
  • Example 19: Inserting a graph of data in a single request
  • Example 20: Request and Response using CategoryName as a concurrency token
  • Example 21: Request to update the name of a Category
  • Example 22: A data service operation to retrieve filtered customers
  • Example 23: Setting Visibility of Service Operations
  • Example 24: Access an Astoria data service from a .NET application using the client library
  • Example 25: Retrieving all customers in the city of London, ordered by company name
  • Example 26: Delay-loading related entities using the Load() method
  • Example 27: Using "expand" to eagerly-load related entities
  • Example 28: Inserting a new entity instance using the client library
  • Example 29: Updating an existing entity using the client library
  • Example 30: Creating a product entity and associate it with a Category
  • Example 31: Sending queries as a batch request
  • Example 32: Using the asynchronous API in the client library
  • Example 33: Setting service-wide access policy
  • Example 34: Query interceptor method implementing a custom, per request access policy
  • Example 35: Update interceptor method that validates input Category entities before being persisted in the underlying store
  • Example 36: Assume a validation error occurred while processing a request which caused an ArgumentException to be thrown invoking the exception handler shown in the ‘service code’ section.

Pretty long list, isn't it?!

For more details and code samples read article Using Microsoft ADO.NET Data Services on MSDN.

Visual Studio 2008 SP1 is here

Well. The wait is over. Visual Studio 2008 SP1 is here.

Visual Studio 2008 SP1 delivers:

  • Improved WPF designers
  • SQL Server 2008 support
  • ADO.NET Entity Designer
  • Visual Basic and Visual C++ components and tools (including an MFC-based Office 2007 style ‘Ribbon’)
  • Visual Studio Team System Team Foundation Server (TFS) addresses customer feedback on version control usability and performance, email integration with work item tracking and full support for hosting on SQL Server 2008
  • Richer JavaScript support, enhanced AJAX and data tools, and Web site deployment improvements

The .NET Framework 3.5 SP1 delivers:

  • Performance increases between 20-45% for WPF-based applications – without having to change any code
  • WCF improvements that give developers more control over the way they access data and services
  • Streamlined installation experience for client applications
  • Improvements in the area of data platform, such as the ADO.NET Entity Framework, ADO.NET Data Services and support for SQL Server 2008’s new features

and more... Read more on what's included in VS 2008 SP1.

Download install .exe.
Download .iso version.

VS2008 seminars in New Horizons Bulgaria

Yesterday was last seminar from VS2008 series held in the New Horizons Bulgaria office with Microsoft Bulgaria support.

The seminars was very interesting (not only from my perspective of trainer) but also from audience perspective we see in their feedback. During high demand I am publishing presentations and demo scripts where available. It is always good to write code in live (although not very easy - try it ;) ) and this is why I cannot provide working demos - I have only my own cheat lists which I use in cse I am stuck somewhere.

Deep Dive in LINQ - Here I talked about new features in C# in details and how they are build internally(also described in my Introduction in LINQ and C# 3.0 (In Bulgarian) ). Also I covered LINQ to SQL, LINQ to XML.  In demos we took a look at C# syntax sugar, new ways to work with XML as well as some problems stated in Project Euler and solved with C# 3.0. Slides + Demo scripts (PPTX+DOCX - 1.22MB)

Develop Dynamic Web Sites with ASP.NET 3.5 - This session was focused on web development. Here I show new controls to work with LINQ to SQL data source declaratively. We took a look at ASP.NET Extensions (aka Futures): ASP.NET MVC, ASP.NET Dynamic Data, ASP.NET AJAX Integration, ADO.NET Data Services. All features was shown in code except ADO.NET Data Services. For ASP.NET AJAX was shown how to control Browser's Back Button from both server-side and client-side. Slides+Demo Scripts (PPTX+DOCX - 3.23 MB).

Overview of WCF, WF, WPF - Although these technologies are not new there is still some improvements in .NET 3.5. These components are very useful but their adoption is not very fast. We talked about the architectural decisions and challenges behind them. The demos show how to work with WCF in VS2008, How to create REST Service and how to expose JSON as result. WF demos show how to create simple sequential workflow. WPF demo presented project structure, generated code, XAML (of course) and WPF data binding basics. Slides+Demo Scripts (PPTX+DOCS - 10.8 MB).

Hope you'll find it useful.
As always any comments and feedback are very welcome.

Visual Studio 2008 and .NET Framework 3.5 Service Pack 1 Beta

There is no doubt that VS 2008 and .NET 3.5  totally rocks! ScottGu's division keeps pushing these products and constantly improving developer's productivity and shortening development cycle.

This time MS is preparing to release .NET 3.5 SP1 and VS 2008 SP1 releases.

In short here are improvements:

Improvements for Client Development
  • ASP.NET Data Scaffolding Support (ASP.NET Dynamic Data)
  • SP.NET Routing Engine (System.Web.Routing)
  • ASP.NET AJAX Back/Forward Button History Support
  • ASP.NET AJAX Script Combining Support - Omar Al Zabir wrote an extensive article about this approach.
  • Visual Studio 2008 Performance Improvements HTML Designer and HTML Source Editor
  • Visual Studio 2008 JavaScript Script Formatting and Code Preferences
  • Better Visual Studio Javascript Intellisense for Multiple Javascript/AJAX Frameworks - who can blame MS that force us to use their JS framework now?!
  • Visual Studio Refactoring Support for WCF Services in ASP.NET Projects
  • Visual Studio Support for Classic ASP Intellisense and Debugging - I am wondering when this technology will be declared dead :) (This is what I used in my first web apps too :))
Improvements for Client Development
  • Application Startup and Working Set Performance Improvements
  • New .NET Framework Client Profile Setup Package
  • New .NET Framework Setup Bootstrapper for Client Applications
  • ClickOnce Client Application Deployment Improvements
  • Windows Forms Controls
  • WPF Performance Improvements
  • WPF Data Improvements
  • WPF Extensible Shader Effects
  • WPF Interoperability with Direct3D
VS 2008 for WPF Improvements
  • Several performance improvements
  • Events tab support within the property browser
  • Ability to sort properties alphabetically in the property browser
  • Margin snaplines which makes form layout much quicker
  • Better designer support for TabControl, Expander, and Grid
  • Code initiated refactoring now updates your XAML (including both control declarations and event declarations in XAML)
  • Go to Definition and Find All References now support things declared in XAML
Data Development Improvements
  • SQL 2008 Support
  • ADO.NET Entity Framework and LINQ to Entities
  • ADO.NET Data Services
WCF Development Improvements
  • Significant scalability improvements (5-10x) in Web-hosted application scenarios
  • Support for using ADO.NET Entity Framework entities in WCF contracts
  • API usability improvements with DataContract Serializers, and with the UriTemplate and WCF web programming models
  • Enhanced TestClient support within VS 2008 SP1
  • New Hosting Wizard in VS 2008 SP1 for WCF Service Projects
  • Improved debugging support in partial trust scenarios
VB and C# Improvements !!!
Team Foundation Server Improvements

Pretty impressive...

Read full novel by Scott Guthrie here :)

Release package is settled: Entity Framework & ADO.NET Data Services in VS 2008 SP1 and .NET 3.5 SP1

It's settled! The Entity Framework (and the Entity Designer) along with ADO.NET Data Services will RTM as part of the Visual Studio 2008 and .NET 3.5 SP1 releases!

Unfortunately, we don't have official release dates at this point, but stay tuned. You'll also want to keep an eye out for the upcoming SP1 Beta 1, which will be your next chance to check out updated bits for both of these products.

Elisa Flasko
Program Manager, Data Programmability

(via this ADO.NET team blog post)

ADO.NET Data Services Framework samples

As project codename Astoria has new name "ADO.NET Data Services Framework " and it is part of ASP.NET 3.5 a.k.a ASP.NET futures it has its place on asp.net site.

There are plenty of samples and descriptions what is it and how to use from client and server site.

If you struggle what is REST and how it is different than well known web services  take a look at this web presentation: Reconciling Web Services and REST Services.

ADO.NET Data Services CTP is released!

ADO.NET Data Services aka Project "Astoria" December CTP is released. Mike Flasko (PM @ Astora team) posted some key points:

The following features are in this CTP:

  • Support to create ADO.NET Data Services backed by:
    • A relational database by leveraging the Entity Framework
    • Any data source (file, web service, custom store, application logic layer, etc)
  • Serialization Formats:
    • Industry standard AtomPub serialization
    • JSON serialization
  • Business Logic & Validation
    • Insert custom business/validation logic into the Request/response processing pipeline
    • simple infrastructure to build custom access policy 
  • Access Control
    • Easily control the resources viewable from a data service
  • Simple HTTP interface
    • Any platform with an HTTP stack can easily consume a data service
    • Designed to leverage HTTP semantics and infrastructure already deployed at large
  • Client libraries:
    • .NET Framework
    • ASP.NET AJAX
    • Silverlight (coming soon)
  • For more information see ADO.NET Data Services official site.

    How to insert record to DB using Javascript and Astoria Service

    I wrote several posts regarding Astoria Services and in this one I will demonstrate how to insert reocord in database using AJAX calls.

    Read my previous post - How to consume Astoria Service - in order to get deeper into solution environment.

    I extended Javascript code so there is one more function that add new record to database. As it is for demostration purposes excuse hard-coded values, please.

    So in order to add new record you need:

    • to create and instanciate variable with appropriate JSON format. Be careful here! All required fields from DB must present and have valid values.
    • set HTTP POST verb to the request.
      wRequest.set_httpVerb("POST");
    • set URL that point to root of entity type (Customer in our case)
    • set Request's headers "Accept" AND "Content-Type" to "application/json"
      wRequest.get_headers()["Accept"] = "application/json";
      wRequest.get_headers()["Content-Type"] = "application/json";
    • set request body's content with serialized variable content.
      wRequest.set_body(Sys.Serialization.JavaScriptSerializer.serialize(customer));

    and finally call Invoke() method.

    Here is full function:

       1:  function createNewCustomer(custID, contactName, companyName)
       2:  {
       3:      //create new item
       4:      var customer = {__metadata: {Type:"Customer" },
       5:          CustomerID:custID ,ContactName: contactName, CompanyName: companyName, ContactTitle:"", Address:"", City:""
       6:          , Region:"", PostalCode:"", Country:"", Phone:"", Fax:"" };
       7:   
       8:      //save item to server using Astoria Service
       9:      var wRequest =  new Sys.Net.WebRequest();
      10:      wRequest.set_httpVerb("POST");
      11:      wRequest.set_url("http://galcho-pc:86/northwind.svc/Customers"); 
      12:      
      13:      wRequest.get_headers()["Accept"] = "application/json";
      14:      wRequest.get_headers()["Content-Type"] = "application/json";
      15:      wRequest.add_completed(function (response, eventArgs){
      16:          var statusText = response.get_statusText();
      17:          alert(statusText);
      18:      });
      19:      
      20:      wRequest.set_body(Sys.Serialization.JavaScriptSerializer.serialize(customer));
      21:      wRequest.invoke();
      22:  }

    And the button that execute is defined with following HTML

    <input type="button" value="New Customers" onclick="createNewCustomer('AAAA','Galin Iliev', 'Galcho.COM');" />

    Hope you find it interesting.

    UPDATE: Here is sample project:

    AstoriaTests.zip (191.09 KB)

    In order to run sample project do following:

    • make sure you installed the packages stated here.
    • extract files and modify Connection string NorthingEntites in web.config to match your SQL server that run Northwind DB
    • when running the sample make sure the URL in browser's address bar contain same hostname as in astoriaUrl JS variable (Default.aspx line 29) otherwise you will get "Access Denied!" error from browser single origin restriction. 

    How to consume Astoria Service

    A week ago I wrote about setting-up Astoria Service. Now is time to expose real power of Astoria - consuming data from client script.

    Once you have setup your Web Data Service you're ready to write ASP.NET AJAX script to consume it. Basically you need these things:

    • set Requiest's header "accept" value of "application/json" so the data can be used directly in JS
    • call appropriate URL
    • and, of course, process data

    The rest is done form Astoria team :) Kudos for them

    this is how the code looks like:

       1:  function loadCustomers(){
       2:      var webRequest = new Sys.Net.WebRequest();   
       3:      
       4:      webRequest.set_url("http://galcho-pc:86/northwind.svc/Customers");   
       5:      
       6:      webRequest.get_headers()["accept"] = "application/json";
       7:      
       8:      webRequest.add_completed(function (result){
       9:          var cust = result.get_object();   
      10:          for (i = 0; i < cust.length; i++){   
      11:              addCustomer(cust[i].ContactName, cust[i].CompanyName);
      12:          }  
      13:      });   
      14:      
      15:      webRequest.invoke(); 
      16:  }
      17:   
      18:  function addCustomer(contactName, companyName){
      19:      $get("customers").innerHTML += "<div>" + contactName + "</div>" + companyName + "<br/>";
      20:  }

    There main function is loadCustomers(). It does all I described above. And there is a helper function that fills a DIV's innerHTML property with approperiate content.

    Here is the HTML:

       1:  <div id="customers">
       2:      <span style="font-size: large; font-weight: bold; border: solid 1px black; width: 100%;
       3:          display: block;">
       4:          <div>
       5:              Contact Name</div>
       6:          CompanyName</span>
       7:  </div>

    And the result looks like:

     

    You can download full ASPX file from here:

    Astoria-Simple-Test.zip (0.94 KB)

    More sophisticated article for Astoria consumer app is coming. It is based on MIX AJAX classed that provide infrastructure for CRUD operations.