Latest / DevQuestions with Tim Corey / 139 What Are Your Thoughts on Entity Framework Core vs. Dapper?
Transcript
- 0:00SPEAKER_00: Welcome to the Dev Questions Podcast with Tim Corey.
- 0:03SPEAKER_00: Join us each episode as we tackle the questions you are asking about a career in software development, understanding the industry, and new technology.
- 0:12SPEAKER_00: If you're just starting out or you want to grow stronger as a developer, this is the place to get your questions answered.
- 0:19SPEAKER_00: Now, here's your host, expert developer and online educator, Tim Corey.
- 0:24SPEAKER_02: What are your thoughts on Entity Framework Core versus Dapper?
- 0:29SPEAKER_02: Which one's better?
- 0:30SPEAKER_02: These are questions that come up a lot and one I thought I'd address in today's episode of DevQuestions.
- 0:36SPEAKER_02: Now, if you have a question you want to get answered, the best way to get that answered is to go to suggestions.iamtimcorey.com, leave that suggestion there, or upvote an existing one, and hopefully you'll see your question answered on a future episode of DevQuestions.
- 0:52SPEAKER_02: Now, I want to get this out of the way right away.
- 0:55SPEAKER_02: Both EF Core and Dapper are excellent tools.
- 1:00SPEAKER_02: Both have their place.
- 1:02SPEAKER_02: This is something that especially my audience might not think I would say.
- 1:07SPEAKER_02: Some people think that I hate Entity Framework, and I don't.
- 1:10SPEAKER_02: It's an amazing tool.
- 1:12SPEAKER_02: But I want to be clear on my thoughts about Entity Framework.
- 1:17SPEAKER_02: This is going to be my opinion.
- 1:20SPEAKER_02: You get to have your own opinion, but I have a lot of facts to back up my opinion.
- 1:24SPEAKER_02: And I would encourage you to think through it and have your own experiences as well.
- 1:28SPEAKER_02: Now, let's talk first about my experience so you know why I have the opinions that I do.
- 1:34SPEAKER_02: 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.
- 1:54SPEAKER_02: I have seen them in production.
- 1:57SPEAKER_02: I have seen the tools they have built.
- 1:59SPEAKER_02: I have seen the systems 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.
- 2:09SPEAKER_02: One of the two.
- 2:10SPEAKER_02: And so I have been a part of a number of organizations that have used Entity Framework.
- 2:17SPEAKER_02: I have seen how they have done it.
- 2:19SPEAKER_02: I have also seen a lot of SQL databases and how they've been structured, how they've been designed, how they've been used, and what the pros and cons are of all of those.
- 2:30SPEAKER_02: I have worked with organizations where things have gone great, and I have worked with organizations where things have gone horribly.
- 2:37SPEAKER_02: I have seen what caused what has gone horribly, and I have seen what has caused things to go great.
- 2:44SPEAKER_02: So this is where I come from when I talk about Entity Framework and DAPR, because I have seen a lot.
- 2:52SPEAKER_02: So here are the things that I hate when it comes to how entity framework is talked about or even used.
- 3:01SPEAKER_02: Number one, people say that entity framework is useful, so you do not have to know SQL.
- 3:09SPEAKER_02: 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.
- 3:23SPEAKER_02: That is not only incorrect, I think it's very, very dangerous.
- 3:29SPEAKER_02: I have seen this firsthand from organizations who have built everything in Entity Framework and say, well, SQL is just the black box that gets talked to from an entity framework.
- 3:41SPEAKER_02: We don't know how it works.
- 3:42SPEAKER_02: We just know that entity framework takes care of it all and it does its job.
- 3:49SPEAKER_02: That works fine sometimes.
- 3:52SPEAKER_02: But usually what happens is it works fine until you get to a certain point in your organization and your application.
- 3:58SPEAKER_02: When your organization grows, when you have a heavy day of traffic, when you have a certain growth in your organization, or when 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.
- 4:17SPEAKER_02: So that's one of my pet peeves with Entity Framework, is more about how it's taught.
- 4:24SPEAKER_02: Entity framework can be a great tool.
- 4:26SPEAKER_02: It is a great tool, but if you abuse it and use it incorrectly, it can be a very, very bad tool.
- 4:34SPEAKER_02: So you need to know how SQL works.
- 4:38SPEAKER_02: You need to know how it builds your tables, how it builds the queries, how it gets the information, and how your data is structured.
- 4:48SPEAKER_02: Because if you know that, you can optimize it.
- 4:51SPEAKER_02: If you know that, you can help it make better choices.
- 4:55SPEAKER_02: But if you just say it's a black box, well, that's not a good thing.
- 4:59SPEAKER_02: It's your application is built upon data.
- 5:03SPEAKER_02: That's really the entire reason for your application to exist.
- 5:07SPEAKER_02: It's to get data and to give data.
- 5:11SPEAKER_02: So if you are an airline, your your whole application is around getting people onto flights, getting that 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.
- 5:29SPEAKER_02: Um, so that's your whole application revolves around that data.
- 5:33SPEAKER_02: If the one thing you don't know well is how your data is actually stored and retrieved, that's a problem.
- 5:40SPEAKER_02: And saying, well, I know entity framework isn't enough because that's just a part of the overall system.
- 5:47SPEAKER_02: You need to know how it works on the SQL side.
- 5:50SPEAKER_02: Now, number two, not learn learning entity framework well enough.
- 5:55SPEAKER_02: Entity framework is a very deep tool.
- 5:59SPEAKER_02: It does a lot and it has a lot of depth to it.
- 6:02SPEAKER_02: But it can often be overlooked because of how quick you can get into Entity Framework.
- 6:09SPEAKER_02: You say, well, I know enough to do the job.
- 6:12SPEAKER_02: And that's not good enough.
- 6:14SPEAKER_02: Because when you just know enough to do the job, that doesn't work forever.
- 6:19SPEAKER_02: And again, this is where I come back to my experience.
- 6:21SPEAKER_02: When I would come into companies where they're not having a good day, when things are not going well, it was not in development, usually.
- 6:29SPEAKER_02: It was usually in production, where they just did enough to get things working, and that worked well when they had 100 customers, when they had a thousand customers.
- 6:40SPEAKER_02: But now they have 50,000 customers.
- 6:42SPEAKER_02: And that may sound like a huge deal.
- 6:45SPEAKER_02: It's really not.
- 6:47SPEAKER_02: Because when a customer comes and buys one thing from you and then leaves and never comes back, that's still one.
- 6:54SPEAKER_02: So it accumulates over time.
- 6:57SPEAKER_02: 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.
- 7:08SPEAKER_02: And over time that builds up.
- 7:10SPEAKER_02: And before you know it, even your small little organization has a database that has tens of thousands, hundreds of thousands of records.
- 7:19SPEAKER_02: And it's then that you usually end up finding the problems.
- 7:23SPEAKER_02: And the problem is it's kind of too late at that point.
- 7:26SPEAKER_02: 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.
- 7:32SPEAKER_02: It just works.
- 7:34SPEAKER_02: So that's number two reasons why I'm frustrated by a lot of entity framework.
- 7:39SPEAKER_02: It's mostly around the training.
- 7:41SPEAKER_02: Number three is using C sharp to do SQL's job.
- 7:47SPEAKER_02: SQL Server is excellent at storing data, at retrieving data, at sorting data, at filtering data.
- 7:56SPEAKER_02: That's its whole job.
- 7:57SPEAKER_02: It's been optimized.
- 7:58SPEAKER_02: There's been a ton of work put into making it as efficient as possible.
- 8:04SPEAKER_02: 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.
- 8:17SPEAKER_02: This is your fault.
- 8:18SPEAKER_02: This is not the tool's fault.
- 8:20SPEAKER_02: 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-tuned manipulation.
- 8:28SPEAKER_02: But for the most part, I'll let SQL do the big stuff.
- 8:31SPEAKER_02: That's what it's designed to do.
- 8:33SPEAKER_02: And if you use it improperly, you can create some real performance issues.
- 8:38SPEAKER_02: And again, it's not the issue of the tool.
- 8:42SPEAKER_02: It's the issue of the person using the tool.
- 8:45SPEAKER_02: So, number four, and the kind of the last in this category of why I'm frustrated when people use entity framework, it's all about using it wrong.
- 8:54SPEAKER_02: Um wasting resources just because you can.
- 8:57SPEAKER_02: I had people tell me, well, sure, that query takes half a second to run, but who cares?
- 9:02SPEAKER_02: It's half a second.
- 9:03SPEAKER_02: No one's gonna know it's half a second.
- 9:06SPEAKER_02: That's a waste of resources.
- 9:07SPEAKER_02: If you could do it in a tenth of a second, then you should.
- 9:11SPEAKER_02: Because when it comes to, again, this is not gonna be a problem now.
- 9:15SPEAKER_02: It's not gonna be a problem in development, it's not gonna be a problem in production when you first start out.
- 9:20SPEAKER_02: It's not gonna become a problem until you grow.
- 9:23SPEAKER_02: And again, it's not until you get into production and are down the road to where it's too late.
- 9:30SPEAKER_02: You've already committed to this, and now all of a sudden you realize, oh no, we have some performance problems.
- 9:38SPEAKER_02: And the solution I often see is companies saying, well, at this point, we just throw more resources at it.
- 9:45SPEAKER_02: We buy bigger servers, we get more RAM, we create, you know, more beer clusters for our database.
- 9:52SPEAKER_02: We expand out, we we grow out because that's all we can do.
- 9:57SPEAKER_02: And they spend more and more and more money trying to get ahead of this resource waste.
- 10:06SPEAKER_02: And that can work for a while.
- 10:08SPEAKER_02: But remote doing this is just kicking a can down the road.
- 10:12SPEAKER_02: Because at some point, your server can't get bigger, you're out of money to spend on more cloud resources, and you're stuck with an application that's just fundamentally broken.
- 10:23SPEAKER_02: And you have to hire an expensive consultant to come in and fix your problem.
- 10:28SPEAKER_02: So if they can.
- 10:30SPEAKER_02: So wasting resources just because you can, I don't approve that.
- 10:34SPEAKER_02: I want you to create an efficient app rep front.
- 10:37SPEAKER_02: You do not have to over-engineer.
- 10:39SPEAKER_02: I am not saying that you have to create an application that can scale like Google when it's just gonna serve 100 people.
- 10:47SPEAKER_02: I'm not saying that.
- 10:48SPEAKER_02: But I also don't think that you should just waste resources just because why not?
- 10:54SPEAKER_02: All right.
- 10:55SPEAKER_02: So that's my reasons why I struggle with a lot of entity framework usage.
- 11:02SPEAKER_02: It's not about the tool, it's about the person using the tool.
- 11:06SPEAKER_02: And quite frankly, I think that the tool does have some limitations.
- 11:11SPEAKER_02: I think the tool is not the solution for every problem.
- 11:15SPEAKER_02: In fact, I think there are other solutions that do a better job in a lot of cases.
- 11:20SPEAKER_02: So this is where I focus a lot on Dapper.
- 11:23SPEAKER_02: And people say, well, why would you focus on Dapr?
- 11:26SPEAKER_02: Well, first of all, it forces you to learn SQL.
- 11:30SPEAKER_02: What Dapper does, if you're not familiar, is it's very, very, very small.
- 11:35SPEAKER_02: All it does is says, okay, give me the query.
- 11:38SPEAKER_02: I'll go tell SQL to run that query.
- 11:41SPEAKER_02: And when SQL returns a result set, I will then map that to the model you have selected.
- 11:48SPEAKER_02: That's it.
- 11:49SPEAKER_02: That's all it does.
- 11:50SPEAKER_02: And if you say, I want to do an insert, it's even easier.
- 11:54SPEAKER_02: Take the query, take any data you want to associate, like parameters, combine that together, send it to SQL, and say, run that.
- 12:02SPEAKER_02: That's that's all it does.
- 12:04SPEAKER_02: And so it's very, very simple.
- 12:06SPEAKER_02: It doesn't do a lot, which does force you to write that SQL.
- 12:11SPEAKER_02: You have to write the select statement or the store procedure or the the whatever query you want to create.
- 12:19SPEAKER_02: So you have to know about SQL.
- 12:22SPEAKER_02: When it comes to creating your tables, you have to know how to do that.
- 12:25SPEAKER_02: You have to figure out what's my optimum structure.
- 12:29SPEAKER_02: So when you say, hey, first name field, how many characters should we allow for that?
- 12:34SPEAKER_02: Should it be an Nvarchar or a Varchar?
- 12:37SPEAKER_02: Should it be 50 characters or a hundred?
- 12:40SPEAKER_02: What's what's the what's our range there?
- 12:42SPEAKER_02: You have to think through that.
- 12:44SPEAKER_02: When you use entity framework, you just say uh string property string first name.
- 12:49SPEAKER_02: Well, what what size is that?
- 12:51SPEAKER_02: Well, it turns out it's nvarchar max, meaning it's massive.
- 12:55SPEAKER_02: It shouldn't be that way.
- 12:56SPEAKER_02: You should restrict that, and there's ways of doing that with entity framework, but you have to decorate that.
- 13:01SPEAKER_02: Well, did you know that?
- 13:03SPEAKER_02: Maybe not, because you didn't go deep enough into the entity framework.
- 13:06SPEAKER_02: You don't know the tool well enough to use it efficiently and effectively.
- 13:10SPEAKER_02: And so you just kind of throw resources at it saying, hey, no big deal that I have five, six, seven columns that are Nvarchar Max.
- 13:18SPEAKER_02: Yeah, it's a big deal.
- 13:19SPEAKER_02: You shouldn't do that.
- 13:20SPEAKER_02: So it forces you when you use Dapper to learn SQL.
- 13:24SPEAKER_02: You should know that if you're going to work with SQL.
- 13:27SPEAKER_02: You should know how the tool works that you're using.
- 13:30SPEAKER_02: It shouldn't be magic.
- 13:33SPEAKER_02: Now, this does make debugging and optimizing SQL much easier.
- 13:39SPEAKER_02: Let's say, and this is my preference, you use a store procedure for your call.
- 13:44SPEAKER_02: So in your C sharp code, you say use SP git first, you know, person get first name, whatever.
- 13:51SPEAKER_02: And then that calls into SQL, and SQL says, here is the statement to run, and it runs that statement and gives the return value.
- 13:58SPEAKER_02: Well, imagine for a minute that that was not an optimized query.
- 14:02SPEAKER_02: And you're trying to figure out why is our database going slower?
- 14:06SPEAKER_02: Why is that, why are we getting results so slowly?
- 14:08SPEAKER_02: And you look at your queries and say, ooh, this query being run a lot and it returns very, very inefficiently.
- 14:14SPEAKER_02: It's returning in half a second.
- 14:16SPEAKER_02: Let's see if we can optimize this.
- 14:18SPEAKER_02: Maybe you need an index.
- 14:19SPEAKER_02: Well, you figure that out.
- 14:20SPEAKER_02: You say, oh, actually, what we need to do is we need to not include this field we don't need anyway.
- 14:25SPEAKER_02: And that will not only return less data, but it will also use a different index we already have in place.
- 14:31SPEAKER_02: Well, you can do that on the SQL side.
- 14:33SPEAKER_02: When you look at the SQL logs, it's going to say this store procedure takes this much time.
- 14:38SPEAKER_02: You can look at that and say, I can change that store procedure to be more efficient.
- 14:43SPEAKER_02: You do that, and now everything that calls that will be more efficient.
- 14:49SPEAKER_02: If you were to use entity framework, you would see this ad hoc query that's quite large, that doesn't necessarily tell you where it was called from or who called it or who created it.
- 15:00SPEAKER_02: You can't even search the source code for the actual query statement because it gets created dynamically.
- 15:06SPEAKER_02: And then when you try to optimize it, you figure out you have to modify a field.
- 15:10SPEAKER_02: Well, go into the C sharp code and make a change.
- 15:13SPEAKER_02: That's not great.
- 15:14SPEAKER_02: So it makes it much easier to debug and optimize your SQL if you have everything in SQL.
- 15:22SPEAKER_02: Now, number three, Dapper is extremely simple.
- 15:26SPEAKER_02: One of the things you'll find as a software developer is when you start off, you write simple code to solve simple problems.
- 15:33SPEAKER_02: When you become a mid-level developer, you end up writing complex code to write to solve complex problems.
- 15:40SPEAKER_02: And as you progress into a senior developer role, you will learn how to create simple code to solve complex problems.
- 15:48SPEAKER_02: Well, Dapr is simple code that solves a complex problem.
- 15:53SPEAKER_02: This is something very simple to do.
- 15:55SPEAKER_02: 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 transactions in SQL and just insert, create, read, update, and delete crudge statements.
- 16:11SPEAKER_02: Well, I can do those all with one line of code.
- 16:15SPEAKER_02: So my actual call is just one line of code, get this data.
- 16:20SPEAKER_02: That's it.
- 16:20SPEAKER_02: Here's the store procedure, here's the parameters, go.
- 16:23SPEAKER_02: And it does it.
- 16:24SPEAKER_02: Super simple.
- 16:25SPEAKER_02: Makes it easy to read, easy to understand.
- 16:28SPEAKER_02: With that, it's as efficient as possible.
- 16:31SPEAKER_02: It's not about wasting resources, it's about wasting as few resources as possible.
- 16:36SPEAKER_02: It's practically as fast as direct ADO access.
- 16:40SPEAKER_02: So when you're calling your data access, it's practically as fast as the bare metal way of calling it.
- 16:47SPEAKER_02: So the big cost difference there is the mapping to the model.
- 16:51SPEAKER_02: And that's the one thing that you want to do, have somebody else do because it's it's a little more complicated.
- 16:56SPEAKER_02: And to do it efficiently, you want to do it through Dapr.
- 17:00SPEAKER_02: So finally, it defaults to SQL doing the SQL work.
- 17:07SPEAKER_02: So when you have that, should C sharp do it or SQL do it, Dapper by default says, Well, I'm not doing it.
- 17:14SPEAKER_02: Either you're fully doing it on C sharp side, as in you ask for all the data and then do the work, which why would you do that?
- 17:22SPEAKER_02: Or you're gonna let SQL do its job.
- 17:24SPEAKER_02: So you're gonna say, hey, here's the query which filters, sorts, and gets just the data you need.
- 17:30SPEAKER_02: And then it returns just that data.
- 17:33SPEAKER_02: So the default makes it more efficient.
- 17:36SPEAKER_02: So that's that's why I lean towards Dapper.
- 17:39SPEAKER_02: That's why I show off Dapper the most.
- 17:41SPEAKER_02: That's why I think if you're a newer developer, you should absolutely use Dapper and not Entity Framework.
- 17:47SPEAKER_02: But I think the Entity Framework is a complex tool that does complex things that requires a complex set of knowledge.
- 17:55SPEAKER_02: And so, yes, you can absolutely use it.
- 17:58SPEAKER_02: Yes, you can absolutely use it to be very, very effective.
- 18:02SPEAKER_02: It's not about is Entity Framework faster or slower than Dapper.
- 18:07SPEAKER_02: That's not what it's about.
- 18:09SPEAKER_02: If in enclosed circumstances, they're practically the same.
- 18:13SPEAKER_02: Okay?
- 18:14SPEAKER_02: Dapper is technically a little faster.
- 18:16SPEAKER_02: But where you see the speed is how you use it.
- 18:21SPEAKER_02: Again, most of the reasons why I don't like Entity Framework have to do with how people use them.
- 18:28SPEAKER_02: I am working with a nonprofit right now that is pulling their hair out because of how entity 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.
- 18:44SPEAKER_02: And it's not just as simple as saying, oh, well, now let's use Dapper.
- 18:48SPEAKER_02: You can't just do that.
- 18:49SPEAKER_02: It's already too late.
- 18:50SPEAKER_02: There's a commitment now to doing things with Entity Framework, but it's kind of embedded in how they do things.
- 18:56SPEAKER_02: 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.
- 19:20SPEAKER_02: Because you're faced up front with a lot of those long-term decisions and you're not locked in.
- 19:26SPEAKER_02: Where if you say, hey, that was a bad store procedure, write a better one.
- 19:31SPEAKER_02: Just rewrite it even in SQL and have it just be the new store procedure.
- 19:36SPEAKER_02: You're not locked in as much.
- 19:38SPEAKER_02: Okay?
- 19:38SPEAKER_02: So the question, which is better?
- 19:41SPEAKER_02: Well, there are different tools for different circumstances.
- 19:44SPEAKER_02: You can use both.
- 19:46SPEAKER_02: In fact, the team that created Dapr, which is Stack Overflow, that's the Stack Exchange organization, they create Dapr.
- 19:54SPEAKER_02: They don't just use Dapr.
- 19:56SPEAKER_02: They are about 50-50 last time I checked between Entity Framework Core and Dapr.
- 20:02SPEAKER_02: So they kind of use both because both can be valuable in their own way, in their own circumstances.
- 20:11SPEAKER_02: Now, for me, what do I prefer?
- 20:13SPEAKER_02: Well, my preference is Dapper.
- 20:15SPEAKER_02: It's simple, it's easy to use, it's easy to debug, it's got better database security.
- 20:22SPEAKER_02: I've got a whole video on store procedures.
- 20:24SPEAKER_02: Check that out.
- 20:25SPEAKER_02: Because in that video, I show you how to lock in a database to only call store procedures.
- 20:30SPEAKER_02: You can't do that with NTV framework.
- 20:31SPEAKER_02: It needs access to everything.
- 20:34SPEAKER_02: So, and lastly, it's not a black box.
- 20:37SPEAKER_02: It's very easy to see what's going on because everything is transparent and open.
- 20:42SPEAKER_02: There's not auto-generated code hiding behind the scenes, and you understand what's happening because it's so simple.
- 20:48SPEAKER_02: So that's my preference.
- 20:50SPEAKER_02: You don't have to use it.
- 20:51SPEAKER_02: But I encourage you, if you go the entity framework route, you need to be very good at it.
- 20:57SPEAKER_02: You need to understand the long-term consequences, and you need to understand how to optimize it very, very well.
- 21:04SPEAKER_02: All right.
- 21:05SPEAKER_02: So a choice is up to you.
- 21:06SPEAKER_02: I do not hate Entity Framework.
- 21:09SPEAKER_02: I do not hate people that use it.
- 21:12SPEAKER_02: Absolutely go use it.
- 21:14SPEAKER_02: Just be careful and cautious.
- 21:16SPEAKER_02: I have seen too many train wrecks not to encourage you to be cautious.
- 21:21SPEAKER_02: All right.
- 21:22SPEAKER_02: So that's my opinion.
- 21:23SPEAKER_02: That's my thoughts on Entity Framework versus Dapper.
- 21:26SPEAKER_02: Thanks for watching.
- 21:27SPEAKER_02: And as always, I am Tim Corey.
- 21:29SPEAKER_01: Thank you for joining us for this episode of Deaf Questions.
- 21:32SPEAKER_01: Tim is committed to making it easier for you to become a developer.
- 21:36SPEAKER_01: If you would like to help make more content like this possible, please like, subscribe, rate, and share Deaf Questions.
- 21:43SPEAKER_01: You can also send your questions to questions at IamTimcorey.com.
- 21:48SPEAKER_01: Until next time, remember, you are too smart and your time too valuable to waste it making all the mistakes Tim did.
- 21:55SPEAKER_01: When you're ready to learn to think and code like a professional developer, head over to imtimcore.com and enroll in a course.