Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded dynamic model creation sample #67

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@ pip-log.txt
.DS_Store
# DB files
*.mdf
*.ldf
*.ldf
.vs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,56 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.13.0.0" newVersion="6.13.0.0" />
<bindingRedirect oldVersion="0.0.0.0-7.4.1.20214" newVersion="7.4.1.20214" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.13.0.0" newVersion="6.13.0.0" />
<bindingRedirect oldVersion="0.0.0.0-7.4.1.20214" newVersion="7.4.1.20214" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.13.0.0" newVersion="6.13.0.0" />
<bindingRedirect oldVersion="0.0.0.0-7.4.1.20214" newVersion="7.4.1.20214" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Linq.Expressions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Linq" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" /></startup></configuration>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System.Web.Http;
using System.Web.OData.Extensions;

namespace DynamicEdmModelCreation
namespace DynamicEdmModelCreation
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
DynamicModelHelper.CustomMapODataServiceRoute(
config.Routes,
"odata",
"odata");
config.AddODataQueryFilter();
}
}
using System.Web.Http;
using System.Web.OData.Extensions;

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableDependencyInjection();

config.CustomMapODataServiceRoute("odata", "odata");

config.AddODataQueryFilter();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,113 +1,113 @@
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;
using DynamicEdmModelCreation.DataSource;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Library;

namespace DynamicEdmModelCreation.Controllers
namespace DynamicEdmModelCreation.Controllers
{
public class HandleAllController : ODataController
{
// Get entityset
public EdmEntityObjectCollection Get()
{
// Get entity set's EDM type: A collection type.
ODataPath path = Request.ODataProperties().Path;
IEdmCollectionType collectionType = (IEdmCollectionType)path.EdmType;
IEdmEntityTypeReference entityType = collectionType.ElementType.AsEntity();
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Extensions;
using DynamicEdmModelCreation.DataSource;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
using ODataPath = System.Web.OData.Routing.ODataPath;

public class HandleAllController : ODataController
{
// Get entityset
public EdmEntityObjectCollection Get()
{
// Get entity set's EDM type: A collection type.
ODataPath path = this.Request.ODataProperties().Path;
IEdmCollectionType collectionType = (IEdmCollectionType)path.EdmType;
IEdmEntityTypeReference entityType = collectionType.ElementType.AsEntity();

// Create an untyped collection with the EDM collection type.
EdmEntityObjectCollection collection =
new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType));

// Add untyped objects to collection.
DataSourceProvider.Get((string) this.Request.Properties[Constants.ODataDataSource], entityType, collection);

return collection;
}

// Get entityset(key)
public IEdmEntityObject Get(string key)
{
// Get entity type from path.
ODataPath path = this.Request.ODataProperties().Path;
IEdmEntityType entityType = (IEdmEntityType)path.EdmType;

// Create an untyped entity object with the entity type.
EdmEntityObject entity = new EdmEntityObject(entityType);

DataSourceProvider.Get((string) this.Request.Properties[Constants.ODataDataSource], key, entity);

return entity;
}

public IHttpActionResult GetName(string key)
{
// Get entity type from path.
ODataPath path = this.Request.ODataProperties().Path;

if (path.PathTemplate != "~/entityset/key/property")
{
return this.BadRequest("Not the correct property access request!");
}

PropertySegment property = path.Segments.Last() as PropertySegment;
IEdmEntityType entityType = property.Property.DeclaringType as IEdmEntityType;

// Create an untyped entity object with the entity type.
EdmEntityObject entity = new EdmEntityObject(entityType);

DataSourceProvider.Get((string) this.Request.Properties[Constants.ODataDataSource], key, entity);

object value = DataSourceProvider.GetProperty((string) this.Request.Properties[Constants.ODataDataSource], "Name", entity);

if (value == null)
{
return this.NotFound();
}

string strValue = value as string;
return this.Ok(strValue);
}

public IHttpActionResult GetNavigation(string key, string navigation)
{
ODataPath path = this.Request.ODataProperties().Path;

if (path.PathTemplate != "~/entityset/key/navigation")
{
return this.BadRequest("Not the correct navigation property access request!");
}

NavigationPropertySegment property = path.Segments.Last() as NavigationPropertySegment;
if (property == null)
{
return this.BadRequest("Not the correct navigation property access request!");
}

IEdmEntityType entityType = property.NavigationProperty.DeclaringType as IEdmEntityType;

EdmEntityObject entity = new EdmEntityObject(entityType);

DataSourceProvider.Get((string)this.Request.Properties[Constants.ODataDataSource], key, entity);

object value = DataSourceProvider.GetProperty((string)this.Request.Properties[Constants.ODataDataSource], navigation, entity);

if (value == null)
{
return this.NotFound();
}

IEdmEntityObject nav = value as IEdmEntityObject;
if (nav == null)
{
return this.NotFound();
}

// Create an untyped collection with the EDM collection type.
EdmEntityObjectCollection collection =
new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType));

// Add untyped objects to collection.
DataSourceProvider.Get((string)Request.Properties[Constants.ODataDataSource], entityType, collection);

return collection;
}

// Get entityset(key)
public IEdmEntityObject Get(string key)
{
// Get entity type from path.
ODataPath path = Request.ODataProperties().Path;
IEdmEntityType entityType = (IEdmEntityType)path.EdmType;

// Create an untyped entity object with the entity type.
EdmEntityObject entity = new EdmEntityObject(entityType);

DataSourceProvider.Get((string)Request.Properties[Constants.ODataDataSource], key, entity);

return entity;
}

public IHttpActionResult GetName(string key)
{
// Get entity type from path.
ODataPath path = Request.ODataProperties().Path;

if (path.PathTemplate != "~/entityset/key/property")
{
return BadRequest("Not the correct property access request!");
}

PropertyAccessPathSegment property = path.Segments.Last() as PropertyAccessPathSegment;
IEdmEntityType entityType = property.Property.DeclaringType as IEdmEntityType;

// Create an untyped entity object with the entity type.
EdmEntityObject entity = new EdmEntityObject(entityType);

DataSourceProvider.Get((string)Request.Properties[Constants.ODataDataSource], key, entity);

object value = DataSourceProvider.GetProperty((string)Request.Properties[Constants.ODataDataSource], "Name", entity);

if (value == null)
{
return NotFound();
}

string strValue = value as string;
return Ok(strValue);
}

public IHttpActionResult GetNavigation(string key, string navigation)
{
ODataPath path = Request.ODataProperties().Path;

if (path.PathTemplate != "~/entityset/key/navigation")
{
return BadRequest("Not the correct navigation property access request!");
}

NavigationPathSegment property = path.Segments.Last() as NavigationPathSegment;
if (property == null)
{
return BadRequest("Not the correct navigation property access request!");
}

IEdmEntityType entityType = property.NavigationProperty.DeclaringType as IEdmEntityType;

EdmEntityObject entity = new EdmEntityObject(entityType);

DataSourceProvider.Get((string)Request.Properties[Constants.ODataDataSource], key, entity);

object value = DataSourceProvider.GetProperty((string)Request.Properties[Constants.ODataDataSource], navigation, entity);

if (value == null)
{
return NotFound();
}

IEdmEntityObject nav = value as IEdmEntityObject;
if (nav == null)
{
return NotFound();
}

return Ok(nav);
}
}
return this.Ok(nav);
}
}
}
Loading