Reader small image

You're reading from  Entity Framework Tutorial (Update) - Second Edition

Product typeBook
Published inAug 2015
Reading LevelIntermediate
Publisher
ISBN-139781783550012
Edition2nd Edition
Languages
Right arrow
Author (1)
Joydip Kanjilal
Joydip Kanjilal
author image
Joydip Kanjilal

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He is currently working as a Chief Software Architect at a reputed software company in Hyderabad. Joydip has almost 20 years of industry experience in IT, with more than 14 years in Microsoft .NET and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of several books and more than 250 articles. Many of his articles have been featured at Microsoft's Official Site on ASP.NET. Joydip has authored the following books:- ASP.NET Web API (Packt Publishing) Visual Studio Six in One (Wrox Publishers) ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing) Joydip has also reviewed more than a dozen books till date. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb's Conference 2014 in Bangalore. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He blogs these days at: http://www.infoworld.com/blog/microsoft-coder
Read more about Joydip Kanjilal

Right arrow

Chapter 8. Working with WCF Data Services

ADO.NET Data Services, formerly known as Project Astoria, comprises a collection of patterns and libraries that can be used to expose an application's data as a service. This service can then be consumed by client applications using HTTP calls. You can use ADO.NET Data Services to isolate the Data Access Layer, and it exposes data via WCF services to discover, manipulate, and retrieve data in a corporate network. You can also use ADO.NET Data Services to expose data that is retrieved using the Entity Data Model as a service and then access this service using WCF service calls over the HTTP protocol.

WCF Data Services replaces ADO.NET Data Services with more added features. WCF Data Services (formerly known as ADO.NET Data Services) enables you to build RESTful services that leverage the Open Data Protocol (OData) to expose and consume data over the web or intranet.

In this chapter, we will examine the following points:

  • Understanding REST and RESTful...

Introducing WCF Data Services


Primarily designed in order to separate the presentation layer and data in a REST-based model, WCF Data Services are used to expose data as a service so it can be accessed via HTTP requests. You can use the standard HTTP verbs MERGE, GET, POST, PUT, and DELETE and perform CRUD operations against the service. WCF Data Services expose the database schema in terms of XML metadata. It uses Atom and JSON data formats for data transfers over the HTTP protocol.

WCF Data Services isolate the Data Access Layer and expose data via WCF Services. We do not need to have the presentation layer as a consumer of Data Services. We could also have another service as occurs in SOA scenarios.

You can use WCF Data Services to expose data through Web Services in terms of EDM abstractions such as EDM objects. These objects can then be accessed by any application in much the same way such as a Web Service is accessed.

How do WCF Data Services and Web Services differ?

The primary difference...

Exposing data as a service using WCF Data Services


You have two options to expose data as a service using WCF Data Services:

  • Use a relational database as the data source

  • Use a data source other than a relational database

If you are using a relational database as the data source, you can use LINQ to SQL or Entity Framework to expose the data. On the other hand, you can use collections of objects as a data source too. In either case, you need to create a WCF Data Service using the designer. In the next section, we will discuss how we can create an ADO.NET Data Service.

Why do we need REST?


In a typical client-server communication over a network, a server is connected to one or more clients using some protocols, that is, HTTP or TCP, and so on.

What is REST? Why is it becoming so popular over time? Is REST an alternative to Web Services? How can I make use of the .NET Framework to implement RESTful Services? We will answer these questions as we progress through the sections in this chapter.

REST is an architectural style for designing distributed applications that can intercommunicate. Note that REST is not a technology or a set of standards. Rather, it is a set of constraints that can be used to define a new style of architecture. Essentially, it is a client-server architectural style where the connections are stateless.

Note

The REST architecture style can be applied to other protocols as well. The word "stateless" applies to HTTP/HTTPS protocols. The REST architectural style is popular in the HTTP world and gives better results when used in combination with...

What is the OData Protocol?


With the rise in the availability of data, it is extremely important that we keep our data in a structured format as much as possible. Whether we choose to work on a mainframe, a mini, a server farm or a PC, a standardized API to deal with the data in a structured format is needed. If the data is relational, SQL provides a set of operations to query and also update it. However, not all data is relational. Further, not all relational data is exposed for use in processing SQL statements or on a wider perspective over the world-wide Internet.

OData is an open protocol for sharing data and exposing data as a web-friendly data feed. It provides you with a uniform way of representing structured data in Atom and JSON formats and a uniform URL for navigation, sorting, filtering, and paging data retrieved from a Data Service.

The official website for OData states:

"The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock...

Working with WCF Data Services and Entity Framework


In this section, we will discuss how we can create a WCF Data Service using Entity Framework and the AdventureWorks database. To add a WCF Data Service application to an existing web application, follow these steps:

  1. Create an ASP.NET application:

  2. Create a new Entity Data Model and name it AdventureWorks:

  3. Select Code First from database:

  4. In the Entity Data Model Wizard dialog, specify the data connection properties and click Next:

  5. Next, choose the database objects you want to be part of the Entity Data Model:

  6. Now, right-click on the project in the solution explorer window and select Add New Item.

  7. Select WCF Data Service from the list of the templates displayed, specify a name, and click OK.

And, you are done! At first glance, the WCF Data Service class looks like this:

using System.Data.Services;
using System.Data.Services.Common;
namespace WCFDataServices
{
    public class CurrencyDataService : DataService< /* TODO: put 
    your data source...

Working with OData Services using WCF and ASP.NET MVC Framework


In this section, we will discuss how we can create an OData Service and consume it from an ASP.NET MVC application. To get started using OData in .NET applications, you need to first create a WCF Data Service. You can create and use an Entity Data Model using the ADO.NET Entity Framework template in Visual Studio.

Here are the steps to create an OData Service:

  1. Create an Entity Data Model or a source of data using LINQ to SQL. You can also use a custom data model, but it should implement the IUpdateable and IQueryable interfaces.

  2. Expose the data model as a WCF Data Service.

  3. Host the WCF Data Service.

  4. Consume the WCF Data Service in a client application.

To create a WCF Data Service, click on Project | Add New Item and then select WCF Data Service from the list of the templates displayed. Note that you will need to add these references to your project:

  • System

  • System.Core

  • System.ServiceModel

  • System.ServiceModel.Web

  • System.Data.Services...

Working with Protobuf WCF Services


Protocol Buffers (Protobuf) is a serialization format developed by Google that is fast and easy to use. Frequently referred to as "Protobuf," the technology consists of a language- and platform-neutral, extensible, binary format for fast data exchange that relies on an interface description language, which is compiled to native code. In this section, we will explore Protobuf and how we can work with Protobuf and Windows Communication Foundation (WCF), which is Microsoft's unified programming model for building service-oriented apps.

Protobuf-net is much faster than the other binary serializers available, and is much faster than BinaryFormatter when it comes to serialization, with a much reduced payload too.

To work with Protobuf, you need to have the following installed on your system:

  • Visual Studio 2013 or higher

  • Protocol Buffers for .NET (Protobuf-net)

You can freely download a copy of Protocol Buffers for .NET from https://code.google.com/p/protobuf-net...

Summary


REST is now all set to be the architectural paradigm of choice for designing and implementing scalable services. It is an architectural paradigm that is based on the stateless HTTP protocol and is used to design applications that can inter-communicate. In REST, resources are used to represent state and functionality and these resources are in turn represented using user-friendly URLs. The RESTful Web Services expose resources through URIs and use the HTTP methods to perform CRUD operations. The REST architectural paradigm not only opens up a lot of possibilities, but also challenges. The OData protocol allows you to query data over the HTTP protocol and then get the results back in Atom, JSON, or XML formats.

Google's Protocol Buffers is a binary serialization format used for data exchanges. Protocol Buffer is fast becoming the technology of choice as a popular serialization format in REST-based WCF services as it is a way of encoding structured data in an extensible format.

In this...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Entity Framework Tutorial (Update) - Second Edition
Published in: Aug 2015Publisher: ISBN-13: 9781783550012
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Joydip Kanjilal

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He is currently working as a Chief Software Architect at a reputed software company in Hyderabad. Joydip has almost 20 years of industry experience in IT, with more than 14 years in Microsoft .NET and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of several books and more than 250 articles. Many of his articles have been featured at Microsoft's Official Site on ASP.NET. Joydip has authored the following books:- ASP.NET Web API (Packt Publishing) Visual Studio Six in One (Wrox Publishers) ASP.NET 4.0 Programming (Mc-Graw Hill Publishing) Entity Framework Tutorial (Packt Publishing) Pro Sync Framework (APRESS) Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing) ASP.NET Data Presentation Controls Essentials (Packt Publishing) Joydip has also reviewed more than a dozen books till date. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb's Conference 2014 in Bangalore. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He blogs these days at: http://www.infoworld.com/blog/microsoft-coder
Read more about Joydip Kanjilal