Will This New EF Core Feature Be The End Of Dapper?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
will dapper become unnecessary with the release of EF core 8. Dapper is a very popular micro RM that is famous for its performance but he of core has been catching up in terms of performance and in EF core 8 we are getting a killer feature that may just make Dapper a thing of the past to show you what I'm talking about we're going to add another feature to the application and this is going to be a query to fetch the order together with the line items so let's add this to our orders feature folder and let's call it get order for example we're going to add a class representing our query we're going to call it get to order query we're just going to give it the raw order ID and also make this a record so let's give it the order ID as a good and we need to make it an I request because we're using mediator and it needs to be a generic one because we want to return our response now what is going to be the response of our query we don't have one yet so let's quickly create it I'm going to use a record to define the order response so let's say I just want the ID of the order the customer ID of the order and a list of line items so I'm going to make this a list and I don't have online item class so I'm going to add that in just a moment so let's make that another record this is going to be the line item response and we're going to give it the line item ID and let's for example give it the price of the line item and that should be enough now I can use the line item response here for our line items list and I prefer keeping the response objects inside of their own files so I'm going to go ahead and move them so let's get back to our get order query now it's going to return an order response so now I need the get order query Handler and this is where I'm going to show you the interesting new feature that we are getting in ef48 so let me create the get order query Handler let's go ahead and make this internal and sealed and let's implement the I request Handler of get order query awesome so now I need to say what this is going to return and that's going to be the order response and I'm actually going to format this a little different like this and let's Implement our interface now I'm going to show you a few ways to return the order response with ef core so let's go ahead and inject our I application DB context which is our DB context subtraction and let's see how we can return the order response so the first approach would be just load the entire order with ef core so that would look something like this so we need an order and we load it by calling await context we get the order and we also include the line items because we're going to need them so we're going to say include the order line items and we're going to return the first order with the given ID so I'm just going to say order ID equals New Order ID and we pass in the order ID from our request so we can even pass that a cancellation token and now that I have the order I can create the order response and this is going to be a new order response which accepts the order ID the customer ID both of them I can pull from the order so order ID value and then order customer ID value and then for the line items I can say order line items and I can just select these single line items so I can say new line item response and we can fill in the line item id value and also the price coming from the price value object on the Align item so this is going to be line item price amount and we need to call to list here to satisfy the Constructor of the order response so maybe I can format this a little differently perhaps like this and now it should be a little bit more readable so with this I can just return the order response and this is one approach how you can return the order response this approach is not ideal because we are loading many things that we don't need from the database and we are also using our tracking query so one simple Improvement that you can do is add has no tracking here this is going to give you a slight performance Improvement but still we can do better so instead of loading the entire order and line items why not just load this structure here directly with ef so you can do that and all you need to do is here instead of selecting the light items and adding as no tracking we can say orders and then select and now I'm going to map the same structure here into the order response so I can say order and we're going to get rid of this order here and make it into an order response and let me copy this because we're going to use it in our select statement so let's get rid of all of this okay and now let me just fix this up this is going to return a new order response so let me just try to format this a little better we need to close this bracket here for the single async we should actually move this into aware statement so we can do this and add the where statement here so now I can say the orders where the order ID equals New Order ID and pass the one from our request so this and okay now this is one link statement it's no tracking by default and it's more performant because it's only returning the values that I need directly from the database you were probably already familiar with everything you saw up to this point so now I'm going to show you the new EF core 8 feature that makes this even simpler and is probably going to make Dapper unnecessary in our applications so let's go ahead and install the EF core 8 Library I need to turn on include pre-release to be able to see this library and let's go ahead and install the latest preview version of EF core so I'm going to update my EF core package and also my EF core relational package and now if I go back to my query Handler I'm going to show you how to write a SQL query with the F core and return an unmapped type we were not able to to do this before EF core 8 and this is pretty much exactly what Dapper does with its native SQL mapping from the database I'm going to need to access the database facade instance so I'm going to expand my I application DB context interface and add the database facade so I'm going to say database facade it's called database in our EF core DB context so I'm going to give it exactly the same name and it only has a get property if I go to my implementation which is the application DB context you'll see that there is nothing new to implement because it already contains the database facade as we defined it here so now in my get order query Handler I can do something like this I'm going to comment this out just so that we have the previous version and let me show you what you can do with ef core 8. let's go ahead and access our database facade and on it we have access to the SQL query and SQL query raw methods in previous EF core versions you could only return scalar types from this method such as integers booleans guides and so on but now you can return complex types like classes and it's going to be supported directly we're going to use the SQL query version and we're going to map it into our order response if you try to just return the order response from this method so something like this you're going to run into a problem because the order response contains a list of line items which we mapped here and you don't have a way to return a list from SQL this is also a common problem in Dapper a simple way to solve this is I'm just going to create a nested type here so maybe I can make it private sealed record let's call it order summary and let's give it a few properties that we're going to map to our order response so one thing I need is the order ID so I'm going to add that then I need the customer ID then I need the line item ID and lastly I need the price so I'm actually going to move this into its own lines so that you can see it better so the last part is a decimal representing the line item price awesome now this object I can load with a SQL query directly and then I can map the order summary list into our auto response so let's go ahead and do this I'm going to actually make this a list this is going to be order summaries because we're going to be returning more of them and let's see what we can do so for the SQL query I need to write a statement to fetch what I need from the database so let's go ahead and do something like this we need to select the order ID which is going to come from the order table so I'm going to say ID as order ID then I need to fetch the customer ID which is going to come from the order custom ready so I don't need to map it to a new name then I'm going to need the line item id this is going to be line item id as line item ID and lastly I'm going to need the line item price this is going to be line item price amount because this is how the value objects is mapped into SQL so as line item price so this is a very long SQL statement I apologize for that but now we need to add a where statement or rather we first need to add a from statement and then aware so from orders and I'm going to call it as o so this is going to match here and we're going to join on the line items table so we're going to say join line items l i and we're going to say join on the line item ID to match the order ID so we're going to say line item order ID equals order ID and lastly I need my where statement to only return the order ID so I'm going to say where and I need an interpolated string here and how you specify a parameter in the SQL query with ef core is you just pass in the interpolated value so I can pass in the request ID here and this completes my query so let's see what I did here I wrote the typical SQL query as you would do with Dapper but I'm returning an unmapped type so this is a type that is not an EF core entity this is something that you couldn't do with ef core previously but with ef48 you can do it it's also important to note that when you specify an argument like this it's not directly parsed as a string it's actually converted into a SQL parameter and this is not suspect to simple injection this returns an i queryable of order summaries and I can say to list async on this collection and get back a list of order summaries now this is a list of flat objects like this which I need to map into my order response so I can say order response and how I do that is I group The quarter summaries by the order ID and then I just map the individual line items so we're going to say order summaries Group by and we're going to group them by the order ID and now I can just say select and because we have the same order ID we're only going to have one group so I can say select this group and return me an order response and from this group I can say take the key which is going to be the order ID I need to select the customer ID I can just say select the first element from this group and give me the customer ID and for the line items I can say select the individual elements in this grouping and these are going to be the order summaries and for each of them we need to instantiate a new line item response and we can just say give me the line item ID and the line item price and because this needs to be a list I need to call to list so this completes the select statement and now I can just say first or single probably single would be better because we want to make sure with our code that we only have one order response returned from the database so this completes the implementation so it's more verbose than this implementation here but it's probably going to be more performant I haven't done any benchmarks I'm going to save this for a future video where I want to compare EF core 8 to Dapper directly and then we're going to see which one comes on top in terms of performance make sure to smash the like button for the YouTube algorithm subscribe to my channel for more amazing videos and until next time stay awesome
Info
Channel: Milan Jovanović
Views: 40,147
Rating: undefined out of 5
Keywords: ef core, ef core tutorial, ef core 7, ef core code first approach, ef core database first approach, ef core migrations, ef core relationship, ef core repository pattern, ef core vs dapper, ef core code first, ef core performance, ef core raw sql, ef core sql query, ef core raw sql query, ef core sql, ef core sql performance, ef core sql mapping, ef core ddd, ddd, domain-driven design, dapper, dapper performance, dapper vs ef core, orm, entity framework core, entity framework
Id: Egd-BMPCHNc
Channel Id: undefined
Length: 14min 10sec (850 seconds)
Published: Fri Mar 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.