Domain to Contract mapping with Automapper | ASP.NET Core 5 REST API Tutorial 20

preview_player
Показать описание

Hello everybody I'm Nick and today I will show you how we can separate our domain objects from our contracts and how we can map from one another using a tool called Automapper.

Don't forget to comment, like and subscribe :)

Social Media:

#tutorial #rest #api
Рекомендации по теме
Комментарии
Автор

0:32 The data context should not operate with DTOs. The application layer and presentation layer operates with data transfer objects. You're showing EF Core there (DbSet) which is something that is part of the infrastructure layer (in this case, the data persistence concern of that layer). You should not have DbSet<Dto>, but DbSet<Entity>. DTOs are used to exchange data between the application (services) layer and the presentation layer, not for persisting domain entities.

For those that are flaring up: I do realize that software architecture is not standard thing and things can vary. My observation is based on a traditional Ports and Adapters or Hexagonal (Clean) architecture approach.

PaulSebastianManole
Автор

Awesome Nick. I like that you cover more advanced concepts . I too, am interested in how you would map your DTO's to your domain and vice versa. :)

larrymcfarlane
Автор

Thanks Nick, great tutorial as always. In the future do you mind showing us how you usually map you DTO's to your Domain and vice versa. Again I really appreciate taking time making these kind of tutorial for us. Thank you.

tirgo
Автор

I just used Automapper for the very first time. It's a beautiful thing, that it is...

larrymcfarlane
Автор

Hi, thanks for all the hard work. Your videos are amazing. I have a couple of questions:
1) It's ok to handle all these logic at Controller Level? What about Services layer?
2) I think it's a matter of simplicity to do the demonstration video but Will be a good idea to add or encapsulate Mapper class. Quick example:

public interface IAMapper

{

TDest Map<TSrc, TDest>(TSrc src, TDest dest);

TDest Map<TDest>(object src);
}



using AutoMapper;

using



namespace
{
public class AMMapper: IAMapper
{
public TDest Map<TSrc, TDest>(TSrc src, TDest dest)
{
return Mapper.Map<TSrc, TDest>(src, dest);
}

public TDest Map<TDest>(object src)
{
return Mapper.Map<TDest>(src);
}
}
}


And then just use IAMapper to avoid calling Automapper directly in each project?
Thanks in advance

kieferFernandez
Автор

Thank you very much for your precious content. A question: how this topic relate to DTOs? Should I use dto as back and forth objects between the rest api and the client?

IvanArdillo
Автор

Is there a good way to handle Injections of Services inside the AutoMapper.Profile? My issue is I would like to use IMemoryCache inside the mapper so I can check some conditions and map base on that. Every solution I found only so far isn't really working...

muhamedkarajic
Автор

I find your tutorial most helpful Nick. Please clear one thing to me. Is making two groups of Contract objects (response and request) necessary? Do I have to create both Request and Response classes for each domain class? Sometimes they are exactly the same. But then, if I use Response object for making a request is inconsistent and would be confusing..

DavorBabic-ifkt
Автор

So far what my understanding about DTO was this, that DTOs are used to transfer data between UI layer and Application layer i.e the response from the controller, and the objects being mapped to the DB are called as entities, but here DTo is being used as Entity. It got mixed. Can you please make it clear to me ?

aaqibzafar
Автор

Hi Nick the video is fantastic, i would like to know more for Would like to know more for nested array for post request with sql db., thanks.

adhivenkatesh
Автор

Hay Nick, How would you use AutoMapper to map the request object to the domain object in "Update" API??

PrabodhaRandima
Автор

What if your API serves only a single client that you also control? Aside from whether you want Domain objects for a more OOAD approach. Would you still recommend all these layers?

davidroberts
Автор

can you do bench mark on manual linq select vs auto mapper?

clashclan
Автор

have any one tried to use automapper with updaterange? I am facing an issue as the data from frontend is fetached it does not have a timestamp in it. updaterange does not fetch that column from db and it returns an error.

bilalmehrban
Автор

System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at argument)
at System.Linq.Enumerable.Select[TSource, TResult](IEnumerable`1 source, Func`2 selector)


That's what I get in GetAll method post controller

hachapuri
Автор

Instead of adding dbset<ModelName> statically, can we do it in a dynamic way like dbset<TEntity>?

srilathakagana
Автор

hi nick, how i can have the source? you repository is archived

shahabnoori
Автор

Could Domain object have logic? Sat I have a Person Domain Class with a birthdate. Should Person Domain Object knows its age? Or Should a PersonService know how to calculate the age?

facundo
Автор

Why Automapper and not mapster? Just wondering

noblenetdk
Автор

Please never change the code outside the camera anymore. Especially if the link to the source code doesn't work anymore

unbekannt
welcome to shbcf.ru