Fully functional REST Controller with one line in .Net




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:

  1. 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 the IModelManager interface and do not depend on specific technology at all;
  2. Fully Swagger-friendly, and we could display any set of Swagger parameters automatically;
  3. Contains default implementation of Create, Update, and Delete methods with Entity Framework;
  4. Contains implementation of Bulk Create, Bulk Update and Bulk Delete methods for Entity Framework implementation;
  5. Contains an extension that allows you to create any type of controller: ReadOnly (2 GET methods), FullCRUD, or even Bulk with EntityFramework in just one line of code;
  6. Contains implementation and examples how to use Manager classes with GRPC services therefore it is easy to switch from REST to GRPC 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();
}
Enter fullscreen mode

Exit fullscreen mode

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 generic DbContext 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 implement IModelIdentifiable with property public 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.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *