-
Notifications
You must be signed in to change notification settings - Fork 8
REST Controllers examples
Ushakov Michael edited this page Sep 18, 2025
·
17 revisions
This page contains examples of how to Create REST Controllers with Wissance.WebApiToolkit.
This controller class contains the following methods:
-
GET /api/[controller]/?[page={page}&size={size}&sort={sort}&order={order}]to get collection ofPagedDataDto<T> -
GET /api/[controller]/{id}to get one object byid
Consider we have to create the Book Controller with EntityFramework:
- Create a book entity and implement the
IModelIdentifiable<T>interface.
public class BookEntity : IModelIdentifiable<int>
{
public int Id {get; set;}
public string Title {get; set;}
public string Authors {get; set;} // for simplicity
public DateTimeOffset Created {get; set;}
public DateTimeOffset Updated {get; set;}
}ModelContext Must derive from DbContext and have appropriate DbSet<BookEntity>
public interface IModelContext
{
DbSet<BookEntity> Books {get;set;}
}
public ModelContext: DbContext<ModelContext>, IModelContext
{
// todo: not mrntioned here constructor, entity mapping and so on
public DbSet<BookEntity> Books {get; set;}
}- Create a
DTO- BookDto
public class BookDto
{
public int Id {get; set;}
public string Title {get; set;}
public string Authors {get; set;}
}- Create a factory function
public static class BookFactory
{
public static BookDto Create(BookEntity entity)
{
return new BookDto
{
Id = entity.Id,
Title = entity.Title,
Authors = entity.Authors;
};
}
}- Declare a manager class with a minimal amount of code
public class BookManager : EfModelManager<BookDto, BookEntity, int, EmptyAdditionalFilters>
{
public BookManager(ModelContext dbContext, Func<CodeEntity, IDictionary<string, string>, bool> filterFunc, Func<BookEntity, BookDto> createFunc,
ILoggerFactory loggerFactory)
: base(dbContext, filterFunc, createFunc, null, null, loggerFactory)
{
}
private readonly ModelContext _modelContext;
}- Declare a controller class with also a minimal (like a Manager above) amount of code:
[ApiController]
public class BookController : BasicReadController<BookDto, BookEntity, int, EmptyAdditionalFilters>
{
public BookController(BookManager manager)
{
Manager = manager;
}
}...tbd
...tbd