What is it and what is it for
Now it is possible to significantly simplify WebAPI
development with the NET
platform (>6). During the last years I was developing my lib (Give us a STAR please if you find our solution interesting or useful) that allows reducing the amount of code to be written during application development. Among other template processors, this library has the following differences:
- Allows the use of any technology (framework) to work with persistent data (i.e.,
Entity Framework
,Dapper
, or whatever else) due to separated Core interfaces and one of the built-in implementations; controllers use theIModelManager
interface and do not depend on specific technology at all; - Fully
Swagger-friendly
, and we could display any set of Swagger parameters automatically; - Contains default implementation of
Create
,Update
, andDelete
methods withEntity Framework
; - Contains implementation of
Bulk Create
,Bulk Update
andBulk Delete
methods forEntity Framework
implementation; - Contains an extension that allows you to create any type of controller:
ReadOnly
(2GET
methods),FullCRUD
, or evenBulk
with EntityFramework in just one line of code; - Contains implementation and examples how to use Manager classes with
GRPC
services therefore it is easy to switch fromREST
toGRPC
or to have them both in same time with just one Manager class;
Generate REST
Controllers in one line
The full example could be find here but very briefly we have the following method that works with ServiceCollection:
private void ConfigureWebApi(IServiceCollection services)
{
services.AddSwaggerGen();
ServiceProvider provider = services.BuildServiceProvider();
Assembly stationControllerAssembly = services.AddSimplifiedAutoController<StationEntity, Guid, EmptyAdditionalFilters>(
provider.GetRequiredService<ModelContext>(), "Station",
ControllerType.FullCrud, null, provider.GetRequiredService<ILoggerFactory>());
Assembly measureUnitControllerAssembly = services.AddSimplifiedAutoController<MeasureUnitEntity, Guid, EmptyAdditionalFilters>(
provider.GetRequiredService<ModelContext>(), "MeasureUnit",
ControllerType.ReadOnly, null, provider.GetRequiredService<ILoggerFactory>());
Assembly sensorControllerAssembly = services.AddSimplifiedAutoController<SensorEntity, Guid, EmptyAdditionalFilters>(
provider.GetRequiredService<ModelContext>(), "Sensor",
ControllerType.FullCrud, null, provider.GetRequiredService<ILoggerFactory>());
Assembly measurementControllerAssembly = services.AddSimplifiedAutoController<MeasurementEntity, Guid, EmptyAdditionalFilters>(
provider.GetRequiredService<ModelContext>(), "Measurement",
ControllerType.Bulk, null, provider.GetRequiredService<ILoggerFactory>());
services.AddControllers().AddApplicationPart(sensorControllerAssembly).AddControllersAsServices();
services.AddControllers().AddApplicationPart(stationControllerAssembly).AddControllersAsServices();
services.AddControllers().AddApplicationPart(measureUnitControllerAssembly).AddControllersAsServices();
services.AddControllers().AddApplicationPart(measurementControllerAssembly).AddControllersAsServices();
}
for the visual indentation process of getting Assembly and load controllers from it is separated on 2 lines.
To implement the following approach, we have to pass:
-
DbContext
(Entity Framework), passing genericDbContext
allows you to use any database (Postgres
,MySql
,SqlServer
, …) - Controller name without suffix Controller
- One of enum value
ReadOnly
|FullCrud
|Bullk
- Filter function (could be null)
-
ILoggerFactory
for making logs in the controllers There is only one requirement — every entity class must implementIModelIdentifiable<T>
with propertypublic T Id {get;set;}
What could be done
One-line controllers add simplified logic, but here we lose some control, i.e., we don’t have DTO objects even though Generic IModelManager
does. If you would like to add custom methods to your controller, you should manually create a controller class (this is also very simple with ~10 lines of code; see i.e.). Authentication and authorization could be simply added via attributes, but in one-line controllers we don’t have yet (wait for the new releases).
Conclusion
This lib was developed by the author and his LLC. We also offer our services in quality enterprise software development; please don’t forget to GIVE us a STAR on GitHub.
Top comments (0)