Это видео недоступно.
Сожалеем об этом.

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

Комментарии • 94

  • @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 6 месяцев назад

      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  2 месяца назад

      @@DiegoC-zt7hp There isn't an option to pass in a CancellationToken so far.

  • @antonmartyniuk
    @antonmartyniuk 11 месяцев назад +2

    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  11 месяцев назад +1

      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  11 месяцев назад +2

      Talked about it here: ruclips.net/video/HhZ4DtON404/видео.html

  • @kodindoyannick5328
    @kodindoyannick5328 5 месяцев назад +2

    Thank Milan. You're a very good teacher. You explain very well.

  • @ddedz0
    @ddedz0 Год назад +2

    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 :)

  • @sunnypatel1045
    @sunnypatel1045 Год назад +2

    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.

  • @joseagustincarrenogallego3704
    @joseagustincarrenogallego3704 9 месяцев назад +1

    Nice Milan, you are really good teaching us what you know. Thanks a lot!

  • @mac653
    @mac653 Год назад +2

    @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  Год назад +1

      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..

  • @nettoaoquadrado
    @nettoaoquadrado Год назад +1

    I was eagerly waiting for this video!!!

  • @nettoaoquadrado
    @nettoaoquadrado Год назад +6

    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  Год назад +1

      Agreed, that's similar to what `AsSplitQuery` does with EF Core

  • @Kingside88
    @Kingside88 Год назад +5

    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 Год назад +1

      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 Год назад +1

      @@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 Год назад +1

      @@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.

  • @antonmartyniuk
    @antonmartyniuk 11 месяцев назад +1

    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  11 месяцев назад

      If they already have the queries, they can expose them as views?

    • @antonmartyniuk
      @antonmartyniuk 11 месяцев назад

      @@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

  • @RichardScoop
    @RichardScoop Год назад +2

    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 11 месяцев назад

      ​@@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  11 месяцев назад

      @@IanWilkinson-SgtWilko Which attribute is that? 🤔

    • @dennisjones1184
      @dennisjones1184 10 месяцев назад

      @@MilanJovanovicTech You can add DefaultTypeMap.MatchNamesWithUnderscores = true; to Program.cs. This will map my_column to MyColumn.

  • @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  Год назад +1

      Right, but you can't pass it as a parameter and satisfy the Dapper argument (as a method group, at least)

  • @d0pare
    @d0pare Год назад +2

    Directly creating npgsql connections is discouraged after version 7. Instead, NpgsqlDataSource needs to be used.

  • @anonlegion9096
    @anonlegion9096 4 дня назад

    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`?

  • @julianhawkins3173
    @julianhawkins3173 Год назад +2

    Which ORM do you prefer, please? Out of a choice of Entity Framework or Dapper? Thanks

    •  Год назад +3

      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  Год назад +2

      EF most of the time. But I use both in my apps.

  • @user-dv2rv8hm7l
    @user-dv2rv8hm7l 10 месяцев назад

    Great explanation!

  • @ariefyousif521
    @ariefyousif521 Год назад +1

    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 😅

  • @MohitTorani-s6x
    @MohitTorani-s6x 15 дней назад

    Thanks for explaning, out of context what editor are you using because the intellisense in it is pretty handy.

  • @Hkumar_new
    @Hkumar_new 3 месяца назад

    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  3 месяца назад

      learn.microsoft.com/en-us/ef/core/modeling/relationships/one-to-many

  • @user-xm7sh3vw8o
    @user-xm7sh3vw8o Год назад +1

    Do multiple orders also use Dictionary, or other structures, set?

  • @tabrizgulmammadov7097
    @tabrizgulmammadov7097 Год назад +1

    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?

  • @AthelstanEngland
    @AthelstanEngland 10 месяцев назад +1

    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  9 месяцев назад

      Automapper solves EF to object mapping. Can you even use it with Dapper?

    • @AthelstanEngland
      @AthelstanEngland 9 месяцев назад

      @@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.

  • @xxXAsuraXxx
    @xxXAsuraXxx Год назад +1

    Dapper is cool, but EF core is another level

  • @whoost
    @whoost Год назад +1

    How would you use Dapper in a DomainEventHandler ?

  • @yogij17
    @yogij17 Год назад +1

    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  Год назад +1

      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.

  • @djsnake1995
    @djsnake1995 Год назад +1

    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.

  • @antonmartyniuk
    @antonmartyniuk 11 месяцев назад +1

    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  11 месяцев назад

      EF Core does so much heavy lifting, it's underapprecaited

    • @antonmartyniuk
      @antonmartyniuk 11 месяцев назад

      @@MilanJovanovicTech absolutely agree. I use EF Core with great success 90% of the time. Dapper only when needed

  • @HeavyMachinery512
    @HeavyMachinery512 2 месяца назад

    On which software are you running the queries? Management studio?

  • @microtech2448
    @microtech2448 Год назад +3

    That's insane, with joins dapper is gonna be too complex.. never gonna use it

  • @joga_bonito_aro
    @joga_bonito_aro Год назад +5

    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 Год назад +3

      @@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 Год назад +2

    Dapper is too verbose, EF 8 is better.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      Dapper is also faster, so that's the price you pay

    • @10Totti
      @10Totti Год назад

      @@MilanJovanovicTech with EF8 they are almost the same now.

  • @techpc5453
    @techpc5453 Год назад