WebApiUtilities 1.0.4
dotnet add package WebApiUtilities --version 1.0.4
NuGet\Install-Package WebApiUtilities -Version 1.0.4
<PackageReference Include="WebApiUtilities" Version="1.0.4" />
paket add WebApiUtilities --version 1.0.4
#r "nuget: WebApiUtilities, 1.0.4"
// Install WebApiUtilities as a Cake Addin #addin nuget:?package=WebApiUtilities&version=1.0.4 // Install WebApiUtilities as a Cake Tool #tool nuget:?package=WebApiUtilities&version=1.0.4
WebApiUtilities
WebApiUtilities is a library that contains tools to facilitate the rapid development of RESTful ASP .Net Core 3.1 Web APIs utilising Entity Framework Core, Fluent Validation, Odata and Swagger.
Getting set up
- Make a new ASP .Net Core 3.1 Web API project and install the WebApiUtilities nuget package.
- For each model you need to add the following classes
- Model which inherits
Entity<TId>
ORAuditableEntity<TId>
- DTO which inherits
Dto<TModel, TId>
and contains the same properties ad TModel - DTO validator which inherits
DtoValidator<TDto, Dto>
where Dto is the DTO made previously - Model configuration which inherits
IEntityTypeConfiguration<TModel>
- Optional: If you want to be able to create the model via the API then a Create command which inherits the DTO made previously and
ICreateCommand<TModel, TId>
- Optional: If you want to be able to update (PUT) the model via the API then an Update Command which inherits the DTO made previously and
IUpdateCommand<TModel, TId
- A controller which inherits either
CrudController<TModel, TId, TCreateComman, TUpdateCommand>
ORReadOnlyController<TModel, TId>
- Model which inherits
- Make a DbContext which inherits either
AuditingDbContext
ORDbContext
- In the ConfigureServices method of Startup.cs configure your DbContext then add the following line.
services.AddWebApiServices(ApiTitle, ApiVersion);
- In the Configure method of Startup.cs add the following
app.AddWebApiUtilities(GetEdmModel(), MaxTop, ApiVersion);
- In Startup.cs add the following method
IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
//Duplicate this line for each entity
odataBuilder.EntitySet<TModel>(nameof(TModel));
return odataBuilder.GetEdmModel();
}
Adding endpoints
WebAppUtilities utilises MediatR to maintain SOLID principles so it is recommended that for each new endpoint a IRequest and IRequestHandler is made and then in the controller you give the desired request to the mediatr.
To add new endpoints for a model add a new method to the CrudController/ReadOnlyController, decorate it with the desired HTTP method and send the Mediatr the request you made for the endpoint.
To add non entity related endpoints make a new controller which inherits ApiController. This will give to access to the Mediatr to send your request to.
Endpoints
- API endpoints are available at
/api/<TModel>/{id}
- The Swagger UI is available at
/swagger
- Odata querying is available at
/api/<TModel>?$<query string>
Other features
IClock an abstraction around DateTime.Now which is by default bound to the Ioc container at startup and can be injected where needed and mocked in tests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
.NETCoreApp 3.1
- AutoMapper (>= 10.0.0)
- AutoMapper.Extensions.Microsoft.DependencyInjection (>= 7.0.0)
- FluentValidation (>= 9.0.1)
- FluentValidation.AspNetCore (>= 9.0.1)
- MediatR (>= 8.0.2)
- MediatR.Extensions.Microsoft.DependencyInjection (>= 8.0.1)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 3.1.6)
- Microsoft.AspNetCore.OData (>= 7.4.1)
- Microsoft.AspNetCore.OData.Versioning (>= 4.1.1)
- Microsoft.EntityFrameworkCore (>= 5.0.0-preview.6.20312.4)
- Microsoft.EntityFrameworkCore.InMemory (>= 5.0.0-preview.6.20312.4)
- Microsoft.EntityFrameworkCore.SqlServer (>= 5.0.0-preview.6.20312.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0-preview.6.20305.6)
- Microsoft.OData.Edm (>= 7.7.0)
- Newtonsoft.Json (>= 12.0.3)
- Swashbuckle.AspNetCore (>= 5.5.1)
- System.Linq (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.