Show / Hide Table of Contents

Model Providing

Model Providing is the fourth step in the request flow. It occurs after authentication and before filtering. A Model Provider is used to get an IQueryable containing all of the models of the data source. Exactly one model provider must be attached to a route. If no model providers are provided, an exception of type ApiException is thrown for every request on that route.

Note

Even though the IQueryable objects returned by Model Providers can be used to enumerate every object in the data source, Seltzr's default Model Providers return IQueryable objects that are only evaluated when ToArray or an equivalent method is called. This means that routes that have many filters or don't use the provided models wont incur a performance penalty.

Builder Methods

Low-Level Methods

These methods are primarily used when you've implemented your own IModelProvider<TModel, TUser> and want to attach it to a route.

  • UseModelProvider(IModelProvider<TModel, TUser>)
  • UseModelProvider<TProvider>()

High-Level Methods

There are no high-level builder methods for adding common Model Providers. Instead you'll typically set a Model Provider when first creating the API. See Examples for more information.

Examples

Using the Entity Framework Model Provider

Both the Seltzr.EntityFramework and Seltzr.EntityFrameworkCore packages include a Model Provider that gets the DbSet<TEntity> for the API's model. Their extension methods on IApplicationBuilder will use this Model Provider automatically.

Seltzr.EntityFramework
// Get all of the `MyModel` objects from `MyDbContext`
app.AddEntityFrameworkSeltzr<MyModel, MyDbContext>(api => {
	api.Get();
});
Seltzr.EntityFrameworkCore
// Get all of the `MyModel` objects from `MyDbContext`
app.AddEFCoreSeltzr<MyModel, MyDbContext>(api => {
	api.Get();
});

Attaching a custom provider to a route

The following example sets up an API that responds to a GET request at the /random endpoint with a list of random numbers.

C#
app.UseSeltzr<int>("random", api => {
	api
		.UseModelProvider<RandomNumbersProvider>()
		.CanGet();
});
  • Improve this Doc
Back to top Generated by DocFX