A simple .Net API using the CQRS + Mediator pattern
- C# - Microsoft.
- C# Extensions -jchannon.
- NuGet Gallery - pcislo.
- SQLite - alexcvzz.
Everything starts with the domain.
- We first must create our domain entity classes.
Has a dependency on the domain and sits above the Domain
- Responsbile for business logic.
- Do validation here.
Has dependency on Application layer.
- Responsible for apis/controllers (interfacing).
Has a dependency on domain so it has access to entities that scaffold our database. Application has dependeny on persistence so that it can make db changes.
- Everything is centered around the domain.
dotnet --info - get installation info.
dotnet -h - get list of commands we can use.
dotnet new -l - get list of types of projects we can make.
dotnet new sln - create a new solution project (contains all our projects), invokes the current folder you are in to be an sln.
dotnet new webapi -n API - creates a web api project called API
dotnet new classlib -n Application - creates a new class library (standard project) with name.
dotnet new classlib -n Domain
dotnet new classlib -n Persistence
dotnet sln add API Application Domain Persistence - add the projects to the solution (looks for the .csproj files in those folders).
dotnet sln list - see what projects are in the sln.
cd into the folders to add dependencies to other projects and execute the respective commands.
(API) dotnet add reference ../Application
(Application) dotnet add reference ../Domain and dotnet add reference ../Persistence
(Persistence) dotnet add reference ../Domain
(API) dotnet run - will run the application.
(API) dotnet watch run - will run the application in watch mode.
To setup code style where initialise field from parameter gives a result like _config = config in constructors, search the settings for keywords this (to remove the this) and private to change the private field prefix.
Must install dotent-ef dotnet entity framework to create migrations.
dotnet tool list --globalwill show what global tools you have installed. if you do not have dotnet ef listed there you will need to install it: https://www.nuget.org/packages/dotnet-ef/6.0.0-preview.5.21301.9- Install the package globally:
dotnet tool install --global dotnet-ef --version 6.0.0-preview.5.21301.9 dotnet ef -hlist executeable commands. To migrate you can execute this at the sln level.
(You will need to install Microsoft.EntityFrameworkCore.Design on Persistence proj from nuget for this to work)
dotnet ef migrations add InitialCreate -p Persistence -s APIdotnet ef migrations add <MigrationName> -p <PersistenceProj>/ <APIProj>/
This will create Persistence.Migrations.
dotnet ef database -hto see what commands we can use for databases.
Has a dependency on the Application layer to which the API layer has a dependecy on it.
This allows us to have an interface in the Application layer and the actual implementation in the
infrastructure project, this keeps the Application layer clear of needing to know about http or the user
or any auth context but still access that logic from another project. In this case we want the user from auth
context GetUsername()
- The goal here is that if we change how auth works in the layer above, then because the Application layer was never concerned with this, no code change is required in the App layer, only API and infrastructure.
dotnet has not by convention supported this. It is supported now but not recommended because the join tables created by ef do not accomodate extra fields on the tables.
To migrate down in dotnet-ef: dotnet ef migrations remove -p Persistence -s API
- For things that are cross-cutting concerns we can use an Infrastructure project.
The related model is included with the model when queried using the Include() syntax.
When a related model is included with the model the first time when the property is accessed.
- You turn it on/off at a very high level. Bit of a deal breaker.
We can call the Load() method on the model to load the related entities. We explicitly use lazy laoding.
Allow us to get our related models directly.
- Allows us to create more efficient queries.