Kull.GenericBackend 0.13.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Kull.GenericBackend --version 0.13.0
                    
NuGet\Install-Package Kull.GenericBackend -Version 0.13.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kull.GenericBackend" Version="0.13.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kull.GenericBackend" Version="0.13.0" />
                    
Directory.Packages.props
<PackageReference Include="Kull.GenericBackend" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kull.GenericBackend --version 0.13.0
                    
#r "nuget: Kull.GenericBackend, 0.13.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kull.GenericBackend@0.13.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kull.GenericBackend&version=0.13.0
                    
Install as a Cake Addin
#tool nuget:?package=Kull.GenericBackend&version=0.13.0
                    
Install as a Cake Tool

The Kull Generic Backend

This package allows Integration of a generic Stored Procedure-based Backend to Asp.Net MVC Core It uses Swashbuckle, Version 5+

Configuration

In Startup.cs, add the following services:

using Kull.GenericBackend;
// ...

public void ConfigureServices(IServiceCollection services)
{
		services.AddMvcCore().AddApiExplorer(); //Or AddMvc() depending on your needs
		services.AddGenericBackend(null, new Kull.GenericBackend.SwaggerGeneration.SwaggerFromSPOptions() {

		});
		
		// You have to inject a DbConnection somehow
        services.AddTransient(typeof(DbConnection), (s) =>
        {
            var conf = s.GetRequiredService<IConfiguration>();
            var constr = conf["ConnectionStrings:DefaultConnection"];
            return new System.Data.SqlClient.SqlConnection(constr);
        });
		services.AddSwaggerGen(c=> {
			c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
			c.AddGenericBackend();
		});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	app.UseSwagger(o =>
    {
		// For compat with ng-swagger-gen on client. You can use ng-openapi-gen if set to false
        o.SerializeAsV2 = true;
    });
    app.UseMvc(routeBuilder=>
    {
		// The package relies on integrated routing of Asp.net MVC Core
        app.UseGenericBackend(routeBuilder);
    });
    // If needed, Swagger UI
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

}

In appsettings.json, set the connection string and the URI's:

{
  "ConnectionStrings": {
    "Default": "Data Source=SOMESERVER;Initial Catalog=SOMEDB;Integrated Security=True"
  },
  "Entities": {
    "Cases": {
      "Get": "api.spGetSomeCases"
    },
    "Cases/{CaseId|int}/Brands": {
      "Get": "api.spGetBrands",
      "Post": "api.spAddUpdateBrands"
    }
  }
}

In the "Entities" Section, the URL's are configured. Each entry correspondends to a URL. Each URL can be accessed by multiple HTTP Methods. Allowed are:

  • GET for getting data, shoud NEVER update something
  • POST for adding/updating. Maybe misused for getting data if the url gets longer then 2000 chars otherwise)
  • PUT for updating data
  • DELETE for deleting data

In the URL there can be route constraints as in MVC, however the | is used instead of : because of limitations of appsettings.json For a full documentation of allowed route constraints, please see here

For best practices for defining a REST Api, see here

Multi-value parameters

To use multiple values for a parameter, use a Sql Server User Defined Table Type for the parameter.

-- Create the data type
CREATE TYPE dbo.IdNameType AS TABLE 
(
	Id bigint, 
	Name nvarchar(1000), 
    PRIMARY KEY (Id)
)
GO
-- Create SP
CREATE PROCEDURE dbo.spTestBackend
	@SomeId int,
	@Ids dbo.IdNameType readonly
AS
BEGIN
	-- ...
END

Main parts of the generic API

Middleware for handling the requests

The main part of the project is the Middleware that handles stored procedures. It is responsible for handling a request against some URL defined in the Entities Section, as seen above.

Middleware for Swagger

The other part is the middleware that is responsible for generating a swagger.json file out of the information of the database. This is done by using the sp_describe_first_result_set and INFORMATION_SCHEMA.parameters

It works by adding an IDocumentFilter to Swashbuckle. Swashbuckle generates the swagger.json This means you can mix this backend and your own Controllers and it will just work in the swagger.json.

Special parameters

There are so called System Parameters, which are parameters defined in any Stored Procedure which should be resolved by the Webserver and not by the Consumer of the API. Built-in are the following System Parameters:

  • ADLogin & NTLogin: The Username in the HttpContext
  • IPAddress: The IP Adress of the User
  • UserAgent: The UserAgent of the Browser

If you would like to add something to this, you can Subclass the SystemParameters class and replace the default implementation by using Asp.Net Core Dependency Injection.

Possible futher development

  • Port to .Net Core 3.0
  • Direct manipulation of tables/views without Stored Procedures
  • Support for other databases, eg Sqlite for Testing
  • More Unit Tests
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Kull.GenericBackend:

Package Downloads
Kull.GenericBackend.OData

Package Description

Kull.GenericBackend-Sanitizer

Package Description

Kull.GenericBackend.Sanitizer

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.6.0-alpha 293 12/4/2023
2.5.4 1,956 11/29/2022
2.5.3 422 11/29/2022
2.5.2 420 11/29/2022
2.5.1 440 11/29/2022
2.5.0 525 11/22/2022
2.4.2 571 11/18/2022
2.4.1 552 11/17/2022
2.4.0-beta5 329 9/22/2022
2.4.0-beta4 255 9/22/2022
2.4.0-beta3 283 9/8/2022
2.4.0-beta2 311 7/19/2022
2.4.0-beta1 267 7/19/2022
2.3.0 979 7/1/2022
2.2.0-beta9 280 6/14/2022
2.2.0-beta8 696 5/27/2022
2.2.0-beta7 319 5/4/2022
2.2.0-beta6 323 2/17/2022
2.2.0-beta4 319 2/17/2022
2.2.0-beta3 351 1/19/2022
2.2.0-beta2 387 1/18/2022
2.2.0-beta1 343 1/14/2022
2.1.4 1,008 1/13/2022
2.1.3 611 1/12/2022
2.1.2 787 1/11/2022
2.1.1 1,008 12/3/2021
2.1.0 1,141 12/3/2021
2.1.0-beta1 886 12/1/2021
2.0.3 477 12/1/2021
2.0.2 472 12/1/2021
2.0.1 451 12/1/2021
2.0.0 461 11/30/2021
2.0.0-beta9 5,050 11/24/2021
2.0.0-beta8 417 11/13/2021
2.0.0-beta7 542 10/25/2021
2.0.0-beta6 349 10/25/2021
2.0.0-beta5 404 10/25/2021
2.0.0-beta4 346 10/6/2021
2.0.0-beta3 405 10/5/2021
2.0.0-beta2 394 9/30/2021
1.5.3 585 8/13/2021
1.5.2 517 8/11/2021
1.5.1 557 8/9/2021
1.5.0 630 8/1/2021
1.4.4 641 8/1/2021
1.4.3 598 7/29/2021
1.4.2 688 4/13/2021
1.4.1 541 4/13/2021
1.4.0 566 4/6/2021
1.3.9 591 2/11/2021
1.3.8 719 1/25/2021
1.3.7 581 1/15/2021
1.3.6 586 1/15/2021
1.3.5 708 12/8/2020
1.3.4 666 12/8/2020
1.3.2 804 9/9/2020
1.3.1 694 9/9/2020
1.3.0 684 8/19/2020
1.2.4 696 8/18/2020
1.2.3 714 8/12/2020
1.2.2 697 8/12/2020
1.2.1 678 7/16/2020
1.2.0 687 7/16/2020
1.1.0 797 6/1/2020
1.1.0-beta3 537 5/25/2020
1.1.0-beta2 493 5/20/2020
1.1.0-beta1 480 5/20/2020
1.1.0-alpha3 514 5/20/2020
1.1.0-alpha2 503 5/20/2020
1.1.0-alpha1 507 5/20/2020
1.0.0-rc3 501 5/19/2020
1.0.0-rc2 491 5/14/2020
1.0.0-rc1 469 5/14/2020
1.0.0-beta3 534 5/6/2020
1.0.0-beta2 511 5/4/2020
1.0.0-beta1 520 4/30/2020
0.15.4 828 3/18/2020
0.15.3 749 3/18/2020
0.14.2 836 11/22/2019
0.14.1 1,403 10/2/2019
0.13.5 780 9/25/2019
0.13.4 791 9/12/2019
0.13.3 755 9/11/2019
0.13.2 779 9/10/2019
0.13.1 770 9/10/2019
0.13.0 817 8/14/2019