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

Building an OData service with WCF Data Service and LINQ to SQL


In addition to ADO.NET Entity Framework, LINQ to SQL is another popular and powerful component we can use for mapping relational database objects to .NET CLR class objects. Many popular RDBMS (such as SQL Server and Oracle) have provided LINQ to SQL providers. And for WCF Data Service, it is quite reasonable to add support for exposing a LINQ to SQL based data source via OData service endpoints. In this recipe, we will introduce you to creating an OData service from a LINQ to SQL based data source model.

Getting ready

Make sure you have a sample database, such as Northwind, installed in a local SQL Server instance. You can use an SQL Express instance (the free version of SQL Server) for convenience.

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

How to do it...

You can follow the steps given for creating an OData service from LINQ to SQL data entities:

  1. 1. Create a new ASP.NET Empty Web Application in Visual Studio 2010.

  2. 2. Create the LINQ to SQL data model types by using the LINQ to SQL Classes item template (see the following screenshot).

    After the data model is created, we can use Visual Studio Server Explorer to drag certain tables (from the sample database) into the data model designer. This will make the Visual Studio IDE create the corresponding data entity types.

    Note

    Save all items in the project so as to make sure Visual Studio IDE has compiled the generated LINQ to SQL data model types.

  3. 3. Create a new WCF Data Service based on the generated LINQ to SQL data model.

    This time, we use the LINQ to SQL data model class as the generic parameter of the service class (see the following code snippet).

    public class NWODataService : DataService< ODataLINQ2SQLService.NorthwindDataContext >
    {
    // This method is called only once to initialize service-wide // policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    }
    }
    
  4. 4. Select the .svc service file in Visual Studio and launch it through the View in browser context menu.

How it works...

Although we directly use the LINQ to SQL data model class as the data source, the WCF Data Service runtime actually treats the LINQ to SQL data model class like a custom data source type. Therefore, any public member (of the data model class) who implements the IQueryable interface will be exposed as an entity set in the generated service. We will talk more about using custom data source type for WCF Data Service within the Using custom data objects as the data source of WCF Data Service recipe of this chapter.

There's more...

By default, the WCF Data Service, which uses the LINQ to SQL data model class, does not support editing/updating operations. In order to make the LINQ to SQL based WCF Data Service support editing/updating, we need to implement the IUpdatable interface (under System.Data.Services namespace) on the LINQ to SQL data model class (see the following code snippet).

partial class NorthwindDataContext: IUpdatable
{
......
}

For detailed information about implementing IUpdatable interface for LINQ to SQL data model class, you can refer to the following MSDN reference:

How to: Create a Data Service Using a LINQ to SQL Data Source (WCF Data Services) available at http://msdn.microsoft.com/en-us/library/ee373841.aspx

See also

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

  • Using custom data objects as the data source of WCF Data Service recipe