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!
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.
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.
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, 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?
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
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.
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.
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.
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?
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?
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?
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.
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.
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.
@@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
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: ua-cam.com/video/HhZ4DtON404/v-deo.html
Thank Milan. You're a very good teacher. You explain very well.
You are welcome!
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
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
@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..
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
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.
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)
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
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 :)
Do multiple orders also use Dictionary, or other structures, set?
Not sure what you mean?
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 😅
Nice Milan, you are really good teaching us what you know. Thanks a lot!
Most welcome :)
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.
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.
I was eagerly waiting for this video!!!
Awesome, it hope it delivered 😁
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 for explaning, out of context what editor are you using because the intellisense in it is pretty handy.
Visual Studio + ReSharper
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))
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.
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
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.
On which software are you running the queries? Management studio?
SSMS / Dbeaver
@@MilanJovanovicTechOk. Thanks. I was looking for such a tool.
How would you use Dapper in a DomainEventHandler ?
What would change?
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!
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.
Great explanation!
Glad it was helpful!
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.
The nature of relational algebra. It's how JOINs return results. ORMs abstract a lot of this from you.
@@MilanJovanovicTech No :) Relational algebra is easier. You could simply say "I don't have an answer."
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?
I suggest you don't use a dict, but rather use a single query and then use a linq group by
That's a nice alternative 🤔 What do we return from the Dapper query in that case?
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
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
Depends on how good you are with SQL
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 👋