Skip to content

Commit

Permalink
Add EdmBuilder.cs and its Breeze.EdmBuilder nuget package
Browse files Browse the repository at this point in the history
  • Loading branch information
wardbell committed Feb 25, 2014
1 parent 8bd28a6 commit 0358ea1
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Breeze.Client/Scripts/Labs/EdmBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Data.Entity;
using System.IO;
using System.Xml;
using Microsoft.Data.Edm.Csdl;

namespace Microsoft.Data.Edm
{
/// <summary>
/// DbContext extension that builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/>
/// </summary>
/// <remarks>
/// We need the EDM both to define the Web API OData route and as a
/// source of metadata for the Breeze client.
/// The Web API OData literature recommends the
/// <see cref="System.Web.Http.OData.Builder.ODataConventionModelBuilder"/>.
/// That component is suffient for route definition but fails as a source of
/// metadata for Breeze because (as of this writing) it neglects to include the
/// foreign key definitions Breeze requires to maintain navigation properties
/// of client-side JavaScript entities.
/// <p>This EDM Builder ask the EF DbContext to supply the metadata which
/// satisfy both route definition and Breeze.</p>
/// </remarks>
public static class EdmBuilder
{
/// <summary>
/// Builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/>
/// </summary>
/// <example>
/// /* In the WebApiConfig.cs */
/// using (var context = new TodoListContext())
/// {
/// config.Routes.MapODataRoute(
/// routeName: "odata",
/// routePrefix: "odata",
/// model: context.GetEdm(),
/// batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)
/// );
/// }
/// </example>
/// <param name="dbContext">The source <see cref="DbContext"/></param>
/// <returns>An XML <see cref="IEdmModel"/> </returns>
public static IEdmModel GetEdm(this DbContext dbContext)
{
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(stream))
{
System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(dbContext, writer);
}
stream.Position = 0;
using (var reader = XmlReader.Create(stream))
{
return EdmxReader.Parse(reader);
}
}
}
}
}
20 changes: 20 additions & 0 deletions Nuget.builds/Breeze.EdmBuilder/Breeze.EdmBuilder.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Breeze.EdmBuilder</id>
<version>1.0.0</version>
<title>Breeze Labs: EdmBuilder</title>
<authors>Ward Bell</authors>
<owners>Ward Bell</owners>
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl>
<projectUrl>http://www.breezejs.com/breeze-labs/breezeedmbuilder</projectUrl>
<iconUrl>http://www.breezejs.com/sites/all/images/breezeb.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext. This EDM can produce the metadata Breeze needs to manage a corresponding JavaScript entity model on the client including the foreign key properties necessary to maintaining associations. This builder replaces the ODataConventionModelBuilder in a Web API OData application. See the comments in the code for an explanation and usage. See it in action in
the "Breeze Web API OData Sample".</description>
<summary>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext.</summary>
<copyright>Copyright © 2014 IdeaBlade</copyright>
<language>en-US</language>
<tags>breeze webapi odata</tags>
</metadata>
</package>
20 changes: 20 additions & 0 deletions Nuget.builds/Breeze.EdmBuilder/Default.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Breeze.EdmBuilder</id>
<version>1.0.0</version>
<title>Breeze Labs: EdmBuilder</title>
<authors>Ward Bell</authors>
<owners>Ward Bell</owners>
<licenseUrl>http://www.opensource.org/licenses/mit-license.php</licenseUrl>
<projectUrl>http://www.breezejs.com/breeze-labs/breezeedmbuilder</projectUrl>
<iconUrl>http://www.breezejs.com/sites/all/images/breezeb.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext. This EDM can produce the metadata Breeze needs to manage a corresponding JavaScript entity model on the client including the foreign key properties necessary to maintaining associations. This builder replaces the ODataConventionModelBuilder in a Web API OData application. See the comments in the code for an explanation and usage. See it in action in
the "Breeze Web API OData Sample".</description>
<summary>Generates an "Entity Data Model" (EDM) from an Entity Framework DbContext.</summary>
<copyright>Copyright © 2014 IdeaBlade</copyright>
<language>en-US</language>
<tags>breeze webapi odata</tags>
</metadata>
</package>
58 changes: 58 additions & 0 deletions Nuget.builds/Breeze.EdmBuilder/content/App_Data/EdmBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Data.Entity;
using System.IO;
using System.Xml;
using Microsoft.Data.Edm.Csdl;

namespace Microsoft.Data.Edm
{
/// <summary>
/// DbContext extension that builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/>
/// </summary>
/// <remarks>
/// We need the EDM both to define the Web API OData route and as a
/// source of metadata for the Breeze client.
/// The Web API OData literature recommends the
/// <see cref="System.Web.Http.OData.Builder.ODataConventionModelBuilder"/>.
/// That component is suffient for route definition but fails as a source of
/// metadata for Breeze because (as of this writing) it neglects to include the
/// foreign key definitions Breeze requires to maintain navigation properties
/// of client-side JavaScript entities.
/// <p>This EDM Builder ask the EF DbContext to supply the metadata which
/// satisfy both route definition and Breeze.</p>
/// </remarks>
public static class EdmBuilder
{
/// <summary>
/// Builds an "Entity Data Model" (EDM) from a <see cref="DbContext"/>
/// </summary>
/// <example>
/// /* In the WebApiConfig.cs */
/// using (var context = new TodoListContext())
/// {
/// config.Routes.MapODataRoute(
/// routeName: "odata",
/// routePrefix: "odata",
/// model: context.GetEdm(),
/// batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)
/// );
/// }
/// </example>
/// <param name="dbContext">The source <see cref="DbContext"/></param>
/// <returns>An XML <see cref="IEdmModel"/> </returns>
public static IEdmModel GetEdm(this DbContext dbContext)
{
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(stream))
{
System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(dbContext, writer);
}
stream.Position = 0;
using (var reader = XmlReader.Create(stream))
{
return EdmxReader.Parse(reader);
}
}
}
}
}

0 comments on commit 0358ea1

Please sign in to comment.