this simple library provide you with ready to go Repository
and Controller
without the need to write code,
so that you can focus on what's important rather than the small tedious details.
dotnet add package AutoCrud
to create repository you need to extends AutoCrudRepository
public class BookRepository : AutoCrudRepository<Book, int>
{
private readonly LibraryDbContext _dbContext;
public BookRepository(LibraryDbContext dbContext) : base(dbContext)
{
_dbContext = dbContext;
}
protected override DbSet<Book> GetDbSet()
{
return _dbContext.Books;
}
protected override int GetPrimaryKey(Book book)
{
return book.Id;
}
}
and to create controller you need to extends AutoCrudContrller
or AutoCrudControllerAsync
[ApiController]
[Route("books")]
public class BookController : AutoCrudController<Book, int>
{
public BookController(AutoCrudRepository<Book, int> repository, ILogger logger) :
base(repository, logger)
{
}
protected override int GetPageSize()
{
return 10;
}
protected override string GetCreatedEntityUri(Book book)
{
return "/books/" + book.Id;
}
protected override void SetPrimaryKeyValueToEntity(Book book, int id)
{
book.Id = id;
}
}
that's all you need to have crud actions for your entities, these endpoints will be available
GET /books/:id
GET /books
POST /books
PUT /books/:id
DELETE /books/:id
for more details see