Skip to content

ahmad2smile/KurzSharp

Repository files navigation

KurzSharp

NuGet Publish Nuget Nuget GitHub

Easily scaffold non-production APIs in any or all of following Architectures:

  1. GraphQl (HotChocolate)
  2. Grpc (Code First protobuf-net.Grpc)
  3. REST (ASP.NET Core Controllers)

and Explore Data with:

AdminDashboard (using Blazor, Radzen Table and MudBlazor UI)

Minimum required dotnet version: .NET8

Usage

  1. Create any model with a partial class and add relevant Attribute for ex: RestApi
[GrpcApi] // Whicherver is desired
[RestApi]
[GraphQlApi]
[AdminDashboard]
public partial class Product
{
    public Guid Id { get; set; }
}
  1. Configure KurzSharp by default Data is stored in Memory with Entity Framework UseInMemoryDatabase
services.AddKurzSharp();

For Database add relavent Db package for Entity Framework and configure KurzSharp similar way to Entity Framework config. (ex: PostgresDb):

services.AddKurzSharp(o => o.UseNpgsql(configuration.GetConnectionString("ProductsDb")));

Map Routes and Services:

app.MapKurzSharpServices();

🎉 You API is ready, Run project and open Swagger Docs.

For GraphQl API open Bana Cake Pop at http://localhost:5114/graphql

image

For REST or Grpc API open Swagger at http://localhost:5114/swagger

image

For Admin Dashboard UI open http://localhost:5114/KurzSharp/ or sub page with /{EntityName}s.

image

For more information please check examples/TestApi

Features:

  • Hook into process to control how/what information on Entity is modified/observed with following hooks. Hook automatically attached the Model, just override the required ones.
    • TDto OnBeforeCreate(TDto dto)
    • IEnumerable<TDto> OnBeforeCreate(IEnumerable<TDto> dtos)
    • IQueryable<TDto> OnBeforeRead(IQueryable<TDto> dtos)
    • TDto OnBeforeRead(TDto dto)
    • TDto OnBeforeUpdate(TDto dto)
    • Enumerable<TDto> OnBeforeUpdate(IEnumerable<TDto> dto)
    • TDto OnBeforeDelete(TDto dto)
    • IEnumerable<TDto> OnBeforeDelete(IEnumerable<TDto> dto)

Ex:

[RestApi]
public partial class Product
{
    
    private readonly ILogger<Product> _logger;

    public Product(ILogger<Product> logger)
    {
        _logger = logger;
    }

    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;

    public override ProductDto OnBeforeCreate(ProductDto dto)
    {
        _logger.LogInformation("DI is working...");

        return dto;
    }
}

How it works:

When a KurzSharp attributes like RestApi, GraphQlApi or GrpcApi is added on some Model class, it creates a new Dto entity which has the same properties as the Model. The Model is used to store data through Entity Framework Core and Dto is sent over the wire through API. It also takes attributes from the properties and puts them on the Dto's properties for example: JsonIgnoreAttribute.

About

Quick API prototyping Library

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages