Skip to content

REST Controllers examples

Ushakov Michael edited this page Sep 18, 2025 · 17 revisions

1 Introduction

This page contains examples of how to Create REST Controllers with Wissance.WebApiToolkit.

1.1 ReadOnly REST Controller

This controller class contains the following methods:

  • GET /api/[controller]/?[page={page}&size={size}&sort={sort}&order={order}] to get collection of PagedDataDto<T>
  • GET /api/[controller]/{id} to get one object by id

Consider we have to create the Book Controller with EntityFramework:

  1. 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;}
}
  1. Create a DTO - BookDto
public class BookDto
{
    public int Id {get; set;}
    public string Title {get; set;}
    public string Authors {get; set;} 
}
  1. 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;
        };
    }
}
  1. 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;
}
  1. 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;
    }
}

1.2 FullCRUD REST Controller

...tbd

1.3 BULK REST Controller

...tbd

Clone this wiki locally