How many times have you written the same List/Get/Create/Update/Delete controller code? In a typical ASP.NET Core API, this boilerplate multiplies fast – 75 controllers doing essentially the same thing. Hazina’s Generic API Framework ends this repetition.
The Old Way
// You'd write this 75 times...
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet] public async Task<IActionResult> List(...) { ... }
[HttpGet("{id}")] public async Task<IActionResult> Get(int id) { ... }
[HttpPost] public async Task<IActionResult> Create(ProductDto dto) { ... }
[HttpPut("{id}")] public async Task<IActionResult> Update(int id, ProductDto dto) { ... }
[HttpDelete("{id}")] public async Task<IActionResult> Delete(int id) { ... }
}
The Hazina Way
[Route("api/[controller]")]
public class ProductsController : GenericEntityController<Product>
{
public ProductsController(IRepository<Product> repo) : base(repo) { }
}
That’s it. You now have:
GET /api/products– List with pagination, filtering, sortingGET /api/products/{id}– Get by IDPOST /api/products– CreatePUT /api/products/{id}– Full updatePATCH /api/products/{id}– Partial updateDELETE /api/products/{id}– Soft deletePOST /api/products/bulk– Bulk createDELETE /api/products/bulk– Bulk deleteGET /api/products/count– CountGET /api/products/exists/{id}– Check existence
Entity Definition
using Hazina.API.Generic.Entities;
public class Product : EntityBase
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public string? Description { get; set; }
}
The EntityBase class gives you Id, CreatedAt, and UpdatedAt automatically.
Registration
// In Program.cs
builder.Services.AddDbContext<AppDbContext>(options => ...);
builder.Services.AddGenericEntityApi<AppDbContext>();
// In the pipeline
app.UseAuthentication();
app.UseGenericEntityApi(); // After auth
Built-in Pagination and Filtering
GET /api/products?page=1&pageSize=20&sort=price&order=desc&filter=name:contains:phone
No extra code needed. Pagination, sorting, and filtering come free with every entity.
In the next post, we’ll explore the different entity base classes and when to use each one.
Frequently Asked Questions
Hazina Generic API allows developers to create a full CRUD API with just one line of code by using a GenericEntityController. This eliminates the need to write repetitive controller code for each entity, significantly reducing development time and boilerplate code.
Hazina Generic API comes with built-in pagination, filtering, and sorting capabilities for every entity without requiring additional code. Developers can easily manage data retrieval with features like bulk create, soft delete, and existence checks.
The EntityBase class in Hazina Generic API automatically includes properties like Id, CreatedAt, and UpdatedAt for entities. This streamlines the development process by providing essential metadata for managing entities without extra code.
To register Hazina Generic API, add the necessary services in Program.cs by configuring the DbContext and calling AddGenericEntityApi. Then, ensure to use the middleware with `app.UseGenericEntityApi()` after authentication in the request pipeline.