What Are Your Thoughts on Entity Framework Core vs. Dapper?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this episode of Dev questions with Tim Corey join us as we tackle the questions you are asking about a career in software development understanding the industry and new technology if you are just starting out or you want to grow stronger as a developer this is the place to get your questions answered now here's your host expert developer and online educator Tim Corey what are your thoughts on Entity framework core versus Dapper which one's better these are questions that come up a lot and one I thought I'd address in today's episode of Dev questions now if you have a question you want to get answered the best way to get that answered is to go to suggestions.imcorey.com leave that suggestion there or upvote an existing one and hopefully you'll see your question answered on a future episode of Dev questions now I want to get this out of the way right away both EF core and Dapper are excellent tools both have their place this is something that especially my audience might not think I would say some people think that I hate Entity framework and I don't it's an amazing tool but I want to be clear on my thoughts about Entity framework this is going to be my opinion you get to have your own opinion by how a lot of facts to back up my opinion and I encourage you to think through and have your own experiences as well now let's talk first about my experience so you know why I have the opinions that I do I have spent years over a decade as a software development consultant which means I have gone to businesses both small and large from Mom and Pop shops to huge businesses International businesses where I have gone in and I have helped them work on their software I have seen them in production I have seen the tools they have built I have seen a system they have built and I have come in usually at the worst of times when things aren't going well or when they want to add new features and new systems one of the two and so I have been a part of a number of organizations that have used Entity framework I have seen how they have done it I've also seen a lot of SQL databases and have it been structured how they've been designed how they've been used and what the pros and cons are of all of those I have worked with organizations where things have gone great and I have worked with organizations where things have gone horribly I have seen what caused what has gone horribly I've seen what has caused things to go great so this is where I come from when I talk about Entity framework and damper because I have seen a lot so here are the things that I hate when it comes to how Anthony framework is talked about or even used number one people say the Anthony framework is useful so you do not have to know SQL this is the messaging around a lot of tutorials a lot of training even some training from Microsoft where they say hey if you know C sharp and you can learn Entity framework you don't have to know SQL that is not only incorrect I think it's very very dangerous I have seen this firsthand from organizations who have built everything in Entity framework and say well SQL is just the the black box that gets talked to from at the framework we don't know how it works we just know that Anthony framer takes care of it all and does its job that works fine sometimes but usually what happens is it works fine until you get to a certain point in your organization and your application when your organization grows when you have a heavy day of of traffic when you have a certain growth in your organization or you just hit certain points and Milestones things start to grind to a halt and now you don't know why and you don't know how to fix it so that's one of my pet peeves with Anthony framework is more about how it's taught Entity framework can be a great tool it is a great tool but if you abuse it and use it incorrectly it can be a very very bad tool so you need to know how SQL works you need to know how it builds your tables how it builds the queries how it gets the information and how your your data is structured because if you know that you can optimize it if you know that you can help it make better choices but if you just say it's a black box well that's not a good thing it's your application is built upon data that's really the entire reason of your application to exist it's to get data and to give data so if you are an airline your your whole application is around getting people onto flights getting the information putting it into the information about the flights and then about letting people know here's where your flight is here's how it's going here's why it's delayed um so that's your whole application revolves around that data if the one thing you don't know well is how your data is actually stored and retrieved that's a problem and saying well I know Entity framework isn't enough because that's just a part of the overall system you need to know how it works on the SQL side now number two not learn learning Entity framework well enough Entity framework is a very deep tool it does a lot and it has a lot of depth to it but it can often be overlooked because of how quick you can get into Entity framework you say well I I know enough to do the job and that's not good enough because when you just know enough to do a job that doesn't work forever and again this is where I come back to my experience when I would come into companies where they're not having a good day when things are not going well it was not in in development usually it's usually in production where they just did enough to get things working and that worked well when they had a hundred customers when they had a thousand customers but now they have 50 000 customers and that may sound like a huge deal it's really not because when a customer comes and buys one thing from you and then leaves and never comes back that's still one so it accumulates over time so now when you have a Black Friday sale and a thousand people come that's a thousand potentially new customers to add on the already growing pile of existing customers and over time that builds up and before you know it even your small little organization has a database that has tens of thousands hundreds of thousands of records and it's then that you usually end up finding the problems and the problem is it's kind of too late at that point you're already committed and you don't know what to do to solve it because you've always just said this is a black box it just works so that's number two the reasons why I I'm frustrated by a lot of entity Frameworks mostly around the training number three is using C sharp to do sql's job SQL Server is excellent at storing data at retrieving data at sorting data at filtering data that's its whole job it's been optimized there's been a ton of work put into making it as efficient as possible when you take that away from SQL server and say just give me everything and then I'll do the filtering sorting and manipulating on the C sharp side you create a much slower application this is your fault this is not the Tool's fault because the tool can say hey do all that work on SQL and then give me the results and I'll do maybe some fine tune manipulation but for the most part I'll let SQL do the big stuff that's what it's designed to do and if you use it improperly you can create some real performance issues and again it's not the issue of the tool it's the issue of a person using the tool so number four and the kind of last in this category of why I'm frustrated when people use Entity framework it's all about using it wrong um wasting resources just because you can I had people tell me well sure that query takes half a second to run but who cares it's half a second No One's Gonna know it's half a second that's a waste of resources if you could do it in a tenth of a second then you should because when it comes to again this is not a problem now it's not a problem in development it's not any problem in production when you first start out it's not going to become a problem until you grow and again it's not until you get into production and are down the road to where it's too late you've already committed to this and now all of a sudden you realize oh no we have some performance problems and the solution I often see is companies saying well at this point we just throw more resources at it we buy bigger servers we get more RAM we create you know more beer clusters for our database we expand out we we grow out because that's all we can do and they spend more and more and more money trying to get ahead of this resource waste and that can work for a while but what we want to do is it's just kicking a can down the road because at some point your server can't get bigger you're out of Midas spend on more Cloud resources and you're stuck with an application that's just fundamentally broken and you have to hire an expensive consultant to come in and fix your problem so if they can so wasting resources just because you can I don't approve that I want you to create an efficient app right up front you do not have to over engineer I am not saying that you have to create an application that can scale like Google when it's just going to serve 100 people I'm not saying that but I also don't think that you should just waste resources just because why not all right so that's my my reasons why I struggle with a lot of Entity framework usage it's not the tool it's about the person using the tool and quite frankly I think that the tool does have some limitations I think the tool is not the solution for every problem in fact I think there are other solutions that do a better job in a lot of cases so this is where I focus a lot on Dapper people say why would you focus on Dapper well first of all it forces you to learn SQL what Dapper does if you're not familiar is it's very very very small all it does is says okay give me the query I'll go tell SQL to run that query and when SQL returns a result set I will then map that to the model you have selected that's it that's all it does and if you say I want to do an insert it's even easier take the query take any data you want to associate like parameters combine that together send it to SQL and say run that that's that's all it does and so it's very very simple it doesn't do a lot which does force you to write that SQL you have to write the select statement or the store procedure or the the whatever query you want to create so you have to know about SQL when it comes to creating your tables you have to know how to do that you have to figure out what's my Optimum structure So when you say hey first name field how many characters should we allow for that should it be an N varchar or a varchar should it be 50 characters or a hundred what's what's the what's our range there you have to think through that when you use Entity framework you just say uh string property string first name well what size is that well it turns out it's n varchar Max meaning it's massive it shouldn't be that way you should restrict that and there's ways of doing the identity framework but you have to decorate that well did you know that maybe not because you didn't go deep enough into the empty framework you don't know a tool well enough to use it efficiently and effectively and so you just kind of throw resources at it saying hey no big deal that I have five six seven columns that are in varchar Max yeah it's a big deal you shouldn't do that so it forces you when you use Dapper to learn SQL you should know that if you're going to work with SQL you should know how the tool works that you're using it shouldn't be magic now this does make debugging and optimizing SQL much easier let's say and this is my preference you use a store procedure for your call so in your c-sharp code you say use SP get first you know person get first name whatever and then that calls in a SQL and SQL says here is the the statement to run and it runs that statement gives the return value well imagine for a minute that that was not an optimized query and you're trying to figure out why is our database going slower why is that why are we getting results so slowly and you look at your queries and say oh this query being run a lot and it returns very very inefficiently it's returning in half a second let's see if we can optimize this maybe you need an index well you figure that out you say oh actually what we need to do is we need to not include this field we don't need anyway and that will not only return less data but it will also use a different index we already have in place well you can do that on the SQL side when you look at the SQL logs it's going to say this store procedure takes this much time you can look at that and say I can change that store procedure to be more efficient you do that and now everything calls that will be more efficient if you were to use Entity framework you would see this ad hot query it's quite large that doesn't necessarily tell you where it was called from or who called it or who created it you can't even search the source code for the actual query statement because it gets created dynamically and then we try to optimize it you figure out you have to modify a field well go into the c-sharp code make a change that's not great so it makes it much easier to debug and optimize your SQL if you have everything in SQL now number three Dapper is extremely simple one of the things you'll find as a software developer is when you start off you write simple code to solve simple problems when you become a mid-level developer you end up writing complex code to write to solve complex problems and as you progress into a senior developer role you will learn how to create simple code to solve complex problems well Dapper is simple code to solved a complex problem this is something very simple to do in fact in most of my applications where I'm just doing data access it's not nothing complicated with even c-sharp side transactions just just transactions in SQL and just you know insert create read update and delete crud statements well I can do this all with one line of code so my my actual call is just one lab code get this data that's it here's a store procedure here's the parameters go and does it super simple makes it easy to read easy to understand with that it's as efficient as possible it's not about wasting resources it's about wasting as few resources as possible it's practically as fast as direct Ado access so when you're calling your data access is practically as fast as the bare metal way of calling it so the the big cost difference there is then mapping to the model and that's the one thing that you want to do has something else to do because it's a little more complicated and to do it efficiently you want to do it through dapper so finally it defaults to SQL doing the SQL work so when you have that should C sharp do it or SQL do it Dapper by default says well I'm not doing it either you're fully doing it on a C sharp side as in you ask for all the data and then do the work which why would you do that or you can let SQL do us his job so you're going to say hey here's a query which filters sorts and gets just the date you need and then it returns just that data so the default makes it more efficient so that's that's why I lean towards Dapper that's why I show off Dapper the most that's why I think if you're a newer developer you should absolutely use Dapper and not Entity framework that I think the Entity framework is a complex tool that does complex things that requires a complex set of knowledge and so yes you can absolutely use it yes you can absolutely use it to be very very effective it's not about is Anthony framework faster or slower than Dapper that's not what it's about there if in in closed circumstances they're practically the same okay Dapper is technically a little faster but where you see the speed is how you use it again most of the reasons why I don't like Entity framework have to do with how people use them I am working with a non-profit right now that is pulling their hair out because of how enthy framework works because it's not efficient because it's causing problems they can't track down easily and it makes a mess with their data as not just as simple as saying oh well now let's use Dapper you can't just do that it's already too late there's a commitment now to doing things with Entity framework but it's kind of embedded in how they do things so I've seen this over and over where organizations struggle with Entity framework not because the tool itself but because of how the tool was inefficiently and ineffectively used by people who didn't know all they needed to know about the tool and about the long-term consequences of the choice they were making which is why I encourage the use of Dabber because you're faced up front with a lot of those long-term decisions and you're not locked in where if you say hey that was a bad store procedure ran a better one just rewrite it even in SQL and have it just be the new store procedure you're not locked in as much okay so the question which is better well they're different tools for different circumstances you can use both in fact the team that created Dapper which is stack Overflow that's the the stack exchange organization they create dapper they don't just use Dapper they are about 50 50 last time I checked between Anthony framework core and Dapper so they kind of use both because both can be valuable in their own way in their own circumstances now for me what I prefer well my preference is Dapper it's simple it's easy to use it's easy to debug it's got better database security I've got a whole video on store procedures check that out because in that video I show you how to lock in a database to only call store procedures you can't do those Entity framework it needs access to everything so and the lastly it's not a black box it's it's very easy to see what's going on because everything is transparent and open there's not auto-generated code hiding behind the scenes and you understand what's happening because it's so simple so that's my preference you don't have to use it but I encourage you if you go to the Entity framework route you need to be very good at it you need to understand the long-term consequences and you need to understand how to optimize it very very well all right so a choice is up to you I do not hate Entity framework I do not hate people that use it absolutely go use it just be careful and cautious I have seen too many train wrecks not to encourage you to be cautious all right so that's my opinion that's my thoughts on Entity framework versus Dapper thanks for watching as always I am Tim Corey [Music]
Info
Channel: IAmTimCorey
Views: 46,125
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6
Id: 7ZcbHFmgVAI
Channel Id: undefined
Length: 21min 48sec (1308 seconds)
Published: Thu Feb 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.