Home > ADO.Net Data Services > Introduction to ADO.Net Data Services (Part 3: Reading and Manipulating results in a .Net consumer application)

Introduction to ADO.Net Data Services (Part 3: Reading and Manipulating results in a .Net consumer application)

NOTE: Since SP1 of .net 3.5 came out this post has become outdated. You no longer have to add the Microsoft.Data.WebClient into the project and you no longer have to create your class outside of VS and bring it in. It has become a lot easier. You can now reference the data service just like any other service in VS by add a new service reference and pointing to the data service URI. This will create the underlying model for you on the client side. I appologize for any confusion as this blog was written pre .net 3.5 SP1. Life is so much easier now!

In My last 2 blogs I have introduced ADO.Net DataServices and I have showed you how to read and manipulate the data in a browser. This blog will be a continuation of those blogs. In order to follow along in this blog you will have to minimally run through Part 1  to create the data service that will be used in this blog. I did not intend this blog to be self contained, it is part of a larger series of blogs. So, please read the first two parts of this series before attempting to work through this one. Thanks!

  1. Make sure the ADO.Net Data Service from Part 1 of this blog is running – you can do this by hooking it up in your IIS or just debugging it from Visual Studio.
  2. Open up Visual Studio 2008.
  3. Click File/New/Project
  4. Choose a Windows Forms Application and click OK. NOTE: I am showing this in a Windows Forms Application to prove a point that any .net application can be the client. You could do this in a WPF, Web Application, Silverlight App, etc….
  5. Add a reference to the Microsoft.Data.WebClient
    1. The Microsoft.Data.WebClient gets put on your computer when you install the ASP.Net Extensions
    2. It is located at %ProgramFiles%\Reference Assemblies\Microsoft\Framework\ASP.NET 3.5 Extensions
    3. Right click on the References folder and choose Add Reference…\
    4. Browse to the location of the dll and add the Microsoft.Data.WebClient.dll
  6. Next we are going to generate a new object layer classes that can be used on our client app. We are going to do this with a command line tool that is installed on the computer when you installed the ASP.Net Extensions. Microsoft will be rolling this into Visual Studio eventually, but for right now this is the best way to do this. You could do this manually by creating each class individually, but this command line tool will interrogate the Data Service to create these classes for you.
    1. Make sure the ADO.Net Data Service from Part 1 of this blog is running – you can do this by hooking it up in your IIS or just debugging it from Visual Studio.
    2. Open a Command Prompt (note to Vista users – make sure you do this as an administrator)
    3. Browse to %ProgramFiles%\Microsoft ASP.NET 3.5 Extensions
    4. Run this command: WebDataGen.exe /mode:ClientClassGeneration /outobjectlayer:AdventureWorks.cs /uri:http://localhost:61823/DemoDataService.svc
    5. Note: replace your localhost and DataService with the URI in the command above
    6. Import the generated class into your project
      1. Right click on the project and choose Add/Existing Item…
      2. Browse to %ProgramFiles%\Microsoft ASP.NET 3.5 Extensions and find the AdventureWorks.cs that was created. Then click Add.
  7. Right click on the project and choose Add/New Item…
  8. Choose a class and call the class AdventureWorksServiceProcessor.
  9. Add a using statement to the class in order to have it reference the classes created in step 6 – using AdventureWorksModel;
  10. Add a using statement to the class in order to have it reference the WebClient dll we created in step 5 – using Microsoft.Data.WebClient;
  11. Add the following member to the class – AdventureWorksEntites context;
  12. Add a constructor to the class in order to open the dataservice and set the reference to the objects.
  13. Your class should look like this now (of course, change the string in the URI to point to your localhost port and service):
  14. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Data.WebClient;
    using AdventureWorksModel;
    
    namespace WindowsFormsApplication1
    {
        class AdventureWorksServiceProcessor
        {
            AdventureWorksEntities context;
    
            public AdventureWorksServiceProcessor()
            {
                Uri serviceUri = new Uri("http://localhost:61823/DemoDataService.svc");
                context = new AdventureWorksEntities(serviceUri);
                context.MergeOption = MergeOption.AppendOnly;
            }
    
        }
    }
  15. Create some retrieve methods. For this example we are going to retrieve two different ways. The first way will be with a WebDataQuery (this is just like what we did in the browser in Part 2 of this blog). The second way will be with LINQ queries. Add them both into your class because they are doing two different things (one is querying the products and the other is querying the categories).
    1. WebDataQuery way:
    2.         public IList GetProducts(string productName, ProductSubcategory category)
              {
                  int categoryId = category.ProductSubcategoryID;
                  WebDataQuery products =
                  context.CreateQuery("/ProductSubcategory(" + categoryId + ")/Product?$filter=contains(Name,'" + productName + "') or '' eq '" + productName + "'");
                  List productsSet = new List();
                  foreach (Product p in products)
                  {
                      context.LoadProperty(p, "ProductSubcategory");
                      productsSet.Add(p);
                  }
                  return productsSet;
              }
      
      
    3. LINQ way:
    4.   public IList GetCategories()
              {
                  var ProductCategories = from c in context.ProductSubcategory
                                          select c;
      
                  return ProductCategories.ToList();
              }
      
      
  16. Create the add, update, and delete methods. In the end your AdventureWorksServiceProcessor class should look like this:
  17. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Data.WebClient;
    using AdventureWorksModel;
    
    namespace WindowsFormsApplication1
    {
        class AdventureWorksServiceProcessor
        {
            AdventureWorksEntities context;
    
            public AdventureWorksServiceProcessor()
            {
                Uri serviceUri = new Uri("http://localhost:61823/DemoDataService.svc");
                context = new AdventureWorksEntities(serviceUri);
                context.MergeOption = MergeOption.AppendOnly;
            }
    
            public IList GetProducts(string productName, ProductSubcategory category)
            {
                int categoryId = category.ProductSubcategoryID;
                WebDataQuery products =
                context.CreateQuery("/ProductSubcategory(" + categoryId + ")/Product?$filter=contains(Name,'" + productName + "') or '' eq '" + productName + "'");
                List productsSet = new List();
                foreach (Product p in products)
                {
                    context.LoadProperty(p, "ProductSubcategory");
                    productsSet.Add(p);
                }
                return productsSet;
            }
    
            public IList GetCategories()
            {
                var ProductCategories = from c in context.ProductSubcategory
                                        select c;
    
                return ProductCategories.ToList();
            }
    
            public void DeleteProduct(Product product)
            {
                context.AttachObject("Product", product);
                context.DeleteObject(product);
                context.SaveChanges();
            }
    
            public void UpdateProduct(Product product)
            {
                ProductSubcategory newCategory = product.ProductSubcategory;
                context.AttachObject("Product", product);
                context.LoadProperty(product, "ProductSubcategory");
                if (newCategory.Name != product.ProductSubcategory.Name)
                {
                    context.DeleteBinding(product, "ProductSubcategory", product.ProductSubcategory);
                    context.AttachObject("ProductSubcategory", newCategory);
                    context.AddBinding(product, "ProductSubcategory", newCategory);
                }
                context.UpdateObject(product);
                context.SaveChanges();
            }
    
            public void AddProduct(Product product)
            {
                product.rowguid = Guid.NewGuid();
                context.AddObject("Product", product);
                context.AttachObject("ProductSubcategory", product.ProductSubcategory);
                context.AddBinding(product, "ProductSubcategory", product.ProductSubcategory);
                context.SaveChanges();
            }
    
        }
    }
    
    
  18. Now you can create your WebForm however you want and call out to your AdventureWorksServiceProcessor as your Data Access layer. For example: Add a combo box onto your WebForm. Then in the code behind set the datasource for the combo box to the GetCategories method:
  19.   public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                AdventureWorksServiceProcessor awsProcessor = new AdventureWorksServiceProcessor();
                comboBox1.DataSource = awsProcessor.GetCategories();
                comboBox1.DisplayMember = "Name";
            }
        }

         

Now you can play around with different form variations and call out to the methods in the AdventureWorksServiceProcessor class. Also, you could take this class to a different type of .net application (Silverlight, Web Application, WPF) and as long as you bring the AdventureWorks.cs class along and set the correct references up, it should all work just the same.

Advertisements
Categories: ADO.Net Data Services
  1. John Morales
    March 31, 2008 at 1:32 pm

    Hi Greg,

    Thanks for the introduction into the world of ADO.NET Data services. Great job. 🙂

    John

  2. rick watson
    June 26, 2008 at 7:37 pm

    Hi Greg, Using your examples I was trying to access context.ObjectStateManager so that I could intercept the the entities that are changing. However the ObjectStateManager property is not available. Can you shed some light on this one. thanks rick

  3. November 23, 2008 at 9:24 am

    Good job. I’ve used this tutorial in part for a tutorial I am writing up that touches on the subject. Great job.

  4. May 15, 2009 at 3:00 am

    thanks! i got some basic idea on data services!

  5. W.A. ten Brink
    July 9, 2009 at 12:03 pm

    It sounds very interesting but how about consuming the service from a non-DotNET application?

  1. March 27, 2008 at 12:25 pm
  2. October 21, 2008 at 2:11 pm
  3. May 26, 2009 at 8:44 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: