4 Versioned ODataModelBuilder
Chris Martinez редактировал(а) эту страницу 2020-11-03 12:36:51 -08:00

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

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

Model Builder Factory

The ModelBuilderFactory property defines a factory function used to initialize 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()
};

Using camel-casing for JSON documents is very common. Beginning 3.0, EnableLowerCamelCase() is automatically called.

Model Configurations

The ModelConfigurations property is a collection of IModelConfiguration objects which define the configuration of one or more models to be applied 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()
    }
};

In ASP.NET Core, IModelConfiguration instances are automatically discovered through Dependency Injection when you declare IEnumerable<IModelConfiguration> or VersionedODataModelBuilder as a dependent parameter. The ModelConfigurations property can be modified after injection, if required.

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, routePrefix )
    {
        // 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 is 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 ODataModelBuilder via the ModelBuilderFactory
    • Invoke IModelConfiguration.Apply for each item defined in ModelConfigurations, including the DefaultModelConfiguration, 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.