DEV Community

Cover image for Asynchronous Calls, Data-Transfer-Objects & Automapper in .NET Core Web API

Asynchronous Calls, Data-Transfer-Objects & Automapper in .NET Core Web API

Patrick God on January 28, 2020

This tutorial series is now also available as an online video course. You can watch the first hour on YouTube or get the complete course on Udemy....
Collapse
 
reddgum profile image
Michael

Hi Patrick,

Under the DTO section, you write, ...First things first, let’s create a folder called Dtos and then another folder called Character for all the data transfer objects regarding the RPG characters."

Here's where we get into trouble. You don't specify where either DTO folder or Character folder should be, or if there's a parent-child relationship to them. It's only hinted at further down when you show: using dotnet_rpg.Dtos.Character

May I suggest being a little more precise with the wording? How about:

First things first, lets create a folder under the main project called 'Dtos' and then under the new 'Dtos' folder, a child folder 'Character'.

Further down, we create the GetCharacterDto and AddCharacterDto classes, and as I've personally discovered, it's really easy to miss the little namespace detail, so those two classes should be INSIDE the Dtos.Character folder, not the Dtos folder.

I know that it's hard writing tutorials that keep it concise and clean - too many words does tend to shut some people down. Thanks for taking the time and making the effort to write what is otherwise a very clean and well crafted tutorial.

Collapse
 
_patrickgod profile image
Patrick God • Edited

Hey Michael,

Thank you very much for your comment. I'll keep that in mind for the upcoming parts.
In general, it doesn't really matter if you follow the folder structure completely.
VS Code will use the proper namespace of your structure, anyways.
But it's a good point!

Take care,
Patrick

Collapse
 
informagico profile image
Alessandro Magoga

Thanks Patric for the great series!

In this chapter I think you're missing to add changes to CharacterController while implementing DTOs.
Parameter type change from Character to AddCharacterDto.

[HttpPost]
public async Task<IActionResult> AddCharacter(AddCharacterDto newCharacter)
{
    return Ok(await _characterService.AddCharacter(newCharacter));
}

or am I missing something?

Collapse
 
_patrickgod profile image
Patrick God

Hey Alessandro,

Thank you very much! You're absolutely right, I totally forgot to add this.
I fixed that now. The change of the types is done right before adding AutoMapper.

Have a great day!

Take care,
Patrick

Collapse
 
ghostbasenji profile image
GhostBasenji

Hi Patrick
I'm learning from your lessons. Repeating all your actions.
Not paying attention to the following warnings, such as
"This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. [dotnet-rpg]"
Postman returns this error:
{
"type": "tools.ietf.org/html/rfc7231#sectio...",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "|738fe68b-408ee4ce1960c8d1."
}
What am I doing wrong?
With respect.
P.S. Just in case, I put a link to the repository. [github.com/GhostBasenji/dotnet-rpg]

Collapse
 
_patrickgod profile image
Patrick God

Hi!
Thank you!
The async warning should disappear later when you're using EF Core and "real" asynchronous methods.

The Postman warning is strange. What's the URI you are using and what method do you want to call? Maybe your body is not configured properly. Did you select text/json?

Hope this helps, and thanks for waiting!

Take care,
Patrick

Collapse
 
jboada profile image
jboada

Hi Patrick,

An amazing article so far.

It called my attention the section called "Proper Service Response with Generics" because it is something that I have done many times in my projects and other developers have laughed at me because they did not see it as useful.

For me, it is one of the thing most useful because there is always an answer, no matter if the app fails, the app records the fail internally and give back a proper answer for the client but never an ugly error that could be handled.

So, thank you for mention it, I see that others have been thru this situation.

Sincerely,
Juan Boada

Collapse
 
henkvandergeld profile image
henkvandergeld

Hi Patrick, we followed your tip to use the ServiceResponse class, but we got feedback that things as OkObjectResult, or NotFoundObjectResult were not available for the consumer of the API's. So instead of returning an ActionResult<>, we returned a ServiceResponse<>. Did we do something wrong? How do you consider this?
Thanks in advance. Regards, Henk

Collapse
 
codexdelta profile image
Aswin Devarajan

Do you have a github repo link for this tutorial?

Collapse
 
_patrickgod profile image
Patrick God

Yep, GitHub repo is now available here: github.com/patrickgod/dotnet-rpg

Collapse
 
codexdelta profile image
Aswin Devarajan

Thank you, loved your tutorial. it got me started with Dotnet core 3 in a single day.

Collapse
 
chinthana84 profile image
Chinthana Gunasekara

nice one. what about the unit of work pattern with repos

Collapse
 
sumitkharche profile image
Sumit Kharche

Great Article Patrick

Collapse
 
_patrickgod profile image
Patrick God

Thank you very much!

Collapse
 
moaatazelkholy profile image
Moataz

Great series, thank you so much.

Collapse
 
_patrickgod profile image
Patrick God

Glad you like it! Thank you very much for your feedback! :)

Collapse
 
abo__bashir profile image
Mohamed Ahmed

Great series, thank you so much, it was useful to me.

Collapse
 
pratyushplp profile image
Pratyush Pradhan

Hi Patrick,
Great tutorial !! . I have a question, why was the mapper implemented in service rather than the controller? Wouldnt it be better if we implemented the mapping in the controller?