How it works
Magicalizer does its work in two places: when the application starts, it finds your classes and creates the services and the controllers for them; when a request comes, it passes it through the layers, from the DTO to the entity and back.
At startup
AddMagicalizer and the start of ASP.NET Core:
- The services. Each model that implements
IModel<TEntity, TFilter>gets a service,IService<TKey, TModel, TFilter>, registered in the dependency injection: the defaultService<...>or your class that implements this interface (usually by deriving fromService<...>). - The validators. Each FluentValidation validator, of a DTO or of a model, is registered.
- The controllers. When ASP.NET Core builds its controllers, each DTO marked with
[Magicalized]gets one: the defaultMagicalizedController<...>or your class derived from it. Its route, HTTP methods, authorization, and OpenAPI settings come from the attributes of the DTO. - The diagnostics. The log tells which DTOs are served, by which controllers, with which methods, and which classes are skipped and why.
The generic classes are closed over your types (MagicalizedController<int, Product, Api.Dto.Product, ProductFilter>),
so there is no code generation: these are the same controllers and services you would write, only written once. Then
UseMagicalizer maps the controllers. See Type discovery for the rules of
finding the classes.
A request
flowchart LR
C(["Client"]) <-->|JSON| A["Controller<br/><b>DTO</b>"]
A <-->|"ToModel()<br/>new DTO(model)"| S["Service<br/><b>Model</b>"]
S <-->|"ToEntity()<br/>new Model(entity)"| E["EF Core<br/><b>Entity</b>"]
E <-->|SQL| DB[("Database")]
The controller binds and validates the request, the service validates the model and builds the query with the prefilters, and EF Core runs it.
Reading a list
GET /v1/products?category.name.equals=Pizza&sort=-price&limit=10&include=photos:
- The query string is checked. An unknown parameter or condition is an error, as the response would have the objects the client didn’t ask for.
- The query is bound. The conditions fill
ProductFilter, andsortandincludebecome the property paths of the model, checked against its properties. Thelimitis checked against the maximum one. All the errors go into one400 Bad Request. - The controller calls the service:
GetAllAsync(filter, sortings, offset, limit, inclusions). - The service builds one query of EF Core: the query prefilters, then the filter, the sorting, the paging, and the inclusions. The query doesn’t track the entities.
- The entities become the models by the constructors of the models (
new Product(productEntity)). - The total count is a second query with the same prefilters and filter, unless the client or the options skip it.
- The models become the DTOs by the constructors of the DTOs (
new Api.Dto.Product(productModel)) and are serialized to JSON.
The filter, the sorting, and the inclusion are translated by EF Core into SQL, so the database does the filtering, not
the application. The property paths (category.name) are the same in the model and the entity, as the filter and the
model follow the properties of the entity.
Writing
POST /v1/products with a JSON body:
- The body becomes the DTO, and the DTO validator checks it with the
DefaultandCreaterule sets (EditforPUTandPATCH). The errors are400 Bad Request. - The DTO becomes the model (
dto.ToModel()), and the controller callsCreateAsyncof the service. - The service validates the model with its own validator, if there is one, the same way.
- The model becomes the entity (
model.ToEntity()), and EF Core saves it. - If the database rejects it, the service tells why: a duplicate primary key (
409 Conflict), a reference to an object that doesn’t exist (400 Bad Request), or, on deletion, an object other ones refer to (409 Conflict). - The saved entity goes back as the model and the DTO:
201 Createdwith theLocationheader.
PUT, PATCH, and DELETE go the same way. They first find the object through the query prefilters, so an object the
prefilters hide is 404 Not Found, as for reading.
Why the three layers
Each layer has its own class, so each of them can change without the others:
- the entity follows the database (foreign keys like
CategoryId, columns, navigation properties); - the model follows the business logic and is what the services and your code work with;
- the DTO is the contract of the API, which the clients depend on, and it can hide or rename what the model has.
The conversions between them are the plain constructors and methods you write (ToModel, ToEntity), so there is no
mapping library and no hidden conventions: what you see is what’s converted. See
Packages and layers for how the layers map to the projects.