Vagif Abilov
Miles AS
vagif.abilov@gmail.com
@ooobject
github: object
http://bloggingabout.net/blogs/vagif/
 OData  and REST
 WCF Data Services
 OData feed in 5 minutes: EF + MS SQL
 The power of ObjectContext
 Using reflection provider
 Using custom providers and NoSQL
 Extending OData services
 Performance and diagnostics
 A song about OData
Are you familiar with OData?

 Have you used OData in your projects?

Do you have components on your machine
     that use OData communication?
 The Open Data Protocol (OData) is a Web
  protocol for querying and updating data
 Consumers query a datasource over HTTP
  and get results back in formats like
  AtomPub, JSON or plain XML
 OData follows many of the principles of
  REST
Querying NuGet feed with LINQPad

Importing data from OData feed into
 Microsoft Excel using PowerPivot
 Command   content specified in URIs
 Use of HTTP verbs (GET, POST, PUT,
  DELETE)
 Use of HTTP status codes
 Response sent as XML (AtomPub) or
  JSON
 Related content specified using link
  elements
Hypermedia


  HTTP


   URI
Method   Safe   Idempotent
GET      Yes       Yes
POST     No        No
PUT      No        Yes
DELETE   No        Yes
http://localhost:50555/InMemory.svc/
Customers('ALFKI')

http://localhost:50555/InMemory.svc/
Customers()?$filter=CompanyName eq
'Alfreds Futterkiste„

http://localhost:50555/InMemory.svc/
Customers()?$expand=Orders
http://localhost.:50555/InMemory.svc/
Customers
<entry term="Northwind.InMemory.Customers" />
 <content type="application/xml">
  <m:properties>
    <d:CompanyName>My
Company</d:CompanyName>
    <d:CustomerID>TEMP</d:CustomerID>
  </m:properties>
 </content>
</entry>
http://localhost.:50555/InMemory.svc/
Customers(„TEMP')
<entry term="Northwind.InMemory.Customers" />
 <content type="application/xml">
  <m:properties>
    <d:CompanyName>My New
Company</d:CompanyName>
    <d:CustomerID>TEMP</d:CustomerID>
  </m:properties>
 </content>
</entry>
http://localhost.:50555/InMemory.svc/
Customers(„TEMP')
Request: a company with non-existing
CompanyName

Response: an empty collection

Request: a company with non-existing
CompanyID

Response: 404 Not Found
var ctx = new NorthwindContext("http://localhost.:50555/InMemory.svc");
ctx.IgnoreResourceNotFoundException = true;
var customers =
         from c in ctx.Customers
         where c.CustomerID == "XYZ"
         select c;
 Formerly ADO.NET    Data Services
 Codename “Astoria”
 OData provider library for .NET
 Not to be confused with WCF RIA Services
  designed specifically for end-to-end
  Silverlight and ASP.NET applications
 Using Entity Framework provider
  • EF model first+ MS SQL – trivial
  • EF code first, different DB vendors – easy
 Using reflection provider
  • Any context exposing a set of IQueryable
    properties – easy
  • Implementing IUpdatable for updatable OData
    feeds – easy
 Using custom provider
  • OData provider toolkit usually helps – still complex
public class EntityFrameworkModelFirstMsSql :
DataService<NorthwindMsSqlContext>
{
  public static void InitializeService(DataServiceConfiguration config)
  {
     config.SetEntitySetAccessRule("*", EntitySetRights.All);
     config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
     config.DataServiceBehavior.MaxProtocolVersion =
DataServiceProtocolVersion.V2;
  }

    protected override NorthwindMsSqlContext CreateDataSource()
    {
      return new NorthwindMsSqlContext(/* connection string */);
    }
}
 As simple as with MS SQL assuming EF
  provider for the respective vendor is
  available
 Proven choice: Devart dotConnect product
  family:
  • Oracle
  • MySQL
  • PostgreSQL
  • SQLite
 Not supported by tools
 EDMX is just an XML file with 3 main
  sections:
  • Conceptual model (CSDL)
  • Storage model (SSDL)
  • Mapping (MSL)
 You   can author your own EF model:
  • Support for n database vendors
  • 2n + 1 XML sections (1 CSDL + n SSDL + n MSL)
 DbContext   class, wraps ObjectContext
 Model is generated from entities
 Database can be generated from the
  model
 Convention over configuration reduces
  configuration code to bare minimum
 No more trick to share conceptual model
  between databases from different vendors
public class NorthwindContextBase : DbContext
{
  public ObjectContext ObjectContext
  {
     get { return (this as IObjectContextAdapter).ObjectContext; }
  }
}

public class EntityFrameworkCodeFirstMsSql : DataService<ObjectContext>
{
  protected override ObjectContext CreateDataSource()
  {
     var ctx = new NorthwindContext("NorthwindContext.EF.CF.MsSql");
     return ctx.ObjectContext;
  }
}
 Suitable for most of scenarios with static
  resource set information (schema known
  at compile time)
 Resource sets exposed as IQueryable<T>
  properties of the context class
 DataServiceKey attribute specifies entity
  key
 Optional support for IUpdatable
OData feed exposing
in-memory data collection

  OData feed using
 NHibernate to access
  MS SQL database
 Entity Framework provider is the quickest
  way to add OData feed on the top of a
  popular SQL database
 Reflection provider covers most of other
  scenarios when the data schema is known
  up-front
 Custom provider is needed when the
  schema is discovered at runtime or data
  don‟t have fixed types
 If the content of NoSQL database is known
  at compile time, reflection provider can be
  used
 If the content of NoSQL database is
  divided into collections of fixed types, the
  resource sets can be built at runtime
 If the content is untyped, OData feed can
  still be built using open types
Creating OData feed for MongoDB
    using WCF Data Services
   and OData Provider Toolkit
 OData   data model supports open types for
  Entries. An entry of an open type may
  have extra properties (dynamic properties)
  in addition to those statically declared in
  metadata
 WCF Data Services framework takes care
  of transforming the incoming requests into
  an expression tree. Custom code is
  responsible for evaluation expression tree
  against the data store
 Custom   service operations can only use
  HTTP GET and POST
 Generated client context code does not
  include custom service operations – it has
  to be added manually
 Default webHttpBinding settings may not
  be sufficient for large payload
  (send/receiveTimeout, maxBufferSize and
  maxStringContentLength often need to be
  increased)
[WebInvoke(Method = "POST")]
public void DeleteProductsByNamePattern(string namePattern)
{
  this.CurrentDataSource.ExecuteStoreCommand(
         string.Format("DELETE FROM Products WHERE
         ProductName LIKE '{0}%'", namePattern));
}
public void DeleteProductsByNamePattern(string namePattern)
{
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
     string.Format("{0}/DeleteProductsByNamePattern?namePattern={1}",
               this.BaseUri, namePattern));
  request.Method = "POST";
  request.Accept = "application/atom+xml,application/xml";
  request.ContentType = "application/atom+xml";
  request.ContentLength = 0;
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
 OData  design goal is to provide data
  publishing protocol for services, not for
  low-level components
 Don‟t use OData to implement cascaded
  updates and deletes of large number of
  dependent entities
 Understand the difference between client-
  side and server-side operations
 LINQPad   (LINQ to OData)
 Fiddler
 RedGate   .NET Reflector Pro (to step-in to
  WCF Data Services code)
 Oren Eini‟s profilers (EF Profiler,
  NHibernate Profiler)
 Elmah logging framework (install it using
  NuGet, it will update your web.config)
 Don‟t reinvent the wheel, don‟t come with
  your own standards
 Consider OData to expose data sources
  on the Web
 Don‟t treat OData as a SQL database
  publisher
 Writing OData feeds is easy
 Code   examples for this presentation:
  https://github.com/object/
  NorthwindOData
 Open Data Protocol:
  http://www.odata.org/
  http://www.odata.org/mailing-list
 WCF Data Services forum:
  http://social.msdn.microsoft.com/Forums/e
  n/adodotnetdataservices/
 WCF    Data Services Toolkit:
  http://wcfdstoolkit.codeplex.com/
 Microsoft WCF Data Services March 2011
  CTP 2 for .NET Framework 4 and
  Silverlight 4:
  http://www.microsoft.com/downloads/en/de
  tails.aspx?FamilyID=60fb0117-8cea-4359-
  b392-6b04cdc821be
OData!
(Lennon – McCartney)
OData! Please relieve me
AtomPub is what I‟m gonna need
Believe me when I tell you:
AtomPub is what I‟m gonna need
OData! Please relieve me
JSON‟s what I want in JavaScript
Believe me when I tell you:
JSON‟s what I want in JavaScript
When you told me:
“Internal error 500”
Oh well you know,
I nearly broke down and cried

When you told me:
“Internal error 501”
Oh well you know,
I nearly broke down and died
OData! Please relieve me
I want to live a RESTful life
Believe me when I tell you:
I want to live a RESTful life
 Vagif  Abilov
 Miles AS
 vagif.abilov@gmail.com
 @ooobject
 github: object
 http://bloggingabout.net/blogs/vagif/

Practical OData

  • 1.
    Vagif Abilov Miles AS vagif.abilov@gmail.com @ooobject github:object http://bloggingabout.net/blogs/vagif/
  • 2.
     OData and REST  WCF Data Services  OData feed in 5 minutes: EF + MS SQL  The power of ObjectContext  Using reflection provider  Using custom providers and NoSQL  Extending OData services  Performance and diagnostics  A song about OData
  • 3.
    Are you familiarwith OData? Have you used OData in your projects? Do you have components on your machine that use OData communication?
  • 4.
     The OpenData Protocol (OData) is a Web protocol for querying and updating data  Consumers query a datasource over HTTP and get results back in formats like AtomPub, JSON or plain XML  OData follows many of the principles of REST
  • 8.
    Querying NuGet feedwith LINQPad Importing data from OData feed into Microsoft Excel using PowerPivot
  • 9.
     Command content specified in URIs  Use of HTTP verbs (GET, POST, PUT, DELETE)  Use of HTTP status codes  Response sent as XML (AtomPub) or JSON  Related content specified using link elements
  • 10.
  • 11.
    Method Safe Idempotent GET Yes Yes POST No No PUT No Yes DELETE No Yes
  • 12.
  • 13.
    http://localhost.:50555/InMemory.svc/ Customers <entry term="Northwind.InMemory.Customers" /> <content type="application/xml"> <m:properties> <d:CompanyName>My Company</d:CompanyName> <d:CustomerID>TEMP</d:CustomerID> </m:properties> </content> </entry>
  • 14.
    http://localhost.:50555/InMemory.svc/ Customers(„TEMP') <entry term="Northwind.InMemory.Customers" /> <content type="application/xml"> <m:properties> <d:CompanyName>My New Company</d:CompanyName> <d:CustomerID>TEMP</d:CustomerID> </m:properties> </content> </entry>
  • 15.
  • 16.
    Request: a companywith non-existing CompanyName Response: an empty collection Request: a company with non-existing CompanyID Response: 404 Not Found
  • 17.
    var ctx =new NorthwindContext("http://localhost.:50555/InMemory.svc"); ctx.IgnoreResourceNotFoundException = true; var customers = from c in ctx.Customers where c.CustomerID == "XYZ" select c;
  • 18.
     Formerly ADO.NET Data Services  Codename “Astoria”  OData provider library for .NET  Not to be confused with WCF RIA Services designed specifically for end-to-end Silverlight and ASP.NET applications
  • 19.
     Using EntityFramework provider • EF model first+ MS SQL – trivial • EF code first, different DB vendors – easy  Using reflection provider • Any context exposing a set of IQueryable properties – easy • Implementing IUpdatable for updatable OData feeds – easy  Using custom provider • OData provider toolkit usually helps – still complex
  • 20.
    public class EntityFrameworkModelFirstMsSql: DataService<NorthwindMsSqlContext> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } protected override NorthwindMsSqlContext CreateDataSource() { return new NorthwindMsSqlContext(/* connection string */); } }
  • 21.
     As simpleas with MS SQL assuming EF provider for the respective vendor is available  Proven choice: Devart dotConnect product family: • Oracle • MySQL • PostgreSQL • SQLite
  • 22.
     Not supportedby tools  EDMX is just an XML file with 3 main sections: • Conceptual model (CSDL) • Storage model (SSDL) • Mapping (MSL)  You can author your own EF model: • Support for n database vendors • 2n + 1 XML sections (1 CSDL + n SSDL + n MSL)
  • 23.
     DbContext class, wraps ObjectContext  Model is generated from entities  Database can be generated from the model  Convention over configuration reduces configuration code to bare minimum  No more trick to share conceptual model between databases from different vendors
  • 24.
    public class NorthwindContextBase: DbContext { public ObjectContext ObjectContext { get { return (this as IObjectContextAdapter).ObjectContext; } } } public class EntityFrameworkCodeFirstMsSql : DataService<ObjectContext> { protected override ObjectContext CreateDataSource() { var ctx = new NorthwindContext("NorthwindContext.EF.CF.MsSql"); return ctx.ObjectContext; } }
  • 25.
     Suitable formost of scenarios with static resource set information (schema known at compile time)  Resource sets exposed as IQueryable<T> properties of the context class  DataServiceKey attribute specifies entity key  Optional support for IUpdatable
  • 26.
    OData feed exposing in-memorydata collection OData feed using NHibernate to access MS SQL database
  • 27.
     Entity Frameworkprovider is the quickest way to add OData feed on the top of a popular SQL database  Reflection provider covers most of other scenarios when the data schema is known up-front  Custom provider is needed when the schema is discovered at runtime or data don‟t have fixed types
  • 28.
     If thecontent of NoSQL database is known at compile time, reflection provider can be used  If the content of NoSQL database is divided into collections of fixed types, the resource sets can be built at runtime  If the content is untyped, OData feed can still be built using open types
  • 29.
    Creating OData feedfor MongoDB using WCF Data Services and OData Provider Toolkit
  • 30.
     OData data model supports open types for Entries. An entry of an open type may have extra properties (dynamic properties) in addition to those statically declared in metadata  WCF Data Services framework takes care of transforming the incoming requests into an expression tree. Custom code is responsible for evaluation expression tree against the data store
  • 31.
     Custom service operations can only use HTTP GET and POST  Generated client context code does not include custom service operations – it has to be added manually  Default webHttpBinding settings may not be sufficient for large payload (send/receiveTimeout, maxBufferSize and maxStringContentLength often need to be increased)
  • 32.
    [WebInvoke(Method = "POST")] publicvoid DeleteProductsByNamePattern(string namePattern) { this.CurrentDataSource.ExecuteStoreCommand( string.Format("DELETE FROM Products WHERE ProductName LIKE '{0}%'", namePattern)); }
  • 33.
    public void DeleteProductsByNamePattern(stringnamePattern) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create( string.Format("{0}/DeleteProductsByNamePattern?namePattern={1}", this.BaseUri, namePattern)); request.Method = "POST"; request.Accept = "application/atom+xml,application/xml"; request.ContentType = "application/atom+xml"; request.ContentLength = 0; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); }
  • 34.
     OData design goal is to provide data publishing protocol for services, not for low-level components  Don‟t use OData to implement cascaded updates and deletes of large number of dependent entities  Understand the difference between client- side and server-side operations
  • 35.
     LINQPad (LINQ to OData)  Fiddler  RedGate .NET Reflector Pro (to step-in to WCF Data Services code)  Oren Eini‟s profilers (EF Profiler, NHibernate Profiler)  Elmah logging framework (install it using NuGet, it will update your web.config)
  • 36.
     Don‟t reinventthe wheel, don‟t come with your own standards  Consider OData to expose data sources on the Web  Don‟t treat OData as a SQL database publisher  Writing OData feeds is easy
  • 37.
     Code examples for this presentation: https://github.com/object/ NorthwindOData  Open Data Protocol: http://www.odata.org/ http://www.odata.org/mailing-list  WCF Data Services forum: http://social.msdn.microsoft.com/Forums/e n/adodotnetdataservices/
  • 38.
     WCF Data Services Toolkit: http://wcfdstoolkit.codeplex.com/  Microsoft WCF Data Services March 2011 CTP 2 for .NET Framework 4 and Silverlight 4: http://www.microsoft.com/downloads/en/de tails.aspx?FamilyID=60fb0117-8cea-4359- b392-6b04cdc821be
  • 39.
  • 40.
    OData! Please relieveme AtomPub is what I‟m gonna need Believe me when I tell you: AtomPub is what I‟m gonna need
  • 41.
    OData! Please relieveme JSON‟s what I want in JavaScript Believe me when I tell you: JSON‟s what I want in JavaScript
  • 42.
    When you toldme: “Internal error 500” Oh well you know, I nearly broke down and cried When you told me: “Internal error 501” Oh well you know, I nearly broke down and died
  • 43.
    OData! Please relieveme I want to live a RESTful life Believe me when I tell you: I want to live a RESTful life
  • 44.
     Vagif Abilov  Miles AS  vagif.abilov@gmail.com  @ooobject  github: object  http://bloggingabout.net/blogs/vagif/