Skip to content

Instantly share code, notes, and snippets.

@joshbooker
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbooker/5ff03b12e77ff93bea14 to your computer and use it in GitHub Desktop.
Save joshbooker/5ff03b12e77ff93bea14 to your computer and use it in GitHub Desktop.
WebAPI OData Sample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ODataSample.Models;
namespace ODataSample.Controllers
{
using Microsoft.Restier.WebApi;
public class AdventureWorksController : ODataDomainController<AdventureWorksDomain>
{
private AdventureWorksContext DbContext
{
get { return Domain.Context;}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ODataSample.Models;
namespace ODataSample.Controllers
{
using Microsoft.Restier.EntityFramework;
public class AdventureWorksDomain : DbDomain<AdventureWorksContext>
{
public AdventureWorksContext Context
{
get { return DbContext; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace ODataSample
{
using Microsoft.Restier.WebApi;
using Microsoft.Restier.WebApi.Batch;
using Controllers;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
RegisterAdventureWorks(config, GlobalConfiguration.DefaultServer);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static async void RegisterAdventureWorks(HttpConfiguration config, HttpServer server)
{
await config.MapODataDomainRoute<AdventureWorksController>(
"AdventureWorksV4", "AdventureWorks/V4",
new ODataDomainBatchHandler(server));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Batch;
using System.Web.Http.OData.Extensions;
using ODataSample.Models;
namespace ODataSample
{
using Microsoft.Restier.WebApi;
using Microsoft.Restier.WebApi.Batch;
using Controllers;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
RegisterAdventureWorksV4(config, GlobalConfiguration.DefaultServer);
RegisterAdventureWorksV3(config, GlobalConfiguration.DefaultServer);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static async void RegisterAdventureWorksV4(HttpConfiguration config, HttpServer server)
{
await config.MapODataDomainRoute<AdventureWorksController>(
"AdventureWorksV4", "AdventureWorks/v4",
new ODataDomainBatchHandler(server));
}
/**/
public static void RegisterAdventureWorksV3(HttpConfiguration config, HttpServer server)
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Address>("Addresses");
builder.EntitySet<AddressType>("AddressTypes");
builder.EntitySet<BusinessEntity>("BusinessEntities");
builder.EntitySet<BusinessEntityContact>("BusinessEntityContacts");
builder.EntitySet<BusinessEntityAddress>("BusinessEntityAddresses");
builder.EntitySet<EmailAddress>("EmailAddresses");
builder.EntitySet<Person>("People");
builder.EntitySet<PersonPhone>("PersonPhones");
builder.EntitySet<PhoneNumberType>("PhoneNumberTypes");
builder.EntitySet<Password>("Passwords");
builder.EntitySet<StateProvince>("StateProvinces");
builder.EntitySet<CountryRegion>("CountryRegions");
builder.EntitySet<ContactType>("ContactTypes");
//HttpServer server = new HttpServer(config);
config.Routes.MapODataServiceRoute("AdventureWorksV3", "AdventureWorks/v3", builder.GetEdmModel(), new DefaultODataBatchHandler(server));
//config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Batch;
using System.Web.Http.OData.Extensions;
using ODataSample.Models;
using System.IO;
using System.Xml;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Microsoft.Data.Edm;
//using Microsoft.Data.Edm.Csdl;
using Microsoft.Data.Edm.Validation;
using Microsoft.Data.OData;
namespace ODataSample
{
using Microsoft.Restier.WebApi;
using Microsoft.Restier.WebApi.Batch;
using Controllers;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
RegisterAdventureWorksV4(config, GlobalConfiguration.DefaultServer);
RegisterAdventureWorksV3(config, GlobalConfiguration.DefaultServer);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static async void RegisterAdventureWorksV4(HttpConfiguration config, HttpServer server)
{
await config.MapODataDomainRoute<AdventureWorksController>(
"AdventureWorksV4", "AdventureWorks/v4",
new ODataDomainBatchHandler(server));
}
/**/
public static void RegisterAdventureWorksV3(HttpConfiguration config, HttpServer server)
{
/*
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Address>("Addresses");
builder.EntitySet<AddressType>("AddressTypes");
builder.EntitySet<BusinessEntity>("BusinessEntities");
builder.EntitySet<BusinessEntityContact>("BusinessEntityContacts");
builder.EntitySet<BusinessEntityAddress>("BusinessEntityAddresses");
builder.EntitySet<EmailAddress>("EmailAddresses");
builder.EntitySet<Person>("People");
builder.EntitySet<PersonPhone>("PersonPhones");
builder.EntitySet<PhoneNumberType>("PhoneNumberTypes");
builder.EntitySet<Password>("Passwords");
builder.EntitySet<StateProvince>("StateProvinces");
builder.EntitySet<CountryRegion>("CountryRegions");
builder.EntitySet<ContactType>("ContactTypes");
//HttpServer server = new HttpServer(config);
config.Routes.MapODataServiceRoute("AdventureWorksV3", "AdventureWorks/v3", builder.GetEdmModel(), new DefaultODataBatchHandler(server));
*/
IEdmModel model = GetEdmModel<AdventureWorksContext>();
config.Routes.MapODataServiceRoute("AdventureWorksV3", "AdventureWorks/v3", model, new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
}
public static IEdmModel GetEdmModel<T>() where T : DbContext, new()
{
IEdmModel model;
DbContext context = new T();
string contextName = new Stack<string>(context.ToString().Split('.')).Pop();
using (MemoryStream stream = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(stream))
{
EdmxWriter.WriteEdmx(context, writer);
writer.Close();
stream.Seek(0, SeekOrigin.Begin);
using (XmlReader reader = XmlReader.Create(stream))
{
model = Microsoft.Data.Edm.Csdl.EdmxReader.Parse(reader);
//LS pukes when there's no m:IsDefaultEntityContainer="true" attribute on the EntityContainer node
IEdmEntityContainer defaultContainer = model.FindDeclaredEntityContainer(contextName);
ODataUtils.SetIsDefaultEntityContainer(model,defaultContainer , true);
}
}
}
return model;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment