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 via WCF Data Service and ADO.NET Entity Framework


There are various means to create an OData service on the .NET Framework platform. And by using different means, we might need to choose different kind of data sources to provide the actual data that will be published and exposed in the OData service. In this recipe, we will start from one of the most typical approaches—creating an OData service through WCF Data Service and ADO.NET Entity Framework data model.

Getting ready

As we will use ADO.NET Entity Framework as the data source of our OData service, make sure you have a sample database, such as Northwind, installed in a local SQL Server instance. You can use SQL Express instance (the free version of SQL Server) for convenience.

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

How to do it...

To concentrate on the OData service generation and make the progress simple and clear, we will use an empty ASP.NET web application with a single OData service for demonstration. The detailed steps are as follows:

  1. 1. Launch Visual Studio 2010 IDE.

  2. 2. Fire the New Project menu and create an ASP.NET Empty Web Application through the Add New Project wizard (see the following screenshot).

  3. 3. Use the Project | Add New Item context menu to add a new ADO.NET Entity Data Model (see the following screenshot).

    The wizard will guide you on selecting a source database (such as the Northwind database used in this case) .The following screenshot shows the entity classes generated through the Northwind sample database:

  4. 4. Create a new OData service via the WCF Data Service item template.

    The WCF Data Service item template can be found in the Visual Studio 2010 built-in template list (see the following screenshot).

    By clicking on the Add button, Visual Studio will automatically generate the .svc file and its associated code files for the WCF Data Service item.

  5. 5. Use View Code context menu to open the source file of the generated WCF Data Service and replace the default service type (the generic parameter) with the Entity Framework model class (generated in the previous step).

    The following code snippet shows the WCF Data Service, which uses the Northwind data model class in this sample:

    namespace ODataEFService
    {
    public class NWDataService : DataService< ODataEFService.
    NorthwindEntities >
    {
    public static void InitializeService(DataServiceConfiguration config)
    {
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    config.SetEntitySetAccessRule ("*", EntitySetRights.All);
    }
    }
    }
    

    Note

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  6. 6. Now, we can start running the service by selecting the .svc file in Solution Explorer and choose the View in browser context menu.

    The default page of the WCF Data service will display all the OData entities that have been exposed in the service (see the following screenshot).

How it works...

In our sample web project, there are only two items. One is the ADO.NET Entity Framework data model and the other is the WCF Data Service item (as shown in the following project structure screenshot).

WCF Data Service has helped encapsulate all the underlying details of implementing an OData service. When using WCF Data Service to generate OData service, what we need to do is:

  • Prepare the data source provider type (in our case, the ADO.NET Entity Framework model)

  • Associate the data source provider with the WCF Data Service

Also, as the name indicates, WCF Data Service is a special implementation of WCF service. And more specifically, WCF Data Service is a specially implemented WCF service over the REST HTTP endpoint (by using the WebHttpBinding binding type). In most cases, we do not need to take care of those WCF service-specific configuration details (in web.config file). If we open the web.config file of our sample service, we can find that there is almost nothing defined within the<system.serviceModel> element for the WCF configuration (see the following screenshot).

See also

  • Exploring an OData service through web browser recipe in Chapter 2, Working with OData at Client Side

  • Applying basic access rules on WCF Data Service recipe in Chapter 3, OData Service Hosting and Configuration