Simple C# Data Access with Dapper and SQL - Minimal API Project Part 1

  Рет қаралды 225,828

IAmTimCorey

IAmTimCorey

2 жыл бұрын

Minimal APIs are now the default in .NET 6. I thought that we should take some time and look at what they are, how they work, and what they might look like in the real world. To that end, we are going to spend two videos covering this topic. This video will cover setting up a SQL database and configuring Dapper for easy data access. We will set up a simple but full CRUD data layer so that when we build our minimal API in part two, we can concentrate on the API itself and not the setup. The great thing is that this lesson will work with almost any user interface, not just a minimal API.
Part 2: • Minimal API in .NET 6 ...
Full Training Courses: IAmTimCorey.com
Source Code: leadmagnets.app/?Resource=Min...
Mailing List: signup.iamtimcorey.com/

Пікірлер: 548
@IAm-MB
@IAm-MB 2 жыл бұрын
Hey Tim... I was watching your videos since sometime but never commented before. I can't resist myself this time. This is a really good video, easy to go along the flow. Nicely explained (as always). I am pretty sure second part will be as interesting to learn as this one.
@ilmche4573
@ilmche4573 2 жыл бұрын
That was such a great help for me, thanks tim! I am really new in c# and worked on a project for my company with ef core but now I am thinking to use dapper instead of ef core because of the advantages you mentioned. I just love this simplicity and the separation of concerns with dapper, also it seems to me much easier to understand than ef core.
@RobertLenior
@RobertLenior 2 жыл бұрын
Switched over to Dapper thanks to you Tim! You have been a great help to me. The way you explain things is incredible. Soo much easier than so many others and also your voice helps a lot. No funny accents and very human. Even the tiny errors and correcting them helps.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad my content has been helpful.
@thomasdewitt6719
@thomasdewitt6719 2 жыл бұрын
Tim, this was very informative and will make me go back and refactor one of my sample projects to get a firm grip on this subject to fully understand it
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Awesome!
@ZRide72
@ZRide72 2 жыл бұрын
This is amazing so far. Thank you Tim. Glad I found your videos.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@MrMarkbad
@MrMarkbad 2 жыл бұрын
Wow! Powerful way to setup data access. I am sold.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@turn1210
@turn1210 2 жыл бұрын
Your videos always seem to pop up just at the right time for me, I’m in two minds about using EF for my latest project, the cons are outweighing the pros at the minute on a technical level, I tend to lean towards stored procedures as I find all but the simplest db schema just bloats the code/queries when using EF, but, the team all use It exclusively, so this will help give another option.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@c130daddy
@c130daddy 2 жыл бұрын
Another great video sir, please keep them coming. For anyone watching wondering wether his courses are worth it, they totally are! I've been a developer for quite some time and I wish I'd found you and your work a long time ago sir. Can't recommend you to other developers enough! Thank you!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I appreciate the kind words.
@manuelgamezz
@manuelgamezz Жыл бұрын
Right now, I understand dapper, it's really cool to use to any database, SQL server + stored procedure is magic. Thanks Tim.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@delw1138
@delw1138 2 жыл бұрын
Thanks Tim, looking forward to part two!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Coming soon!
@marmazza1
@marmazza1 Жыл бұрын
Hi Tim, can't find the words to describe my thoughts now. Your contents are awesome, that's it, thank you very much!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@TheEamonKeane
@TheEamonKeane 2 жыл бұрын
Beautifully simple solution. Thanks Tim!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@haihai2999
@haihai2999 2 жыл бұрын
FINALLY. My most waiting Topic this month. Thanks Tim
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@waynehawkins654
@waynehawkins654 Жыл бұрын
Thanks Tim. Amazing video. It's changing my workflow and now do a mix of EF and Dapper. EF to build my database tables, Visual Studio database project to build my Stored Procedures and Dapper to do all the CRUD. The best of both worlds, or maybe good and evil being nice to each other.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@SpiritVector
@SpiritVector Жыл бұрын
Just wanted to say thank you for helping me learn about web development in general. I will soon contribute to this awesome cause!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@soroushsaghafi8473
@soroushsaghafi8473 2 жыл бұрын
That was a great video. Thanks for the good content, organized presentation, clear voice and accent. Also the last but not least, thanks for NOT using Entity Framework.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@JasonTira
@JasonTira 2 жыл бұрын
Its like you are reading my mind on what I want/need
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Awesome!
@amirhosseinerambue1214
@amirhosseinerambue1214 2 жыл бұрын
OH MY GOD , thank u so much , this is very handy , i think every beginner dev should watch this video , thanks bro
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@integrsolutions7064
@integrsolutions7064 2 жыл бұрын
Hey Tim, I am a big fan of you - and even suggest your videos to newbies. I am applying Dapper to my main project, I did a basic dbcontext myself with MySQLConnector (that I could type Data.Table(procedureQuery, parsObjectArray). It worked well by now but everytime I need to add something is still painful. With your videos I have learned coding patterns like nowhere. Keep it up! ♥
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Excellent!
@bernardlogador5364
@bernardlogador5364 2 жыл бұрын
Thanks Tim for this! Exactly what i need at the moment
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Glad it was helpful!
@adisakyfx
@adisakyfx 2 жыл бұрын
I really need part 2. Waiting for it. Good job.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@hutchm92
@hutchm92 4 ай бұрын
Outstanding work here. This has help me design a clearer roadmap to migrate framework project to core replace linqtosql Orm. Thanks again!
@IAmTimCorey
@IAmTimCorey 4 ай бұрын
Thank you!
@niangel2704
@niangel2704 2 жыл бұрын
Excellent video Tim!! The only weakness that I could think of, of this approach, is the lack of a proper way to get back the Id of the newly created object, by hitting the DB once with the Insert stored proc!! I really value your opinion about this!! Thanx again Tim, for enlightening us with your in depth knowledge of what we all love!! I look forward to hearing from you...
@rolfherbert2051
@rolfherbert2051 2 жыл бұрын
If you are using an SP you can return an integer value or if you switch on NOCOUNT you can return any data such as SELECT SCOPE_IDENTITY(), or SELECT FIRSTNAME, LASTNAME FROM USERS WHERE ID = SCOPE_IDENTITY() to get back the ID. If you were writing your own SQL code you can combine multiple statements so "SET NOCOUNT ON; INSERT INTO USER (FIRSTNAME, LASTNAME) VALUES (@0, @1); SELECT SCOPE_IDENTITY()
@Benexdrake
@Benexdrake Жыл бұрын
I love working with SQL and this Video was Awsome, thank you.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@sainsoftsoftwares2302
@sainsoftsoftwares2302 2 жыл бұрын
I have been searching .net core web api from the last 2 months and my search end here It help me a lot, Thanku you very much for this greate informational videos.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad it was so helpful.
@mahmoudalballah3387
@mahmoudalballah3387 2 жыл бұрын
Amazing as always!
@michlef774
@michlef774 2 жыл бұрын
Very nice presentation. Thanks for sharing your Knowledge. Keep going........
@deltaphilip8611
@deltaphilip8611 Жыл бұрын
When I want to learn something new, Corey is my go to trainer. To show my appreciation I will purchase one of his classes. I learned working with SQL DB's with EF. I like EF, but it made my SQL programming weak. Learning Dapper my alleviate that handicap.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I’m glad my content has been helpful.
@hamzaanushath
@hamzaanushath Жыл бұрын
This is my 10th hour of watching your explanation. Mind-blowing ❣
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I’m glad they have been helpful.
@MaddCoder
@MaddCoder Жыл бұрын
Awesome video, this has really helped me with my new project. Thanks so much Tim.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@philipvandenheever2084
@philipvandenheever2084 Жыл бұрын
Brilliant as always Tim.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@1kuruvchi
@1kuruvchi 7 ай бұрын
An absolutely great tutorial, thank you, Tim!
@IAmTimCorey
@IAmTimCorey 7 ай бұрын
You are welcome.
@FabioPereira-dp4bs
@FabioPereira-dp4bs 2 жыл бұрын
Thanks Tim! You rock man!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@girornsveinsson7970
@girornsveinsson7970 2 жыл бұрын
I really like the Contrib extension of Dapper. It allows us to write generic CRUD methods in the case of this video to the SqlDataAccess class and then we don't have to write CRUD stored procedures for every table unless there are some specific side effects that are needed in the sprocs. I think it saves a lot of coding time but of course there might be some reasons such as access control and maybe as you mentioned, performance which would require stored procedures. In my experience though, the Contrib methods are just fine.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I haven't used that extension before in one of my videos. I'll have to change that. Since it does not use pre-written procedures, it will make it a bit tougher to debug slow queries in SQL, but that seems like a bit of a non-issue with simple queries.
@JasonTira
@JasonTira 2 жыл бұрын
@@IAmTimCorey I would like to see this, count this as a +1
@girornsveinsson7970
@girornsveinsson7970 2 жыл бұрын
This is an example of how I implemented generic GetAll and Insert methods using Contrib: public async Task GetAllAsync() where T : class { using IDbConnection connection = new SqlConnection(_connectionString); var rows = await connection.GetAllAsync(); return rows; } public async Task InsertAsync(T entity) where T : class { using IDbConnection connection = new SqlConnection(_connectionString); var entityId = await connection.InsertAsync(entity); return entityId; } In this case I would then use this in the UserData class like so: public async Task GetUsers() { var users = await _sqlDataAccess.GetAllAsync(); return users; } Note that the UserModel class need to have the table decorator from Contrib: [Table("User")] public class UserModel { //... }
@aythienutties6223
@aythienutties6223 9 ай бұрын
Hello Corey your videos ready amazing.. since I've been watching these videos I have broadly understanding in programming
@IAmTimCorey
@IAmTimCorey 9 ай бұрын
Glad you like them!
@thomstunes6485
@thomstunes6485 2 жыл бұрын
Exactly what I've done after seeing your older tutorial on dapper as my data access layer. I'm curious to see if my approach is close than yours then!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Sounds great!
@thomstunes6485
@thomstunes6485 2 жыл бұрын
@@IAmTimCorey The main difference is that I did not isolated DataAccess into its own DLL. I've put all objects and calls through Dapper into the API's models. Which I guess is fine for me. Small DB, only for our entreprise using... The other big difference is the technical level but I cannot compete with God himself :)
@kevingates5851
@kevingates5851 2 жыл бұрын
You always have timely videos.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I'm glad.
@crispaul27
@crispaul27 Жыл бұрын
Wow... this tool is soooo awesome!!! How did I survive for so long without it!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I am glad you found it.
@scotttct
@scotttct 10 ай бұрын
Great Tutorial, Thanks. Looking to speed up my web that utilize EF6, Wow, all in an hour. Tim you are awesome!
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
You are welcome.
@claytonmyers6211
@claytonmyers6211 Жыл бұрын
Hi Tim, In your advanced Dapper tutorial, you showed how to combine multiple datasets with Dapper. Do you know of a good way to write a generic implementation as done in this video, but capable of joining multiple datasets? If not, what would be your preferred way to handle such an issue (IE: Loading a user model which may have a phone model as a property)
@eloyrolandocanchanyabalbin4692
@eloyrolandocanchanyabalbin4692 2 жыл бұрын
Thanks a lot sensei Tim Corey, regards from some place in Perú.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome!
@TheBKTaco
@TheBKTaco 2 жыл бұрын
Hi Tim. Great video as always. I just finished part 2 and decided to try to implement another class that implements the DataAccess interface and use MySQL. I've entered a second connection string in the appsettings.json file, but it always tries to grab the "Default" one instead of the new one I've added for MySQL. I put in a breakpoint and inspected the value for the connectionId and found it to be "Default" even though my new MySqlDataAccess class is setting it to something else. I think it's doing this because it's being assigned in the interface itself. If I remove it from the interface then I don't have the correct number of arguments when the UserData class calls the new implementation. If I add it in the UserData class when it calls the DataAccess then every method needs to know the connection string name and changing it in the future would need it to be changed everywhere. Is there a way to get this to work the way it was intended, or am I just doing something wrong?
@chidieberelevi3344
@chidieberelevi3344 2 жыл бұрын
Thank you for this Tim 🚀🚀 really helpful
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@swordblaster2596
@swordblaster2596 2 жыл бұрын
Excellent "starting from scratch" video.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@family--A
@family--A 2 жыл бұрын
Thank you very much, it was really helpful! Could you please add dynamic sorting, paging and filtering on part 2? Or at least point me to some example of how this could be achieved.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
That isn't in the plan for the next video (which is already recorded). However, I'll keep that in mind for the future.
@tosinakinyemi3948
@tosinakinyemi3948 2 жыл бұрын
Thanks' Tim for another awesome video.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@DiscipleW
@DiscipleW 2 жыл бұрын
Can't wait for part 2
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You won't have to wait long.
@scotttct
@scotttct 10 ай бұрын
Tim Love using your videos to learn proper coding patterns and new technique, after this I wonder what you would recommend for data security, already have an Encryption Library, so any help with a proper pattern to secure the api other than that would be awesome.
@ralphgoyarts8930
@ralphgoyarts8930 Жыл бұрын
Hi Tim, I brushed up my knowledge of SQL and SQL server in your “SQL Databases From Start to Finish” course and I learned how to do most of the SQL Server development in the “System.data.SqlClient”. This helped me a lot! The SqlClient draws the database into the application development environment and offers a single source development here. I am fully adapting the Only Stored Procedure approach to protect my data. I consider the relation ships between tables as an important part in that as it guarantees the relational characteristics of the database against programmer mistakes. But I am missing the relationship definitions in the SqlClient. Is there a way to overcome this? I Appreciate your videos, keep them coming.
@BinaryNexus
@BinaryNexus Жыл бұрын
This is awesome! Great video! I never take enough advantage of generics to be honest.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@RUSSWILSONII
@RUSSWILSONII 2 жыл бұрын
Wonderful first video, Tim! Thank you! For the insert operations, isn't in best practice to return (at a minimum) the ID of the object that was created, if not the entire new UserModel itself? I'm just wondering if practices have changed or if I have misinterpreted things along the way. Maybe I'm conflating the behavior of this w/ a POST operation for a RESTful API, which would typically return the object being created, but I'm assuming that's what this DataAccessLayer would be used for in part 2 (a RESTful API implementation?). Thanks for all you do for the community!
@RaMz00z
@RaMz00z Жыл бұрын
In a Restful API, a Post doesn't return an object, it returns the URI for the newly created object. Often the caller just wanted to create somthing, why bother them with a (potentially big) new object in return that will take place on the network ? As for inserts in DBb, that is what SQL does, and what EF does as well, so I would find that logical. Particurlarly Updates actually, more than Inserts.
@therealrolanddeschain9253
@therealrolanddeschain9253 2 жыл бұрын
Ok, probably I'm just weird for asking, but could you not somehow default the parameters to null if you don't need one, or use a single parameter (e.g. 'ID') directly? I assume this is a non-issue, but the new-ing up of dynamic objects makes me a bit uneasy. For multiple params it makes perfect sense to me.
@Kevin-mb4xf
@Kevin-mb4xf 2 жыл бұрын
Hey Tim, thanks for making this video. I was looking for a up-to-date tutorial on dapper. Could you tell us when you will release part 2?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Next week.
@Kevin-mb4xf
@Kevin-mb4xf 2 жыл бұрын
@@IAmTimCorey thank you!
@mikebreeden6071
@mikebreeden6071 2 жыл бұрын
Fantastic... I'm going on to the next one... I'll have to pay for another class of some sort. Cash is almost as flattering as imitation...
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@chaikuanhong
@chaikuanhong 4 ай бұрын
Excellent video
@IAmTimCorey
@IAmTimCorey 4 ай бұрын
Thanks!
@JeetKhinde
@JeetKhinde 2 жыл бұрын
Thanks for a great video. At 1:08:00, I guess it might be better in the long run if you return inserted record after inserting it in DB and the same is true for the update and delete method.
@princeonukwili996
@princeonukwili996 Жыл бұрын
Thank you so much sir...I never really liked the idea of using ORM's in my .NET apps, but from what I've seen in this tutorial, I thing I'm gonna start implementing Dapper in any new application I'll develop😌.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Great!
@Mercino311
@Mercino311 2 жыл бұрын
Hi Tim, do you have any resources for understanding how to work with Dapper with Identity? Seems to me like a complete overhaul of their interfaces may be required but I'm afraid I might leave vulnerabilities. I really like the idea of using dapper and SQL Data Tools but can't seem to commit to it without some understanding of how to work without EF and identity. Small Edit: Just to add some context. For me this would include working in MVC with EF and Identity.
@RC-uq4lw
@RC-uq4lw 10 ай бұрын
Great video. If I wanted to write integration tests for this database, what approach would you suggest for keeping the test DB schema synced with the prod version (or db project) and for resetting the DB to a known state between tests. With EF, this is fairly straightforward, but coming up with an approach to use without EF is a little tricky. Any ideas?
@kawthooleidevelopers
@kawthooleidevelopers Жыл бұрын
Wow... love this workflow. I honestly have been looking for something that is easy to work with. I've tried FluentMigrator and the DB initializer technique and I think this is a best approach for me now, since I don't want to use EF Core. Every other course just use EF core but I want my codes to be optimized, thus Dapper. However, I find it clumsy to work with it when I need to make changes to the models or just simply seed more data. Thanks for the great content. I've canceled my every other subscriptions. I will sign up to your course soon.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@roncoy887
@roncoy887 Жыл бұрын
Hi Tim, Thanks for your great tutorials, I am an enthusiast rather than active developer, you have advised the seperation of identity context from data contextand I understand why, do you have anything on how to implement that in an .net core app, i.e. do you have 2 active contexts at the same time. Thanks again Ron Coy
@muhammedeminokcu1327
@muhammedeminokcu1327 2 жыл бұрын
Awesome content for beginner to be a LEO ;). Thank you.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@rafaelrodriguessilva9994
@rafaelrodriguessilva9994 Жыл бұрын
Great video! Thanks!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@RajashekarReddydasari
@RajashekarReddydasari Жыл бұрын
Thank You so much. really helpful video
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@xchanmolx
@xchanmolx 2 жыл бұрын
Thanks Tim
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@selahattinkaradogan2744
@selahattinkaradogan2744 Жыл бұрын
very useful video for me thanks tim
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@rolfherbert2051
@rolfherbert2051 2 жыл бұрын
Hi there, Thanks for all the resources. I know alot more about MSSQL than I do about .NET so thanks for getting me learning new skills. Few comments about stored procedures; stored procedures are not inherently faster than ad-hoc statements, all plans are compiled and cached ad-hoc or otherwise. MSSQL will consider dynamic SQL to be a different query and so will create a new plan for every execution - but if you use parameters (please god use parameters) then the query plan is reused for each different parameter. If you wrote this sql as a dapper query 'SELECT FIRSTNAME, LASTNAME FROM USERS WHERE FIRSTNAME = @0' and substituted in the parameter at run time then the query plan would be reused regardless of which parameter is used. You get parameter sniffing issues with both adhoc and SPs, where the same plan is grossly wrong for different parameters but you are definitely using the same query plan. If you write dynamic sql eg "SELECT FIRSTNAME, LASTNAME FROM USERS WHERE FIRSTNAME = '" + firstName + "'" then you would produce a different query plan for every query. Parameter sniffing is where the plan is optimised for the first parameter used but maybe grossly inefficient for other parameters..you get weird effects where you run the query in SSMS and it runs quick but your app runs slow. Its a whole different topic. Stored procedures are not faster than parameterized queries...its the same to the engine. Replicating simple SQL statements (even update ones) in a SP is no faster and if you know SQL is probably slower to create. What stored procedures definitely do is act as a gatekeeper to your database and prevent unstructured data access. Thanks again for producing so many quality learning resources
@rolfherbert2051
@rolfherbert2051 2 жыл бұрын
for the pre-script reasoning....the EXISTS keyword is used here rather than say IF (SELECT COUNT(*) FROM [USER]) > 0 because EXIST will stop retrieving rows as soon as it finds one...so IF NOT EXISTS (SELECT 1 FROM [USER]) will only ever return one row regardless how many rows there are. Its the exist keyword which is making all the speed difference not the 1 or * which if you used in an EXIST statement would be an unmeasurable difference. In normal selects * would be slower cos it has to enumerate the columns first. COUNT(*) I believe always produces an index or table scan so it could be much slower if the table was large even with an exist (it is only returning a single row anyways)
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks for sharing. I agree on the stored procedure vs ad-hoc caching. What I believe I was trying to communicate is that stored procs that are used are always cached (after the plan is created), whereas ad-hoc isn't always cached (if the signature changed) so you get a 100% vs a possible 100% cache reuse. Also, you are right about exists. 👍
@gabrielvelazquezrios7324
@gabrielvelazquezrios7324 Жыл бұрын
Thank you so much!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@AliAktepee
@AliAktepee 2 жыл бұрын
Hey Tim, Another great tutorial thank you. But I have one hesitation, if I recall correctly, PostgreSQL does not use dbo prefix? So I will have to re-write a separate UserData for it? Or is there a shorter way? I can probably do something in DataAccess class like if connectionID == "Postgres" then replace "dbo." with "" but I don't think this is the right way to do it. Maybe you mentioned this in Part-2 , moving to that one now. Thanks again for this great tutorial.
@arianhaghani3452
@arianhaghani3452 9 ай бұрын
Thank you very much for this awesome video 😇
@IAmTimCorey
@IAmTimCorey 9 ай бұрын
You are welcome.
@tresaidh3y90
@tresaidh3y90 2 жыл бұрын
Hey Tim once again another beautiful video. Do you have a video explaining Lambdas? The process seems similar to Java however your tutorials are so great I would love to see one on Lambdas expressions
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@user-hq8rh1fo8h
@user-hq8rh1fo8h 9 ай бұрын
Tim, Great video! I love the idea of using generic data access with dapper. But, I don't understand how I can use generic data access with complex models (i.e. models that have some number of sub-models within them). For example, a customer model where the prefix isn't just a string... It is a prefix model. ...and a suffix where the suffix is a suffix model. Using Dapper, is it possible to use just one generic data retrieval function regardless of how many sub-models there are within my model? Thank you so much for your videos. Looking forward to your reply. :)
@IAmTimCorey
@IAmTimCorey 9 ай бұрын
That takes developing a bit of logic, but that's normal. Think about how SQL stores data. It stores data in rows. How do you get a complex object back in one row? You can't. What you need to do is either create a query that puts all of the data in a row (which would be a model without sub-models) or you need to do more than one query. Let's talk through an example of more than one query. Let's say you have a set of people and those people have one or more addresses per person. OK, so the first query would be "get all the people you want". That gives you the "main" model's data but leaves the List empty in each PersonModel. Now, do another query that is selects all of the addresses for all of the people selected in the original query. Then in C#, use LINQ to put those AddressModels into the correct PersonModels. That seems like a lot of work, but that is extremely efficient compared to what EF is doing. Also, when you do that, you start to rethink if you need all of that data. Odds are you don't actually need it all. That makes your application even more efficient. For instance, maybe you don't look at each person's list of addresses but you do want to know how many addresses they have. Cool. You can return that number as a column in the first query. Then, if you do want to actually look at the addresses a person has, you can load just those addresses up when requested.
@Astral100
@Astral100 Жыл бұрын
I wonder if it is possible to unit test db layer with this approach same as you can with Entity Framework by using memory implementation of EF? That is one benefit I think EF has over every other approach (as far as I know) is that you can easily test all the operations that manipulate the database.
@JasonTira
@JasonTira 2 жыл бұрын
After watching the whole video, and following along of course, it is very similar to your older video with a ton of wonderful little tidbits thrown in for the updated versions, and looks like a bit cleaner code as well. I still need to take your dapper course to get a better understanding of what is available. The quick intro to the stored procedures covered more than I thought it would in such a short time. I would like to see a 10 min video on how to change this DataAccess layer over to a different db like you mention. My current work process is using docker with a restored db from a backup from your video ( kzbin.info/www/bejne/j5uZapaNeqySj9E ), setting up in the appsettings.json and appsettings.development.json so when developing/updating the application ( also from your videos, I believe the paid course - worth every penny ). I just have to make sure I start the container, then when I publish it out it just moves to the live db. This gives a great way for me to just spin up a backup and replicate any issues the users are seeing in a safe environment for me to break.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Awesome! Thanks for the suggestion. It sounds like you are getting a lot of value out of my content.
@JasonTira
@JasonTira 2 жыл бұрын
@@IAmTimCorey very much so. I can't thank you enough.
@MeJirr
@MeJirr 2 жыл бұрын
Thanks dude !
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@Barto2You
@Barto2You 2 жыл бұрын
Hey Tim, great video again! What's your advise on using a bit more complex sql statements with the use of transactions in a DataAcessLayer. or do you have some example's on this topic?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I did use a transaction in the TimCo Retail Manager series here on KZbin when we stored the sales data (sales record and sale detail records). If possible, I like to do the transactions on the SQL side, but that isn't always practical so in that case, I do it on the C# side.
@mohamedzakaria9124
@mohamedzakaria9124 Жыл бұрын
Thanks Tim, How can I return a list of users and use multi mapping with this way for example return data depend on tow tables
@alfonsdeda8912
@alfonsdeda8912 Жыл бұрын
Hi Tim. If I want to connect to different data providers, for example SQL connection and oledbconnection, i create two repository sqlserverrepository and Oracle repository that inherit from generic repository interface. Should I change in every repository the IDBconnection for each different method or create a factory method that choose the idbconnection and create only once repository that get the IDBconnection from this factory?
@philipvandenheever2084
@philipvandenheever2084 2 жыл бұрын
Hey Tim. Great video and very timely. Could you touch on how we handle dynamic parameters?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
In what way? We are going to be using an anonymous object for most of these calls, which on the receiver side is of type dynamic Are you looking for something beyond that?
@philipvandenheever2084
@philipvandenheever2084 2 жыл бұрын
@@IAmTimCorey If you wanted a ReturnValue parameter for instance?
@bipinchandrakant3085
@bipinchandrakant3085 2 жыл бұрын
Wow! This is simple and clean. can you please recommend an optimal way to implement retry logic along with Dapper? Let's say one of the DB operations fails due to a deadlock/intermediate connectivity issue, is there a direct way to retry like Polly for HTTP? As of now, I use a recursive function to retry one more time and before failing, but I am sure there should be a better way to handle this.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I would probably look at Polly for retry logic.
@torrvic1156
@torrvic1156 6 ай бұрын
Thank you so much for enlightening us Mr. Corey! Can you tell me if SqlDataAccess class methods can be used with MySql instead of Microsoft SQL without significant modification? I am not sure if StoredProcedures will function the same way. And as far as I understand Identity is an Auto_Increment in MySql.
@andysmith872
@andysmith872 2 жыл бұрын
Firstly - first video I've seen of yours Tim - awesome - I'm now a subscriber / fan! Q - LoadData is only implemented with IEnumerable. Why not one also for returning one row? Is this out of simplicity for the demo/video or do you *always* do it this way? Thanks.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I always do it this way because an IEnumerable can return 0, 1, or many records. I don't usually have a need to create a separate method with a different name (since you cannot overload a method by only changing the return type) just to limit the results to one record.
@bulelf
@bulelf 2 жыл бұрын
Great video! What are your thoughts on the scalability of this approach? Is a small query string here and there considered a bad practice? My only concern with stored procedures is that when looking at the code, you can't actually tell how you got that result, you'll have to go and find the stored procedure.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Well, since we built the stored procedure in the database project, it is all right there in our solution. As far as writing T-SQL in C#, I prefer not to do that because we are mixing our two systems. We are relying on C# to provide the SQL statement, which means SQL cannot optimize the statement until we pass it over. That's not ideal. The issue isn't as much scalability as it is one of being easy to maintain. If you are evaluating long-running or expensive queries, knowing where they are located is a real time-saver. If you think finding the stored procedure is inconvenient, try having a SQL statement that was dynamically generated that you have to figure out where it came from in your source code.
@victorbouffier3580
@victorbouffier3580 2 жыл бұрын
Hi Tim. Kudos on your page. You have great insight and your explanations are fantastic I am using a Mac M1 with Visual Studio 17 for Mac. I cannot seem to find the SQL project. My understanding is that SQL Server is not available for Mac but the underlying DB is SQLite. Should I just ignore the project setup? How do you proceed?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
The data project won't work, but you could still create a database and then just connect to it. If you know how to create a SQLite database then use that. You won't be able to create stored procedures, but you can just call queries to do the same thing.
@jmiedreich
@jmiedreich 2 жыл бұрын
Hey Tim, great video as always. I enjoy using Dapper myself and more recently repodb . I'm looking for some best practices on rolling out sql databases changes to production. EF does the migrations, what's the best approach to move examples in your video to production?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I use the database project I showed in this video. It allows for migrations too.
@jmiedreich
@jmiedreich 2 жыл бұрын
@@IAmTimCorey Thank you, I will look at that again.
@leriosindane720
@leriosindane720 Жыл бұрын
Excelent. Tim. You change my way of thinking about database and layer implementation. Just one question. How can i return one or more output parameters?
@leriosindane720
@leriosindane720 Жыл бұрын
public async Task ProcessarPedidoCreditoTeste(PedidoModel pedido) { int Msg = 0;//OUTPUT PARAMETER await _db.SaveData("sp_IbCPanel_PedidoCredito", new { pedido.iduser, pedido.idempresa, pedido.idavalista, pedido.idgestor, pedido.idpedido, Msg }); return Msg; }
@codefoxtrot
@codefoxtrot Жыл бұрын
Thanks!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@tarekhajj8809
@tarekhajj8809 Жыл бұрын
Extremely interesting video Tim !!! thanks a lot, I was just wondering about the exceptions handling in the data access layer, you didn't add any try catch block, can you please elaborate on this matter, how are we supposed to handle database operations exceptions ? (on which level ? data access layer, business layer ?)
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I typically handle them in the UI layer unless there is a specific reason to handle them lower down. That way, we can alert the user to the issue and let them make a decision (retry, etc.)
@user-ub4uw9mr2p
@user-ub4uw9mr2p 8 ай бұрын
Great Video!!! Hey Tim I was wondering in the case where my query didnt return any rows; how could I construct an empty row just to display text "No Records Found". For example, rows.Add(new Model{LastUser = "No Records", AccessDate = "", blahblah= "", blahblah= "", Id = 1, blahblah= "" }); I am getting this compiler error that says it cannot convert from the 'Model' to 'T'. Thanks Sir !!!
@IAmTimCorey
@IAmTimCorey 8 ай бұрын
You would need to know the structure of the object (T). I would not recommend doing things this way. Return no rows. That should be the indicator your caller needs to know that there are no records. Otherwise, you will always have to deal with required fields, data types, knowing the specific object, and more. If you really need to do it that way, do it on the caller side rather than on the data access side.
@user-ub4uw9mr2p
@user-ub4uw9mr2p 8 ай бұрын
@@IAmTimCorey Thanks Mr. Corey. Is there a generic way of doing this because in the method is where the exception is going to occur. Are you suggesting that i check to see if there will be records returned before I call the method that returns rows?
@ianwanjala8621
@ianwanjala8621 6 ай бұрын
hey tim, very very nice course. So my question is, is there a way to log what dapper is doing while executing those procs? i.e what if in the update proc I had a method that checked whether a user with Id 2 exists and I found out he/she doesn't, in such a case I might be returning 31 from the proc. Is there a way to check for such a value being returned from the .net application?
@msizinxumalo6585
@msizinxumalo6585 Жыл бұрын
Thank you
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You're welcome.
@rogerswetnam1416
@rogerswetnam1416 2 жыл бұрын
Hi Tim - thanks for the excellent video. I have been used to using EF to scaffold my Identity Management - Register Login, etc. I feel a lot more secure than trying to do this on my own. However, I really like the way you have used Dapper for the Db Sql project. Can you suggest how I might combine MS Identity Management with classes like UserManager with the Dapper approach that you are demonstrating in your video. Learned a lot and registered in my first course from you.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I don't have any content on that because I don't mess with Identity. I use Entity Framework for that because that is what is built-in. Authentication is something you NEED to get right. I try not to make major changes to it. However, if you want an easier system, try out Azure Active Directory B2C. It is easier, integrates with C# well, and is free for the first 50,000 active users per month. I've got a course on it and I'll be doing some KZbin content on it soon.
@rogerswetnam1416
@rogerswetnam1416 2 жыл бұрын
@@IAmTimCorey Hi Tim - thanks - that's my point - I don't want to mess with Identity either. I am guessing that to use MS identity with a solution that also includes Dapper as you have demonstrated in this demo, I would create another separate project called something like IdentityDataAccess which uses EF - but not migrations and then import a scaffolded identity database and access the identity related tables (aspUser etc.) using the MS classes such as UserManager via the IdentityDataAccess project? Any suggestions here?
@Reaper7mk
@Reaper7mk 2 жыл бұрын
Is this the repository pattern or some version of it? I would love a video on that at some point! Thank you Tim
@torrvic1156
@torrvic1156 6 ай бұрын
Looks like that. SqlDataAccess is like a repository and UserData is like a service of some sort tied with SqlDataAccess.
@PatricksDad
@PatricksDad Жыл бұрын
I am re-watching your video (great content by the way). In your update stored procedure, would you not want to wrap the set values around a "COALESCE" statement to avoid a null value overwriting the value in the database, if a value is not passed?
@IAmTimCorey
@IAmTimCorey Жыл бұрын
It depends on the circumstances, but typically no. Otherwise, you would not be able to remove a fields data.
@diligencehumility6971
@diligencehumility6971 2 жыл бұрын
Hey Tim. Maybe you could do a video on how to do a scalable app? Now you have covered most aspect of real-world-applications, and lots of us do have real world app's out there, customers use. Now scalability becomes a problem, since one server instance is not enough. Should we do Azure Functions and let Azure scale our app? Should we do CosmosDB instead of MS SQL, because it's easily scalable? What about the serverless options? Serverless Functions? Serverless CosmosDB? There is even a serverless SignalR service. Like to hear your thoughts on this
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@louiseeggleton7420
@louiseeggleton7420 Жыл бұрын
Is it possible to do this with business models that require a constructor? I like my models to require that their creation be done through a constructor so that they are instantiated properly and always in a valid state. Or does this technique require a paramterless constructor?
@iourouz
@iourouz 2 жыл бұрын
Thanks. I was wondering, for the model, could you have used public record class UserModel(int Id, string FirstName, string LastName);
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
It depends. If we are not anticipating changing the data at all in our application then yes. Otherwise, no. In our case, I chose not to use a record because I want to leave it open for change and I did not want to lock myself into something that would have to change later. Besides, we won't really gain anything from records here.
@FiveNineO
@FiveNineO 2 жыл бұрын
Great video. Regarding inserts, I’ve seen various practices, some return nothing, some return the newly inserted entity (with Id) and others return only the Id. What do you think is the best way? Typical use case: add user on webpage, want new user to appear in users table on the page.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
The best way is the way that works best for your specific situation. If you don’t need the data, don’t return it. If you do, look it up and return it. The lookup is an additional action in SQL (getting the identity and doing a select for that record).
@dawidbiell
@dawidbiell 2 жыл бұрын
Good video!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@girlygamer3102
@girlygamer3102 2 жыл бұрын
Hello Tim, thanks again for a wonderful tutorial. I generally avoid to use stored procedures as I do not want to encapsulate business logic in the database. Also it is also recommended not to encapsulate business logic at database level. I try to use SPs for creating reports etc. My simple question is - what is your recommendation I mean when to use stored procedures?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I disagree with the "no stored procedures" or "no business logic in the database" recommendations. We need security in depth. If the database layer is fully permissive then you are relying on applications to do all of your security. That's fine as long as those applications all have the same business logic. Also, don't forget that there is a security issue here - your connection to the database. If a user has access to it, they can bypass the business logic and insert data directly that might be harmful/malformed. Plus, if you use stored procedures, you can lock down your database much more than if you use ad hoc queries.
@easy-e4472
@easy-e4472 2 жыл бұрын
​@@IAmTimCorey Tightly coupling the API to a specific DB is generally frowned upon to my understanding. In this example, wouldn't your business logic become dependent on a SQL Server backend? BL in SPs are also more difficult to unit test, can be more difficult to maintain when complexity is involved, etc.... Always exceptions of course and tradeoffs to be made.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
No because you don’t rely on the logic in the stored procedures. Also, the SP logic is limited.
@girlygamer3102
@girlygamer3102 2 жыл бұрын
@@IAmTimCorey Ok thank you for your advise. I understand the security point of view. I asked this question because we recently feel pain when we had to change our backend database from SQL Server to Oracle. The product that I am working on has a SQL server database. But recently a new client wanted Oracle as the backend instead of SQL server. Then we had to do a lot of work in converting SQL SPs into Oracle SPs. That was a real headache. But almost all the EF queries ran successfully without doing any changes with Oracle as the backend. This was my main concern.
@rolfherbert2051
@rolfherbert2051 2 жыл бұрын
Stored procedures are not faster or better than ad hoc, and some logic is always better in your app (unless you are an expert on creating set based queries then loops and the like should not be in your SPs) but store procedures most definitely keep your developers and app away from the data. If your app only uses stored procedures no one is ever going to be able to accidentally write a DELETE FROM USERS WHERE NAME=NAME type query, its just not possible if that user does not have access to the data but only to the SPs.
What Are Your Thoughts on Entity Framework Core vs. Dapper?
21:49
😱СНЯЛ СУПЕР КОТА НА КАМЕРУ⁉
00:37
OMG DEN
Рет қаралды 1,8 МЛН
одни дома // EVA mash @TweetvilleCartoon
01:00
EVA mash
Рет қаралды 6 МЛН
КИРПИЧ ОБ ГОЛОВУ #shorts
00:24
Паша Осадчий
Рет қаралды 6 МЛН
Which Programming Techniques Should Every Developer Know?
16:39
IAmTimCorey
Рет қаралды 32 М.
Raw SQL, SQL Query Builder, or ORM?
16:19
ArjanCodes
Рет қаралды 91 М.
Learn Dapper in an Hour! (.NET Core 5)
1:00:43
DotNetMastery
Рет қаралды 54 М.
How Do I Structure My Application?
12:25
IAmTimCorey
Рет қаралды 17 М.
Entity Framework Core vs Dapper Performance in 2023
13:59
Nick Chapsas
Рет қаралды 93 М.
Dapper v/s Entity Framework(Core) -  .NET Interview Question
5:45
DotNetMastery
Рет қаралды 23 М.
Where People Go When They Want to Hack You
34:40
CyberNews
Рет қаралды 50 М.
The Path to C# in 2024
1:03:41
IAmTimCorey
Рет қаралды 63 М.
😱СНЯЛ СУПЕР КОТА НА КАМЕРУ⁉
00:37
OMG DEN
Рет қаралды 1,8 МЛН