2.5: Add DTO and AuthController
Add UserForRegister DTO
To register, a user will enter their username and password. They obviously won't enter their ID stored on our database, so we'll need a data transfer object (DTO) to only send the username and password.
In your .Models
project - add a folder called User
.
Inside of that folder, add a class called UserForRegister.cs
.
Add AuthController
Inside of our .API
project - create a new class called AuthController.cs
inside of the Controllers
folder.
First, we'll identify the route we'd like to use for this controller:
Next, we'll write our constructor.
The interesting thing to note here is that we don't have to tell our constructor about the concrete implementation of our IAuthService
interface.
The controller will accept anything that implements the interface - we just have to register what we want to use in the ConfigureService() method in our Startup class - and our 'container' will inject what is needed.
Dependency Injection is hopefully starting to click!
As we've done before - ctrl + . on authService and choose "Initialize field from parameter."
Last updated