- Notifications
You must be signed in to change notification settings - Fork 8
REST Controllers examples
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; }; } }In case of BookEntity we are not using Context, example of Context usage in factory for create many-2-many relations see here
- Declare a manager class with a minimal amount of code
public class BookManager : EfModelManager<ModelContext, BookDto, BookEntity, int, EmptyAdditionalFilters> { public BookManager(ModelContext dbContext, Func<CodeEntity, IDictionary<string, string>, bool> filterFunc, Func<BookEntity, BookDto> createResFunc, Func<BookDto, ModelContext, BookEntity> createObjFunc, Action<BookDto, int, ModelContext, BookEntity> updateObjFunc, ILoggerFactory loggerFactory) : base(dbContext, filterFunc, createFunc, createObjFunc, updateObjFunc, 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; } }Full CRUD controller (BasicCrudController) = basic read controller (BasicReadController) + Create, Update, and Delete operations :
-
POST /api/[controller]- for a new object creation -
PUT /api/[controller]/{id}- for edit an object by id -
DELETE /api/[controller]/{id}- for delete an object by id
Consider the same Book Resources, all what we need:
- Change controller base class to
BasicCrudController
[ApiController] public class BookController : BasicCrudController<BookDto, BookEntity, int, EmptyAdditionalFilters> { public BookController(BookManager manager) { Manager = manager; } }- Extend Factory class with 2 function to perform
CreateandUpdateoperations
public static class BookFactory { public static BookDto Create(BookEntity entity) { return new BookDto { Id = entity.Id, Title = entity.Title, Authors = entity.Authors; }; } public static BookEntity Create(BookDto dto, ModelContext context) { return new BookEntity { Id = dto.Id, Title = dto.Title, Authors = dto.Authors; }; } public static void Update(BookDto dto, int id, ModelContext context, BookEntity entity) { entity.Title = dto.Title; entity.Authors = dto.Authors; } }In case of BookEntity we are not using Context, example of Context usage in factory for create many-2-many relations see here
Pass Create && Update function to manager class:
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, BookFactory.Create, BookFactory.Update, loggerFactory) { } private readonly ModelContext _modelContext; }Full CRUD with Bulk operations (operations over multiple objects at once), Base class - BasicBulkCrudController = basic read controller (BasicReadController) + BulkCreate, BulkUpdate and BulkDelete operations, but all methods have route prefix - /api/bulk/:
-
POST /api/bulk/[controller]- for a multiple new objects creation -
PUT /api/bulk/[controller]- for editing multiple objects -
DELETE /api/bulk/[controller]/{idList}- for deleting multiple objects by ids.
Since version 4.0 Managers with EntityFramework have default BulkCreate and BulkUpdate methods; therefore, considering the previously defined Book resource, the only thing we need is to change the Controller base class to BasicBulkCrudController :
[ApiController] public class BookController : BasicBulkCrudController<BookDto, BookEntity, int, EmptyAdditionalFilters> { public BookController(BookManager manager) { Manager = manager; } }