Introduction
In DDD (Domain-Driven Design) architectures, developers often need to manually create API controllers after implementing service layers. Plus.AutoApi eliminates this step by dynamically generating RESTful-style WebAPIs without requiring Controller classes.
Quick Start
1. Install NuGet Package
Add the package to your application service layer:
| |
2. Service Registration
Configure in Startup.cs:
| |
3. Implement Service
Create service classes implementing IAutoApi with [AutoApi] attribute:
| |
Key Features:
- Naming Convention: Services ending with
Service/ApplicationServiceauto-generate APIs - Custom Endpoints: Use standard ASP.NET Core attributes like
[HttpGet("{id}")] - Opt-Out: Disable generation with
[AutoApi(Disabled = true)]
Swagger Integration
Enable OpenAPI documentation:
| |
HTTP Method Convention
Default verb mapping based on method names:
| Method Prefix | HTTP Verb |
|---|---|
| Add/Create/Post | POST |
| Get/Find/Fetch | GET |
| Update/Put | PUT |
| Delete/Remove | DELETE |
Example API Routes
Generated endpoints for the WeatherService:
GET /api/weatherGET /api/weather/{id}POST /api/weatherPUT /api/weather/{id}DELETE /api/weather/{id}
Resources
Key Benefits
- Zero Controller Boilerplate Eliminate manual Controller class creation
- Coexistence Support Works alongside traditional controllers
- Customizable Rules Override default naming conventions through configuration options
- Swagger Ready Full OpenAPI specification support
- DDD Alignment Focus on domain logic rather than infrastructure code
Note: This approach works best for CRUD-heavy applications. Complex endpoints may still require traditional controllers.
