efradix-pg-webapi
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet new install efradix-pg-webapi::1.0.0
This package contains a .NET Template Package you can call from the shell/command line.
dotnet-efradix-pg-webapi
A dotnet new template for scaffolding a layered ASP.NET Core WebAPI backed by PostgreSQL using EFRadix.Core.Postgres.
Features
- Layered architecture: Api → Sdk → Repository → Common
- EFRadix source-generated repository context (
IAppRepositoryContext) - PostgreSQL via Npgsql + EF Core
- JWT Bearer authentication (JwtAdapter)
- API versioning (URL segment + header)
- Swagger UI + ReDoc
- Mapster for object mapping
- Global exception handler middleware
- Sample
EmployeeCRUD to illustrate the full pattern
Installation
dotnet new install efradix-pg-webapi
Usage
dotnet new efradix-pg-webapi -n MyService
This generates:
MyService/
├── src/
│ ├── APIs/MyService.Api/
│ ├── Commons/MyService.Common/
│ ├── SDKs/MyService.Sdk/
│ └── Repositories/MyService.Repository/
├── tests/
├── docs/
└── MyService.sln
Project responsibilities
| Layer | Namespace | Responsibilities |
|---|---|---|
| Common | MyService.Common |
Entities (extend BaseEntity), DTOs, Options, Helpers |
| Repository | MyService.Repository |
AppDbContext, EF Migrations, Entity Configurations |
| Sdk | MyService.Sdk |
Service interfaces + implementations (business logic) |
| Api | MyService.Api |
Controllers, Middlewares, Program.cs |
Configuration
Update appsettings.json in the Api project:
{
"ConnectionStrings": {
"DbConnection": "Server=127.0.0.1;Port=5432;User Id=postgres;Password=postgres;Database=MyServiceDb;"
},
"BearerTokenConfig": {
"Audience": "my-service-api",
"Issuer": "https://localhost:5001",
"SigningKey": "your-signing-key-here"
}
}
Running migrations
Migrations run automatically on startup via:
await app.RunMigrationsAsync<AppDbContext>();
To add a new migration manually:
dotnet ef migrations add <MigrationName> \
--project src/Repositories/MyService.Repository \
--startup-project src/APIs/MyService.Api
EFRadix patterns
Entities extend BaseEntity (provides Id as string Guid + CreatedAt):
public class Product : BaseEntity
{
public required string Name { get; set; }
}
DbContext extends BaseNpgsqlDbContext<T>:
public class AppDbContext(DbContextOptions<AppDbContext> options)
: BaseNpgsqlDbContext<AppDbContext>(options)
{
public DbSet<Product> Products { get; set; }
}
Inject the source-generated repository context in your services:
public class ProductService(IAppRepositoryContext repo) : IProductService
{
public async Task<Product?> GetByIdAsync(string id)
=> await repo.ProductRepository.GetByIdAsync(id);
}
Publishing the template to NuGet
# Pack
dotnet pack DotnetEfRadixPgTemplate.TemplatePackage.csproj -o ./nupkg
# Push
dotnet nuget push ./nupkg/dotnet-efradix-pg-webapi.*.nupkg \
--api-key <YOUR_NUGET_API_KEY> \
--source https://api.nuget.org/v3/index.json
Uninstalling
dotnet new uninstall dotnet-efradix-pg-webapi
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.