Hazina Generic API: Build a Full CRUD API in One Line of Code
Hazina Framework

Hazina Generic API: Build a Full CRUD API in One Line of Code

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, sorting
  • GET /api/products/{id} – Get by ID
  • POST /api/products – Create
  • PUT /api/products/{id} – Full update
  • PATCH /api/products/{id} – Partial update
  • DELETE /api/products/{id} – Soft delete
  • POST /api/products/bulk – Bulk create
  • DELETE /api/products/bulk – Bulk delete
  • GET /api/products/count – Count
  • GET /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

How does Hazina Generic API simplify CRUD operations in ASP.NET Core?

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.

What built-in features does Hazina Generic API provide?

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.

What is the EntityBase class in Hazina Generic API?

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.

How do I register Hazina Generic API in my ASP.NET Core application?

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.

Terug naar overzicht

Frequently Asked Questions

What is the main advantage of using Hazina's Generic API Framework? +

The main advantage is that it eliminates the need to write repetitive CRUD controller code for each entity, allowing developers to create a fully functional API with just one line of code.

How does the Hazina way of defining a controller differ from the traditional ASP.NET Core approach? +

In the Hazina way, you define a controller by inheriting from GenericEntityController, which automatically provides all necessary CRUD operations, whereas the traditional approach requires explicitly writing each operation in separate methods.

What built-in features does the Hazina Generic API provide without additional coding? +

The Hazina Generic API offers built-in pagination, sorting, and filtering for every entity, which means developers do not need to implement these features manually.

What is the purpose of the EntityBase class in the Hazina framework? +

The EntityBase class automatically provides properties such as Id, CreatedAt, and UpdatedAt for entities, simplifying the definition of data models.

What are some of the endpoint functionalities included with the GenericEntityController? +

Included functionalities are listing with pagination, filtering, sorting, getting by ID, creating, full and partial updates, soft deletes, bulk creation, bulk deletion, counting, and checking existence of entities.

ENNL