Becoming a software developer – episode XVI

Becoming a software developer – episode XVI

Welcome to the sixteenth episode of my course “Becoming a software developer” in which we will implement the login endpoint in our API, discuss the caching mechanism and how to initialize the application with basic data.

All of the materials including videos and sample projects can be downloaded from here.
The source code repository is being hosted on GitHub.


 

Scope

  • Login
  • Seed

Abstract

Login

Once we have JWT authentication in place, the final step is to allow the users to actually get the valid token via exposed login endpoint. In order to do so, we can define a simple Login command along with its handler, like this:

As you might have already noticed, I’m using the IMemoryCache which allows using caching mechanism in our application. Whenever you want to cache something, always think in terms how big the data would be (server has a limited memory), does it change too often or not and, is it a costly to e.g. fetch it from a remote resource (database, service) and is it going to be accessed by the end users quite often.

Back to the point, eventually, we can create a controller that will consume and handle the Login command. You might be wondering what do we need caching here for? Since we’re following the CQS (Command & Query Separation) pattern, our command handlers do not return any values. Thus, we need to store our token in some place (it could be e.g. a real database, but in this case, it’s just a memory) in order to fetch it and return to the user.

Seed

Quite often, we’d like to have some initial data, so we can easily browse or test the API. We can delegate such task to a specialized service that usually exposes a method called Seed().

We may also go one step further and include some application settings (e.g. a boolean flag) in order to control whether the application should initialize the data or not.

Next

In the next episode we will keep on implementing our business logic and findining out both, the boundaries and responsibilities of the different services and how they should interact with each other.

3 Comments Becoming a software developer – episode XVI

  1. child in time

    Nice episode. However I don’t think that injecting logger into each class is a good practice. In my opinion it’d be better to use simply private static field instead of defining additional constructor dependency.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *