Skip to content

Versioned ODataModelBuilder

Chris Martinez edited this page Jul 29, 2016 · 4 revisions

The VersionedODataModelBuilder is a builder of builders, which enables creating an Entity Data Model (EDM) for each service API version.

public class VersionedODataModelBuilder
{
    public VersionedODataModelBuilder( HttpConfiguration configuration ) { }
    public Func<ODataModelBuilder> ModelBuilderFactory { get; set; }
    public Action<ODataModelBuilder, ApiVersion> DefaultModelConfiguration { get; set; }
    public IList<IModelConfiguration> ModelConfigurations { get; }
    public Action<ODataModelBuilder, IEdmModel> OnModelCreated { get; set; }
    public virtual IEnumerable<IEdmModel> GetEdmModels();
}

Model Builder Factory

The ModelBuilderFactory property defines a factory function used to initial a new ODataModelBuilder for each service API version. The default value creates a new instance of the ODataConventionModelBuilder. You can update this property to substitute your own ODataModelBuilder or provide a custom initialization setup.

var modelBuilder = new VersionedODataModelBuilder( configuration )
{
    ModelBuilderFactory = () => new ODataConventionModelBuilder().EnableLowerCamelCase()
};

Model Configurations

The ModelConfigurations property is a collection of IModelConfiguration objects which define the configuration of one or more models to be apply for each API version. Although it's not required, it's recommended that you create one IModelConfiguration per entity model.

var modelBuilder = new VersionedODataModelBuilder( configuration )
{
    ModelConfigurations =
    {
        new PersonModelConfiguration()
    }
};

Default Model Configuration

The DefaultModelConfiguration property defines a callback that can be used to apply a default model configuration. Specifying a callback is useful if you have a configuration that applies to all models or if you want to have a single, inline model configuration.

var modelBuilder = new VersionedODataModelBuilder( configuration )
{
    DefaultModelConfiguration = ( builder, apiVersion )
    {
        // TODO: default configuration for all models
    }
};

On Model Created

The OnModelCreated property is a callback that serves the same purpose as ODataConventionModelBuilder.OnModelCreated. This callback can be used to perform any additional setup or configuration required after each EDM model is created.

Get EDM Models

The GetEdmModels method behavior similar to the ODataModelBuilder.GetEdmModel method. This method performs the following actions:

  • Discover and enumerate each service API version
  • For each service API version:
    • Create an ODataModelBuidler via the ModelBuilderFactory
    • Invoke IModelConfiguration.Apply for each item defined in ModelConfigurations with the current model builder and API version
    • Invoke ODataModelBuilder.GetEdmModel to generate the current EDM model
    • Apply the ApiVersionAnnotation with the current API version to the generated EDM model
    • Invoke OnModelCreated with the current current model builder and generated EDM model, if defined

The return value of this method is typically used to provided the EDM models provided to one of the MapVersionedODataRoutes extension methods.

Clone this wiki locally