Becoming a software developer – episode XVIII

Becoming a software developer – episode XVIII

Welcome to the eighteenth episode of my course “Becoming a software developer” in which we will finalize the basic CRUD for the Driver type, implement extension methods for the repository and build custom middleware in order to deal with exceptions.

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


 

Scope

  • Extensions
  • Middleware

Abstract

Extensions

I did write about extensions method already, but this time, I’d like to present how they can be used along with repository. Let’s consider the following scenario:

Quite often, we want to fetch e.g. driver within some application service and use it for some specific use case. Do we have to write the same code over and over again? Of course not, we could create a very simple extension methods:

And then reduce the repetitive code to the single line:

Middleware

ASP.NET Core framework is built around the middleware, which means that you can easily plug into the so-called pipeline and do pretty much whatever you want with the incoming HTTP request. This great feature can be used, for example for creating a global handler that will deal with the exceptions:

We can define a separate extension to make this integration look really cool

And eventually use it within Startup class Configure() method like this:

Next

In the next episode, we will deal with gracefully handling errors and extend the logging services by using NLog.

3 Comments Becoming a software developer – episode XVIII

  1. Pingback: Dew Drop - May 25, 2017 (#2487) - Morning Dew

Leave A Comment

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