Minimal .net core framework based on three tier architecture, can be used as template for building API's
I've built this framework mostly for myself with all needed features out of the box, but this foundation can be more modular to have more potential use cases.
Example route with validation
Get("/api/v1/item/get", _ => {
var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
new ExistsInTable("item_guid", "items", "guid"),
});
if (errors.Count > 0) {
return HttpResponse.Errors(errors);
}
return HttpResponse.Item("item", new ItemTransformer().Transform(
ItemRepository.FindByGuid(Request.Query["item_guid"])
));
});
Example controller with middleware (check JWT token)
public class ItemCrudController : BaseController {
protected override IMiddleware[] Middleware() => new IMiddleware[] {
new JwtMiddleware()
};
public ItemCrudController() {
Post("/api/v1/item/create", _ => {
var errors = ValidationProcessor.Process(Request, new IValidatorRule[] { });
if (errors.Count > 0) {
return HttpResponse.Errors(errors);
}
var item = ItemRepository.CreateAndGet((string) Request.Query["title"], (float) Request.Query["price"]);
return HttpResponse.Item("item", new ItemTransformer().Transform(item));
});
}
}
Example transformer
public class ItemTransformer : BaseTransformer {
public override JObject Transform(object obj) {
var item = (ItemModel) obj;
return new JObject {
["guid"] = item.guid,
["title"] = item.title,
["price"] = item.price,
};
}
}
Supported databases: PostgreSQL
- .net core > 2.2
- database (PostgreSql only)
- PHP > 7.0 (for migrations)
Foundation of the framework is located in framework-base-core and loaded as submodule into Base project
Database:
- Auth: JWT(jwt-dotnet)
- WebServer: Nancy
- ORM: Dapper(StackExchange)
- Migrations: Phinx(CakePHP)
- build project
- copy config.example.json into:
- For main app
- App/bin/%BUILD_TYPE%/netcoreapp2.2/config/config.json
- For unit tests
- Tests/bin/%BUILD_TYPE%/netcoreapp2.2/config/config.json Example: App/bin/Debug/netcoreapp2.2/config/config.json
- edit config files - fill db name / user / etc.
Migrating
- copy migrations/phinx.example.yml to migrations/phinx.yml
- edit phinx.yml - fill database user / password etc.
- install php & composer dependencies from migrations/composer.json
- run migrations (in migrations folder):
php vendor/bin/phinx migrate
- to migrate with default database (development)
php vendor/bin/phinx migrate -e testing
- to migrate with test database
Building & running project
You should be able to build with dotnet build
and run app via for example: dotnet App/bin/Debug/netcoreapp2.2/App.dll
Thank you for considering contributing to this repo. Feel free to submit any improvements / issues / refactoring / documentation etc.