Это видео недоступно.
Сожалеем об этом.
Master Dapper Relationship Mapping In 18 Minutes
HTML-код
- Опубликовано: 16 авг 2024
- ☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
🚀 Support me on Patreon to access the source code: / milanjovanovic
Dapper is a popular micro-ORM in the .NET ecosystem. It's fast and easy to work with. But, the mapping from SQL to an object can be complicated in some scenarios. One-to-one mapping is straightforward. One-to-many mapping is a bit tricky. So I'll show you how to solve the most common mapping scenarios with Dapper in this video.
Mastering Dapper Relationship Mappings
www.milanjovan...
Join my weekly .NET newsletter:
www.milanjovan...
Read my Blog here:
www.milanjovan...
Subscribe for more:
/ @milanjovanovictech
Chapters:
0:00 Adding Dapper to the project
1:20 Creating the ISqlConnectionFactory interface and using it
2:15 Implementing the first Dapper query
4:27 Implementing the SqlConnectionFactory and registering with DI
6:15 Fixing issues with One-to-One mapping
9:56 The problem with One-to-Many mapping
15:42 How to solve One-to-Many mapping with Dapper
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
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!
@@DiegoC-zt7hp There isn't an option to pass in a CancellationToken so far.
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
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.
Talked about it here: ruclips.net/video/HhZ4DtON404/видео.html
Thank Milan. You're a very good teacher. You explain very well.
You are welcome!
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 :)
Thank you very much! Stay awesome :)
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.
.NET is pretty flexible in that regard
Nice Milan, you are really good teaching us what you know. Thanks a lot!
Most welcome :)
@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?
I have a few Clean Architecture 'from scratch' videos you can check out, but this isn't much different from your typical CA setup
@@MilanJovanovicTech Thank you, let me go check them out..
I was eagerly waiting for this video!!!
Awesome, it hope it delivered 😁
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.
Agreed, that's similar to what `AsSplitQuery` does with EF Core
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.
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.
@@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.
@@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 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 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.
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
If they already have the queries, they can expose them as views?
@@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
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
But I don't want to have dirty names with underscores in my classes
@@MilanJovanovicTechI think Richard meant that you can add attributes to the class name, and avoid having nasty names, and also manual name mapping.
@@IanWilkinson-SgtWilko Which attribute is that? 🤔
@@MilanJovanovicTech You can add DefaultTypeMap.MatchNamesWithUnderscores = true; to Program.cs. This will map my_column to MyColumn.
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.
Right, but you can't pass it as a parameter and satisfy the Dapper argument (as a method group, at least)
Directly creating npgsql connections is discouraged after version 7. Instead, NpgsqlDataSource needs to be used.
Got a resource for that?
It is mentioned on official Npgsql website under basic usage.
@@d0pare Awesome thanks a lot!
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`?
No, this is SQL -> object. Nothing to do with JSON.
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.
EF most of the time. But I use both in my apps.
Great explanation!
Glad it was helpful!
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.
Implementing a proper UoW on your own is a lot of work - and probably not worth it 😅
Thanks for explaning, out of context what editor are you using because the intellisense in it is pretty handy.
Visual Studio + ReSharper
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.
learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many
Do multiple orders also use Dictionary, or other structures, set?
Not sure what you mean?
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?
You can rectify that by loading fewer items/paging 😁
@@MilanJovanovicTech thanks))
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?
Automapper solves EF to object mapping. Can you even use it with Dapper?
@@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.
Dapper is cool, but EF core is another level
They solve different problems
How would you use Dapper in a DomainEventHandler ?
What would change?
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?
Why do you need out params?
Thank u for the reply!
The stored procedure accepts and returns out parameters. So I need them.
Dapper can work with records, but record parameters need to have the default value specified
It's too much conforming to Dapper for my liking. I find classes easier.
Interesting how such an one-to-many mapping does EF Core under the hood and how it compares with Dapper code you've written
EF Core does so much heavy lifting, it's underapprecaited
@@MilanJovanovicTech absolutely agree. I use EF Core with great success 90% of the time. Dapper only when needed
On which software are you running the queries? Management studio?
SSMS / Dbeaver
@@MilanJovanovicTechOk. Thanks. I was looking for such a tool.
That's insane, with joins dapper is gonna be too complex.. never gonna use it
Haha, take a look at the performance first
Why not call SPs then with EF core?
My gut feeling says that this seems very hackish... The one to many mapping that is.
How would you do it differently?
@@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
Dapper is too verbose, EF 8 is better.
Dapper is also faster, so that's the price you pay
@@MilanJovanovicTech with EF8 they are almost the same now.
Been a while 👋