Skip to content

Exposing LINQ2DynamoDB.DataContext as an OData endpoint

scale-tone edited this page Apr 16, 2017 · 6 revisions

NOTE: Now we have Linq2DynamoDb.WebApi.OData package, that provides an easier and more up-to-date way of creating OData resources for DynamoDB tables. Please, see this post for details. The approach described below still works though.

As soon as you have designed your data context, it is now very easy to expose your data to Internet via the becoming more and more popular OData protocol.

To do that:

1. Change the base class for your data context from DataContext to UpdatableDataContext:

public class NotesDataContext : UpdatableDataContext
{
...
}

The UpdatableDataContext class implements the IUpdatable interface, which is necessary to enable data modifications via the OData protocol.

2. If your entity does not conform to WCF Data Services convention for key properties, then put DataServiceKeyAttribute to your entity:

[DynamoDBTable("Notes")]
[DataServiceKey("SomeSpecificId")](DataServiceKey(_SomeSpecificId_))
public class Note : EntityBase
{
    public int SomeSpecificId { get; set; }

    public string Text { get; set; }

    public DateTime TimeCreated { get; set; }
}

3. In your Visual Studio create a new ASP.NET Web Forms Application (if you don't have one yet).

4. Add a new WCF Data Service to your ASP.NET project (right-click on the project->Add->New Item->WCF Data Service) and specify your data context as DataService's template parameter:

public class NotesDataService : DataService<NotesDataContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
}

5. You would probably need to adjust the Microsoft.Data.Services package version used by your project:

install-package Microsoft.Data.Services -version 5.0.2

and fix the DataServiceHostFactory's version in your .SVC-file:

<%@ ServiceHost Language="C#" Factory="System.Data.Services.DataServiceHostFactory, Microsoft.Data.Services, Version=5.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Service="MobileNotes.Web.Services.NotesDataService" %>

(because of the problem described here)

That's it! Now your DynamoDb entity sets are accessible via a browser: Exposing LINQ2DynamoDB.DataContext as an OData-endpoint_NotesOdata

You can query them by specifying a query string in the address bar, you can access and modify your data from JavaScript on your HTML page, from Silverlight, from a mobile application etc., etc.

[This sample project](Exposing DynamoDb data as an OData endpoint and consuming it on WinPhone Sample) demonstrates the concepts described above and also shows how to authenticate your mobile users and restrict access to your OData endpoint.

See more about creating a WCF Data Service using the Reflection provider here.

Why OData protocol is cool, is clearly explained here.