Giter Club home page Giter Club logo

webapi-odata's Introduction

WebApi-OData

These are some useful classes for when you need to auto-expose (with minimal fuss) a number of entity sets from an Entity Framework context in OData format using Web API.

It uses Web API 2.2 and exposes OData v4.

Since WCF Data Service is no longer being actively developed, I was searching for an easy-to-use replacement based on Web API, but couldn't find any that didn't require creating a godzillion custom controllers. As a result, I spun my own.

Reflection is used in the base class to locate the entity set (DbSet) property within the DbContext based on the entity's type.

The process can actually be automated further by using reflection to register all DbSet properties in the DbContext with the EDM builder, as well as to auto-generate the controller classes. Then it will be almost as easy as WCF Data Services. I haven't done it since I don't mind a few more lines of code to define the controllers, but it shouldn't be difficult.

How To Use

Exposing an entity set from a DbContext is as simple as adding a line of code:

builder.EntitySet<Entity1>("Entities1");

and building a controller (yes, you still need to do this):

public class Entities1Controller : StandardODataController<DB, Entity1, KeyType> { }

The standard controller base class implements the standard set of CRUD operations with POST/GET/PUT/PATCH/DELETE verbs.

However, many web servers actually have problem (or require additional configuration) supporting the PATCH or MERGE verbs, so in this implementation PUT is used for incremental update while POST is used for creation as well as full-object replace:

  • GET = SELECT
  • POST (no URL parameters) = INSERT
  • POST (with URL key parameters) = full-object replace
  • PUT = UPDATE (incremental)
  • DELETE = DELETE

Composite Primary Keys

There is support (via a custom routing convention) for composite primary keys. For example:

http://example.com/odata/Entities1(Key1=..., Key2=...)

gets the particular entity with the corresponding composite key values.

To use this custom routing convention, you need to add CompositeKeyRoutingConvention during configuration:

var routingConventions = ODataRoutingConventions.CreateDefault();
routingConventions.Insert(0, new OData.CompositeKeyRoutingConvention());
config.MapODataServiceRoute("ODataRoute", "odata", model, new DefaultODataPathHandler(), routingConventions, new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

See the sample for details.

Type Casting Support for TPH

There is also built-in support for type casting (again via a custom routing convention) -- which is Web API-speak for simulating OData's isof function.

For example, to get only a certain type of objects from a TPH inheritance table, you can do:

http://example.com/odata/Entities1/Namespace.Type

to get a list of all items in Entities1 that is of type Namespace.Type.

To use it, add CastRoutingConvention during configuration:

var routingConventions = ODataRoutingConventions.CreateDefault();
routingConventions.Insert(0, new OData.CastRoutingConvention());
config.MapODataServiceRoute("ODataRoute", "odata", model, new DefaultODataPathHandler(), routingConventions, new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

webapi-odata's People

Contributors

schungx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

meiometro

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.