Master Dapper Relationship Mapping In 18 Minutes

  Рет қаралды 15,536

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 97
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@DiegoC-zt7hp
@DiegoC-zt7hp 7 ай бұрын
Hi Milan, I really appreciate your videos, i have learned a lot!!! Thank you. I have a question, do you know if its possible to pass a cancelation token to a dapper QueryAsync method? I saw you have the cancelation token, but did not see how its passed. Thanks again!
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
@@DiegoC-zt7hp There isn't an option to pass in a CancellationToken so far.
@kodindoyannick5328
@kodindoyannick5328 7 ай бұрын
Thank Milan. You're a very good teacher. You explain very well.
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
You are welcome!
@antonmartyniuk
@antonmartyniuk Жыл бұрын
The one thing worth mentioning is that EF Core is so DDD and rich domain model friendly out of the box comparing to Dapper. When using Dapper you lose rich domain model for mapped objects but gain query execution boost. It's a philosophy law: something you win and something you lose simultaneously. When using a DDD the best approach with Dapper would be to map objects "mapped" from Dapper into Rich domain model. Extra step, but it's a good trade-off
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Oh yeah, anything more complex than a flat structure gets really messy with Dapper. A solution can be to have two separate models. One for persistence, and another for your domain - the rich model. And then using the Memento/Snapshot pattern to map between the two.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Talked about it here: kzbin.info/www/bejne/fpm9ZXeqhLNnZpY
@ddedz0
@ddedz0 Жыл бұрын
Your videos and posts contain distilled information and are very useful. Thank you for your shares, and I wish you good days ahead. Stay awesome :)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you very much! Stay awesome :)
@nettoaoquadrado
@nettoaoquadrado Жыл бұрын
Milan, wouldn't it be interesting to employ Dapper's queryMultiple feature for the 1:N mapping approach? I mention this because, when we have N items of orders associated with a single order, for example, using the QueryAsync approach would result in generating N-1 instances of 'order' in memory that won't be necessary.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Agreed, that's similar to what `AsSplitQuery` does with EF Core
@sunnypatel1045
@sunnypatel1045 Жыл бұрын
Always use IOptions if passing connection string etc in the factory. Like your other video saves use doing a check in the constructor and can validate before running the application. Or better to store in some sort of vault and get it out config later.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
.NET is pretty flexible in that regard
@joseagustincarrenogallego3704
@joseagustincarrenogallego3704 11 ай бұрын
Nice Milan, you are really good teaching us what you know. Thanks a lot!
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
Most welcome :)
@mac653
@mac653 Жыл бұрын
@MilanJovanovicTech, Thank for the good informative video. I noticed your unique (to my eyes) project structure and was curious are there prior videos to this one where you explained how and why to architect your solutions like this?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I have a few Clean Architecture 'from scratch' videos you can check out, but this isn't much different from your typical CA setup
@mac653
@mac653 Жыл бұрын
@@MilanJovanovicTech Thank you, let me go check them out..
@bennybloch
@bennybloch Жыл бұрын
Thanks Milan for this video. I would like to mention that if you move your mapping lambda out as a regular method (e.g. in order to reuse it) then you need to make your dictionary class level or pass it as a parameter to the method.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Right, but you can't pass it as a parameter and satisfy the Dapper argument (as a method group, at least)
@RichardScoop
@RichardScoop Жыл бұрын
With dapper you can add the column names on the class and have it translate that for you without the need of doing that in the SQL
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
But I don't want to have dirty names with underscores in my classes
@IanWilkinson-SgtWilko
@IanWilkinson-SgtWilko Жыл бұрын
​@@MilanJovanovicTechI think Richard meant that you can add attributes to the class name, and avoid having nasty names, and also manual name mapping.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
@@IanWilkinson-SgtWilko Which attribute is that? 🤔
@dennisjones1184
@dennisjones1184 Жыл бұрын
@@MilanJovanovicTech You can add DefaultTypeMap.MatchNamesWithUnderscores = true; to Program.cs. This will map my_column to MyColumn.
@Kingside88
@Kingside88 Жыл бұрын
This dapper relational mapping thing is horrible. But I've no idea how to develop it better. I just wonder (I know it sounds crazy but) why is it not possible to redevelop the whole thing. Since we have relations in the database, wouldn't it be great to reveive a result not as table structured? Many RDBMS can transform childs as json or xml. But what I am asking for are child tables inside table. The C# data provider could translate it into proper c# objects.
@feitan8745
@feitan8745 Жыл бұрын
For that you could just use EF, but you could load child entities with Dapper while spliting queries results into multiple sets. It's not that complicated, just write the correct SQL and make sure your table is well structured.
@Kingside88
@Kingside88 Жыл бұрын
@@feitan8745 I know, we can use EF and I can read dapper queries. But they look horrible. And they can getting complicated, when you have complicated objects which you have in real world scenarios. Just take the order as example.
@feitan8745
@feitan8745 Жыл бұрын
@@Kingside88 hm it can be complicated at first, but just like EF, once you get things going, it's pretty sraightforward. Are you using repositories, command patterns, UoW or something alike?
@feitan8745
@feitan8745 Жыл бұрын
@@Kingside88 hm it can be complicated at first, but just like EF, once you get things going, it's pretty sraightforward. Are you using repositories, command patterns, UoW or something alike?
@Kingside88
@Kingside88 Жыл бұрын
@@feitan8745 to be honest I like EF nowadays. Its very simple straighforward. But I also like to use Dapper in repositories. My point was more how my dream is to handle sub-data.
@nettoaoquadrado
@nettoaoquadrado Жыл бұрын
I was eagerly waiting for this video!!!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Awesome, it hope it delivered 😁
@antonmartyniuk
@antonmartyniuk Жыл бұрын
One of use cases for using Dapper that is not query execution speed. If you work with a customer database and to query data from there - the customer gave you a ton of ready-made SQL queries that he maintains himself. It would be easier to add these queries to Dapper rather than spend days or weeks understanding how to create EfCore mapping and LINQ queries for this
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
If they already have the queries, they can expose them as views?
@antonmartyniuk
@antonmartyniuk Жыл бұрын
@@MilanJovanovicTech of course, but if they don't and don't want to maintain them. I had such use case in one of the recent projects
@d0pare
@d0pare Жыл бұрын
Directly creating npgsql connections is discouraged after version 7. Instead, NpgsqlDataSource needs to be used.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Got a resource for that?
@d0pare
@d0pare Жыл бұрын
It is mentioned on official Npgsql website under basic usage.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
@@d0pare Awesome thanks a lot!
@ariefyousif521
@ariefyousif521 Жыл бұрын
Excellent video! I've always been a fan of dapper because of its speed and I prefer writing raw sql. I'm wondering how you would implement repository pattern and uof? I've seen many examples where uof is injected into a repository and a transaction is created from there. I'm wondering if there is another approach; where uof is activated outside of a repository and is working behind the scenes.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Implementing a proper UoW on your own is a lot of work - and probably not worth it 😅
@antonmartyniuk
@antonmartyniuk Жыл бұрын
Interesting how such an one-to-many mapping does EF Core under the hood and how it compares with Dapper code you've written
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
EF Core does so much heavy lifting, it's underapprecaited
@antonmartyniuk
@antonmartyniuk Жыл бұрын
@@MilanJovanovicTech absolutely agree. I use EF Core with great success 90% of the time. Dapper only when needed
@julianhawkins3173
@julianhawkins3173 Жыл бұрын
Which ORM do you prefer, please? Out of a choice of Entity Framework or Dapper? Thanks
Жыл бұрын
For me: If the database is an application in it self, has a lot of logic in it, and/or is heavily optimized etc... then Dapper or Ling2db. If the database is just a "dumb" place to put data, then EF / EFCore.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
EF most of the time. But I use both in my apps.
@tabrizgulmammadov7097
@tabrizgulmammadov7097 Жыл бұрын
Firstly, thank you so much for your videos. As if all items are loaded and after, is worked on them. Isn't it a problem in terms of performance when we work with many items?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can rectify that by loading fewer items/paging 😁
@tabrizgulmammadov7097
@tabrizgulmammadov7097 Жыл бұрын
@@MilanJovanovicTech thanks))
@MohitTorani-s6x
@MohitTorani-s6x 2 ай бұрын
Thanks for explaning, out of context what editor are you using because the intellisense in it is pretty handy.
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
Visual Studio + ReSharper
@JuanPabloAlvisNieto
@JuanPabloAlvisNieto Жыл бұрын
Great explanation!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad it was helpful!
@djsnake1995
@djsnake1995 Жыл бұрын
Dapper can work with records, but record parameters need to have the default value specified
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It's too much conforming to Dapper for my liking. I find classes easier.
@AthelstanEngland
@AthelstanEngland 11 ай бұрын
New to all this and having a lot of SQL experience has started with Dapper. It seems to make more sense, gave me control and faster (although not really noticed that with the size datasets I'm working with). But this seems lots a lot of code for a pretty simple bit of mapping compared to what EF gives. How,or would, Automapper help here?
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
Automapper solves EF to object mapping. Can you even use it with Dapper?
@AthelstanEngland
@AthelstanEngland 11 ай бұрын
@@MilanJovanovicTech 😂 hey I'm asking the questions! That was my query although it seems you can but from what I've found it didn't really solve this.
@sunzhang-d9v
@sunzhang-d9v Жыл бұрын
Do multiple orders also use Dictionary, or other structures, set?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Not sure what you mean?
@whoost
@whoost Жыл бұрын
How would you use Dapper in a DomainEventHandler ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
What would change?
@HeavyMachinery512
@HeavyMachinery512 3 ай бұрын
On which software are you running the queries? Management studio?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
SSMS / Dbeaver
@HeavyMachinery512
@HeavyMachinery512 3 ай бұрын
@@MilanJovanovicTechOk. Thanks. I was looking for such a tool.
@yogij17
@yogij17 Жыл бұрын
Thank you for the video Milan In my case, I have to create a wrapper method around dapper execute method and pass output parameters to stored procedure. The function should return as many output parameters as I need. Can you please guide me?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Why do you need out params?
@yogij17
@yogij17 Жыл бұрын
Thank u for the reply! The stored procedure accepts and returns out parameters. So I need them.
@microtech2448
@microtech2448 Жыл бұрын
That's insane, with joins dapper is gonna be too complex.. never gonna use it
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Haha, take a look at the performance first
@microtech2448
@microtech2448 Жыл бұрын
Why not call SPs then with EF core?
@Hkumar_new
@Hkumar_new 5 ай бұрын
Hay , can you tell me how, where is the document is available to read for one to many relationships and multiple set returns and how bind up them.....please.
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many
@anonlegion9096
@anonlegion9096 Ай бұрын
8:56 Will it help if we use the `[JsonPropertyName()]` attribute in the model class that the result is being mapped to instead of using `AS`?
@MilanJovanovicTech
@MilanJovanovicTech Ай бұрын
No, this is SQL -> object. Nothing to do with JSON.
@ahmettek315
@ahmettek315 Күн бұрын
Isn't that unnecessarily complex :( It is even easier in nHibernate (which I don't like). And the question, what would happen if I need to map something that has a 1-to-many connection to many other tables and many-to-1 some and also many-to-many? That is not something rare when you get into real world applications. I couldn't find good documentation and samples anywhere.
@MilanJovanovicTech
@MilanJovanovicTech Күн бұрын
The nature of relational algebra. It's how JOINs return results. ORMs abstract a lot of this from you.
@ahmettek315
@ahmettek315 Күн бұрын
@@MilanJovanovicTech No :) Relational algebra is easier. You could simply say "I don't have an answer."
@joga_bonito_aro
@joga_bonito_aro Жыл бұрын
My gut feeling says that this seems very hackish... The one to many mapping that is.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
How would you do it differently?
@joga_bonito_aro
@joga_bonito_aro Жыл бұрын
@@MilanJovanovicTechI'm not saying that what you did is hackish, I'm saying that the wayp dapper wants us to implement these relationships seems hackish. I just don't use dapper, even if it's faster. I'd rather have a better readable/maintainable code. And EF Core does provide that imho
@10Totti
@10Totti Жыл бұрын
Dapper is too verbose, EF 8 is better.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Dapper is also faster, so that's the price you pay
@10Totti
@10Totti Жыл бұрын
@@MilanJovanovicTech with EF8 they are almost the same now.
@techpc5453
@techpc5453 Жыл бұрын
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Been a while 👋
@xxXAsuraXxx
@xxXAsuraXxx Жыл бұрын
Dapper is cool, but EF core is another level
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
They solve different problems
Vertical Slice Architecture Project Setup From Scratch
22:43
Milan Jovanović
Рет қаралды 60 М.
Using Multiple EF Core DbContexts in a Single Application
17:46
Milan Jovanović
Рет қаралды 35 М.
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17
She's very CREATIVE💡💦 #camping #survival #bushcraft #outdoors #lifehack
00:26
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 21 МЛН
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 27 МЛН
I Choose THIS Over EF Core - How To Use Dapper in C#
16:09
Dev Leader
Рет қаралды 1,7 М.
One to Many Relationship with Dapper & .NetCore 5
40:58
Alagbala Damilola
Рет қаралды 5 М.
Getting Started with Dapper in .NET
11:29
Amichai Mantinband
Рет қаралды 12 М.
Raw SQL, SQL Query Builder, or ORM?
16:19
ArjanCodes
Рет қаралды 101 М.
Adding Filtering, Sorting And Pagination To a REST API | .NET 7
24:02
Milan Jovanović
Рет қаралды 55 М.
What Are Your Thoughts on Entity Framework Core vs. Dapper?
21:49
The Problem With Microservices
17:47
Continuous Delivery
Рет қаралды 434 М.
iPhone Standby mode dock, designed with @overwerk
0:27
Scott Yu-Jan
Рет қаралды 7 МЛН
Самый дорогой iPHONE 16 PRO MAX #shorts
0:58
Арсений Петров
Рет қаралды 2,6 МЛН
Скучнее iPhone еще не было!
10:48
itpedia
Рет қаралды 620 М.
3x 2x 1x 0.5x 0.3x... #iphone
0:10
Aksel Alze
Рет қаралды 2,8 МЛН
Китайцы сделали телефон БАЯН
0:14
Собиратель новостей
Рет қаралды 1,6 МЛН