Master Dapper Relationship Mapping In 18 Minutes

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 101

  • @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 9 місяців тому

      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  5 місяців тому

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

  • @antonmartyniuk
    @antonmartyniuk Рік тому +3

    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  Рік тому +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  Рік тому +2

      Talked about it here: ua-cam.com/video/HhZ4DtON404/v-deo.html

  • @kodindoyannick5328
    @kodindoyannick5328 9 місяців тому +2

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

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

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

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

  • @antonmartyniuk
    @antonmartyniuk Рік тому +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  Рік тому

      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

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

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

  • @antonmartyniuk
    @antonmartyniuk Рік тому +2

    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

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

  • @sunzhang-d9v
    @sunzhang-d9v Рік тому +1

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

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

  • @joseagustincarrenogallego3704
    @joseagustincarrenogallego3704 Рік тому +1

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

  • @AthelstanEngland
    @AthelstanEngland Рік тому +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  Рік тому

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

    • @AthelstanEngland
      @AthelstanEngland Рік тому

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

  • @anonlegion9096
    @anonlegion9096 3 місяці тому

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

  • @nettoaoquadrado
    @nettoaoquadrado Рік тому +1

    I was eagerly waiting for this video!!!

  • @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 Рік тому

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

  • @MohitTorani-s6x
    @MohitTorani-s6x 4 місяці тому

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

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

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

  • @Hkumar_new
    @Hkumar_new 6 місяців тому

    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  6 місяців тому

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

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

  • @HeavyMachinery512
    @HeavyMachinery512 5 місяців тому

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

  • @whoost
    @whoost Рік тому +1

    How would you use Dapper in a DomainEventHandler ?

  • @d0pare
    @d0pare Рік тому +2

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

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

  • @JuanPabloAlvisNieto
    @JuanPabloAlvisNieto Рік тому

    Great explanation!

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

  • @microtech2448
    @microtech2448 Рік тому +3

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

  • @simonbateman8803
    @simonbateman8803 Місяць тому

    I suggest you don't use a dict, but rather use a single query and then use a linq group by

    • @MilanJovanovicTech
      @MilanJovanovicTech  Місяць тому

      That's a nice alternative 🤔 What do we return from the Dapper query in that case?

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

  • @nothingisreal6345
    @nothingisreal6345 Місяць тому

    Look at the code and you know why using Dapper is not the best idea ever. I doubt that the performance gain is worth the effort and potential errors

  • @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 Рік тому