Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

schmoli/service-template

Repository files navigation

Service Template

ASP.NET Core WebAPI Microservice Template with Postgres Db Backend. Non sevice-specific code is located in the Services.Core project. This could be included in all services or shared as a NuGet package. An xunit test project also exists for the Core project.

  • REST routes defined in the Controller classes accept and return Resource objects
  • A Swagger page is hosted by default at the application root
  • The resources are validated automatically using Validators
  • Paging of results is provided by use of the PagedRequest and PagedResultSet<T> classes. These may be enhanced to be specific to your domain.
  • The controllers map these resource objects to domain Models and pass them to Services.
  • The services use a UnitOfWork to interact with one or more Repositories within a single Transaction
  • Repositories can use the generic IRepository implementation Repository to get access to standard CRUDS operations. Additional operations may be added.

Issues / To-Do

There are some features that are not implimented yet:

  • Identity & Access Management
  • dotnet-new templates
  • devcontainer support

Patterns

  • UnitOfWork is used to allow single transactions to span multiple Repositories
  • FluentValidation is used to validate incoming Resource objects. This validation is displayed on the Swagger endpoint and helps enforce the Single Responsibility Principal (SRP) by keeping the validation logic out of the Resource object.
  • IEntityTypeConfiguration<T> is used to define the schema for each table using a fluent API. This is helps enforce the SRP.

Configuration

Configuration for this service is provided through the appSettings[Environment].json file. Additionally, configuration can be overwritten via environment variables. For example, given the below configuration, the port for the database connection can be changed to 4242 by setting the environment variable POSTGRES__PORT=4242 before running the application.

  "Postgres": {
    "Server": "localhost",
    "Port": 5433,
    "User": "yourusernamehere",
    "Password": "yourpasswordhere",
    "Database": "Schmoli.ServiceTemplate",
    "Options": "Application Name=Schmoli.ServiceTemplate"
  },

Service Contents

Controllers\

  • API Controllers
  • Inject Service and Mappers

Data\

  • EF Core DB Context
  • Configuration class for each table IEntityTypeConfiguration<Model>
    • Keeps model clear of any schema-related information (SRP)

Mappers\

  • Automapper Profile(s)
  • Map Resources <-> Entities and back

Migrations\

  • EF Core migrations, autogenerated code.

Models\

  • Service Domain Models
  • Used for transmision from Services to Repositories

Repositories\

  • Individual repositories for each model
  • UnitOfWork interface and concrete implementation
  • Base IRepository/Repository provide simple CRUDS operations
    • Only need to implement advanced searches and nested item includes

Requests\

  • Customized Request objects for models (custom sorting, etc)
  • Use simple PagedRequest when not needed

Resources\

  • Resource classes (previously DTO) used by controllers
  • NOTE: No DataAnnotations required

Sevices\

  • Interfaces and Implementations of Domain Services

Validators\

  • FluentValidation of Resources instead of DataAnnotations (SRP)

About

ASP.NET Core Microservice Template

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published