Book Image

OData Programming Cookbook for .NET Developers

By : Juntao Cheng
Book Image

OData Programming Cookbook for .NET Developers

By: Juntao Cheng

Overview of this book

Odata (Open Data Protocol) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData enables data access among a variety of applications, services, and stores by adopting existing Web technologies such as HTTP, XML, and JSON. This book deals with common OData programming cases over the Microsoft .NET Framework platform and eases the learning curve for a .NET developer to start incorporating OData in data service development.This book provides a collection of recipes that help .NET developers to get familiar with OData programming in a quick and efficient manner. The recipes cover most OData features from the former ADO.NET Data Service to the current WCF Data Service platform. In addition, all the sample cases here are based on real-world scenarios and issues that .NET developers might come across when programming with OData in application development.This book will be your handy guide with basic to advanced walkthroughs of common OData programming cases for the Microsoft .NET Framework platform. You will learn quick solutions to necessary tasks to integrate the power of OData at both server-side and client-side.This book will help you master the use of OData with .NET Framework by taking you through hands-on and practical recipes. It starts by talking about the common means for building OData services and consuming OData services in client applications. Then, some more specific topics like hosting, configuration and security are discussed. The book also covers many popular and interesting topics such as integrating OData in web applications, and developing data-driven mobile applications with OData. Moreover, you can also find quite a few recipes discussing real-world OData producers and new features in latest and future versions.Within "OData Programming Cookbook for .NET Developers", all the recipes are selected based on real-world scenarios that you will commonly come across. Each recipe covers a specific topic, going from the description of the problem, through a conceptual solution, to a solution containing sample code. By following these recipes, you can acquire how to program with OData in a simple, effective, and easy manner.
Table of Contents (15 chapters)
OData Programming Cookbook for .NET Developers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Exposing database stored procedures in WCF Data Service


When developing data access applications with relational databases, we will often use stored procedures to encapsulate some frequently used queries. We can also gain performance improvements by using stored procedures (compared to using raw SQL queries).

Then, can we also take advantages of database stored procedures in our WCF Data Services which expose data from relational database? Absolutely yes! In this recipe, we will discuss how to expose data entities from relational database via stored procedures.

Getting ready

The service here will expose two stored procedures from the Northwind database. They are the CustOrdersOrders procedure (return Order list of a given customer) and the Ten Most Expensive Products procedure. The following are the raw signatures of these two stored procedures:

ALTER PROCEDURE [dbo].[Ten Most Expensive Products] AS ...
ALTER PROCEDURE [dbo].[CustOrdersOrders] @CustomerID nchar(5)
AS ...

The source code for this recipe can be found in the \ch01\ODataSPServiceSln\ directory.

How to do it...

  1. 1. Create a new ASP.NET Empty Web Application.

  2. 2. Create the ADO.NET Entity Framework data model and include the stored procedures together with the tables.

    We can select the database tables, views, and stored procedures we want in the Choose Your Database Objects dialog (see the following screenshot). In this case, we need to select all tables and two stored procedures.

  3. 3. Add Function Import for the stored procedures in the Data Model class.

  4. 4. Open the EF designer by double-clicking on the generated data model (.edmx file in Visual Studio Solution Explorer).

  5. 5. Right-click on the designer surface and fire the Function Import... context menu (see the following screenshot).

  6. 6. In the Add Function Import dialog, specify the detailed information of the target stored procedure we want to import.

    The following screenshot shows the import settings for the CustOrdersOrders procedure:

    The return value of the previous stored procedure mapping function is a custom complex object. You can create this complex data type (based on the columns returned in the stored procedure) by using the Create New Complex Type button at the bottom of Add Function Import dialog (see the following screenshot).

  7. 7. Add custom operations in the WCF Data Service class, which directly invokes the stored procedure mapping functions imported in the previous step.

    The following code snippet shows the custom operations definition in the sample WCF Data Service:

    [WebGet]
    public IQueryable<ExpProductObj> GetTop10ExpensiveProducts()
    {
    return this.CurrentDataSource. GetTop10ExpensiveProducts().AsQueryable();
    }
    [WebGet]
    public IQueryable<CustomerOrderObj> GetOrdersByCustomer(string custID)
    {
    return this.CurrentDataSource. GetOrdersByCustomer(custID).AsQueryable();
    }
    
  8. 8. Launch the service and invoke the stored procedure based operations in the web browser.

    The following screenshot shows the web browser output by invoking the GetOrdersByCustomer operation in the sample service:

How it works...

To use stored procedures in WCF Data Service (using the ADO.NET Entity Framework data model as data source), we need to import stored procedures as functions in the generated EF data model class. In this sample, we create some custom data types for the return value of each stored procedure mapping function. This is because in most cases, the returned data columns from a given stored procedure don't exactly match a complete data entity type (corresponding to the target database table).

In addition to the imported functions on EF data model class, we also need to add custom operations within the WCF Data Service class. These operations simply delegate the operation call to the corresponding stored procedure mapping functions.

When calling a service operation mapping to a void stored procedure (which does not return any value), we can simply use the URL address of the operation (relative from the service base address). For stored procedures that take some input parameters, we can supply the parameters by using query strings in the operation URL (as shown in the previous GetOrdersByCustomer operation sample).

See also

  • Building an OData service via WCF Data Service and ADO.NET Entity Framework recipe

  • Adding custom operations on OData service recipe