Still Working on
RestFul API with .NET Core
This project is just to test the knowledge with the .NET Core´s API and entity framework core concepts.
Which techs/patterns/frameworks are being used in the project?
- C#
- ASP NET Core
- Entity Framework Core
- Sql Lite
- XUnit
- JWT Authentication
- Cache Store
- Fluent Assertions
- Polly Retry Policy
- Unit Of Work
- Circuit Breaker
- Decorator Pattern
- K6 API Loading Testing (Stress, Spike, Load, Soak)
MVCS Structure, but without the View since this project is not about frontend concepts.
The business layers can be find inside the services folder.
Configurations about database it´s in the the AppDbContext.cs on the method OnConfiguring.
We can see that it´s configured to be cache shared. It´s because we in this way we can share the same cache in memory for every thread, reducing the numbers of connections in database.
We also can see the DbSet that are used to inform which classes are tables in our database.
Each model represents a table in database, which each propertie represents a column.
Here we have our controller named as "UserController" that in inherits from "ControllerBase". That´s because doing this we can use some return types such as BadRequest() or Ok() that help us to make a restful Api.
We can also see some Dependency injection.
That´s possible because it´s mapped in the Startup class.
It´s was choosed to use AddScoped because since the same instance keep´s from each web request, makes sense to use in a controller.
Backing to our controller we can see that we are calling our business layer by the variable called "_services" that it´s being populated by DI.
One cool thing that we can see in the image above is the routing.
As we can see we are doing routing in a different way, as an example, "skip:int". We could have done in the normal way such as [FromRoute] but doing in this way it´s forces the consumer to pass a value with the type that we are expecting. If the consumer passes a value such as string instead a int he is going to get a 404 error.
Here we have the IService interface that provides the methods, and the class that implements. We also have the folder called ViewModel, that has the DTO classes.
Quick example of the interface
We can se in the image above that the class implements the interface and also has a DI.
We have some classes for custom erros that we use to throw inside the business class to the controller layers.
- Unit of work
- Repository Pattern
- Better tests
- Fluent Validation instead data annotation on models
- Change DI AddScope dependencies to AddTransient
- Better use of the SOLID principle
- Clean Archiecture
Clone the project where you want to save it
Open the solution
Go to the package manager console
Run the command
dotnet-ef database update
Run the command
nuget restore
Open cmd inside the TrackerApi Folder
Run the command
dotnet watch run
MVCS Shared Cache Sql Lite Dependency Injection Types of DI Data Annotation Xunit With Moq Xunit With Moq Moq MVC With Asp Net Core Relationships in Entity Framework Core More Relationships in Entity Framework Core Even More Relationships in Entity Framework Core