Skip to content

REST API with one line of code

Ushakov Michael edited this page Sep 22, 2025 · 8 revisions

Introduction

This page describes how to use and what limitations Wissance.WebApiToolkit has when we are speaking about the one-line controller feature . Adding a controller in one line occurs without declaring a controller class, thereby we are free from declaring new types in the application and save our time on writing code.

Limitations

This feature imply following limitations:

  1. At the current version, the 1-line controller feature is working only with the EntityFaramework related manager class.
  2. For this feature, we are using SimplifiedEfModelManager therefore DTO and Entity are the same class. This simplification reduces factory parameters. However, we will add one more option to pass the factory in a separate method too see
  3. Temporarily we are not having authorization for one line controller until this issue is resolved

How to use

  1. Prepare Entity class implemented IModelIdentifiable<T>
  2. Get a Controller Assembly via the AddSimplifiedAutoController and pass:
    • Class that derives DbContext (EntityFramework)
    • Name of Resource = Name of a Controller without the "Controller" suffix that is using in route -> api/[Controller]
    • enum that specifies controller type: ReadOnly|FullCrud|Bulk

Example of an instantiation of a controller in one line, split to 2 for the better visual perception:

private void ConfigureWebApi(IServiceCollection services)
{
    ServiceProvider provider = services.BuildServiceProvider();
    // in one line
    Assembly stationControllerAssembly = services.AddSimplifiedAutoController<StationEntity, Guid, EmptyAdditionalFilters>(provider.GetRequiredService<ModelContext>(), "Station", ControllerType.FullCrud, null, provider.GetRequiredService<ILoggerFactory>());
            
    services.AddControllers().AddApplicationPart(sensorControllerAssembly).AddControllersAsServices();
}

The full example is here

Clone this wiki locally