Becoming a software developer – episode XI

Becoming a software developer – episode XI

Welcome to the eleventh episode of my course “Becoming a software developer” in which we will use AutoMapper library, implement simple HTTP POST method and use asynchronous methods.

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


 

Scope

  • AutoMapper
  • HTTP POST
  • Async

Abstract

AutoMapper

As programmers, we want to simplify and automate as many things as possible. One of such activities might be transforming object of type A into B, for example User into UserDto. For sure, we can do it in a naive way, like this:

We can also do it in a better way, by extracting IMapper interface, for example:

Eventually, we can use external library, like AutoMapper which allows for a quick object mapping, as well as provides a rich configuration and settings.

HTTP POST

In order to create a new resource (for example user), we should create a new endpoint that supports HTTP POST operation. As the documentation states, POST is responsible for doing exactly this (unlike the GET for fetching the data or PUT for updating it). It’s very trivial to add the new HTTP POST endpoint within ASP.NET Core controller – just mark with the attribute HttpPost, provide a route path if needed and make sure to include FromBody attribute within a method parameter in order to bind incoming request to the defined type.

Async

When it comes to I/O (In/Out) operations, we should strive for making them asynchronous always when it’s possible. There’s no point to spend the valuable server resources and keep it busy only to wait till some external database or web service call finishes. This is why the asynchronicity was introduced in a first place. Perform a request, get a Task object in return and await it when you need the result of such operation. We will apply this pattern to our repositories, services and controllers.

Next

In the next episode, we’ll talk about HTTP Status Codes, Headers and write first unit and integration (end-to-end) tests.

1 Comment Becoming a software developer – episode XI

  1. Pingback: Dew Drop - April 6, 2017 (#2457) - Morning Dew

Leave A Comment

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