-
Notifications
You must be signed in to change notification settings - Fork 707
Versioned ODataModelBuilder
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> DefaultModelConfiguration { get; set; }
public IList<IModelConfiguration> ModelConfigurations { get; }
public Action<ODataModelBuilder, IEdmModel> OnModelCreated { get; set; }
public virtual IEnumerable<IEdmModel> GetEdmModels();
}
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.
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.
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
}
};
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.
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.
- Home
- Quick Starts
- Version Format
- Version Discovery
- Version Policies
- How to Version Your Service
- API Versioning with OData
- Configuring Your Application
- Error Responses
- API Documentation
- Extensions and Customizations
- Known Limitations
- FAQ
- Examples