simpleapi.core
1.0.4
dotnet add package simpleapi.core --version 1.0.4
NuGet\Install-Package simpleapi.core -Version 1.0.4
<PackageReference Include="simpleapi.core" Version="1.0.4" />
paket add simpleapi.core --version 1.0.4
#r "nuget: simpleapi.core, 1.0.4"
// Install simpleapi.core as a Cake Addin #addin nuget:?package=simpleapi.core&version=1.0.4 // Install simpleapi.core as a Cake Tool #tool nuget:?package=simpleapi.core&version=1.0.4
simple-api project
NuGet: NuGet Gallery
simple-api Project is an attempt to create a framework for creating very transparent in terms of code and efficient API-type applications. In accordance with the principle of Domain Driven Design, each endpoint is a separate class, which allows for very simple work on the entire project.
The framework allows you to
- Creating API interface (http/https)
- Middlewares implementations
- Dependency injection *
- Simple UI for testing and creating documentation *.
- Web page construction *
Implementation
Construction of the API interface
If you want to start a server
IApp app = App.Init<Program>(5050)
app.Run();
Construction of Endpoints
[Api("/example", Method.GET)]
public static class ExampleEndpoint
{
public class Command
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
}
public class Handler : IEndpoint<Command, int>
{
public async Task<int> Handle(Command command)
{
//TODO all features
return command.A + command.B + command.C;
}
}
}
Command is a class that has a request body in it if it is a GET it will be taken from the parameters in the URL, if it is a method that has a body it will be built from JSON
Construction of Middleware
if you want to create middleware you must instantiate the class
public class ExampleMiddleware : IMiddleware
{
public async Task<HttpListenerContext> Invoke(HttpListenerContext ctx)
{
Console.WriteLine("TODO somethink cool");
return ctx;
}
}
And then you have two options
Register middleware witch runs before endpoint actions
IApp app = App.Init<Program>(5050) .RegisterPreMiddleware<ExampleMiddlewareBeforeEndpoint>()
Register middleware witch runs after endpoint actions
IApp app = App.Init<Program>(5050) .RegisterPostMiddleware<ExampleMiddlewareAfterEndpoint>()
Services
The sites are divided into Singletons and Multi sites. Singletons hundred objects having only one instance, built at the start of the project. Type of service can occur only once during registration
Register Singleton Service
if you want to add a singleton service to your application you must add to the app object
Option 1: Use interface and type of object
IApp app = App.Init<Program>(5050) .RegisterSingletonService<IExampleService, ExampleService>();
Option 2: Use Type of interface and ready instance
var serivce = new ExampleService(); IApp app = App.Init<Program>(5050) .RegisterSingletonService<IExampleService>(service);
Use only instance without interface
var serivce = new ExampleService(); IApp app = App.Init<Program>(5050) .RegisterSingletonService<IExampleService>(service);
Multi Services
The implementation of multiservices has 2 variants
- Use instance type and interface to create per all requests
IApp app = App.Init<Program>(5050) .RegisterMultiService<IExampleService, ExampleService>();
- Or use only instance type
IApp app = App.Init<Program>(5050) .RegisterMultiService<ExampleService>();
Use Services
In order to use the registered services in the Handler class of a given endpoint, you must create a constructor containing the registered services
[Api("/example", Method.GET)]
public static class ExampleEndpoint
{
public class Command
{
}
public class Handler : IEndpoint<Command, int>
{
readonly IExampleService _exampleService;
public Handler(IExampleService exampleService)
{
_exampleService = exampleService;
}
public async Task<int> Handle(Command command)
{
return _exampleService.TwoPlusTwo();
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
alpha