MVC design pattern
there is some correlation to this pattern and the concept of 'data driven' apps that I haven't fully grasped.
for APIs and serving web pages
what is the mapper for?
Entities
Classes I use to represent real world things... Person, Animal, Match, Apple... these classes usually have boilerplate getters and setters for their properties
Controllers
Controllers are classes that map URL routes to the Model or data access layer functions that handle business logic for fetching and processing data.
for ex: UserController routes and handles all the URLs related to Users
Controllers handle what to do on cases of failure or success of the called Model functions.
Never handles data logic.
Shouldn't contain much code.
after getting the data from the Model, the Controller interacts with the View to render the data.
services / repositories
service and repository are interchangeable names.
usually an interface for the class is made and then the actual class that implements the interface. The interface has the common crud functions like GetById(), Create, Update, Delete...
the service/repository class is a layer between the database context (that actually executes) the queries and more top level clients that call it.
I could for example also add a Authenticate function for a UserRepository
Models
I'm not sure if this is it how the concept is typically used as a standard but it's how the tutorial I'm currently following uses and defines Models for
he creates a CreateRequestModel which defines the parameters for incoming POST requests to the /users route and is attached to the route by setting it as the parameter to the Create
action method of the UsersController
he also creates a UpdateRequest model.
Auto Mapper
in a more general sense, type mapping is transforming one data type to another.
This is kinda especific the tutorial im following
But he uses a AutoMapper package that automatically maps User entities and CreateRequest models.
what this means is that using that package I would'nt have to manually set the properties that come from a Post request to a newly created entity obj.
This is to avoid the following:
void User myPostHandler(CreateRequestModel){
newUser = new User();
newUser.name = CreateRequestModel.name
...x100 this can get repetitive and boiler platey
}
error handling
this is completely relevant to the tutorial im following. probably good practice for .net world.
ErrorHandlerMiddleware.cs executes / delegates handling a http request. if it catches an error it handles it depending its type, it can be AppException or other types of errors. It catches them and respond with a different response code accordingly.
AppException.cs is for defining custom exceptions for throwing app specific exceptions that can be caught and handled withing the app.
View
only cares about presenting the data
template file that dynamically renders HTML.
frontend UI
The view never interacts with the Model