Тёмный

Repository Pattern with C# and Entity Framework, Done Right | Mosh 

Programming with Mosh
Подписаться 4 млн
Просмотров 867 тыс.
50% 1

🔥Get the COMPLETE Entity Framework course (80% OFF - LIMITED TIME): bit.ly/2rZAgrD
Want to learn more from me? Check out my these links:
Courses: codewithmosh.com
Blog: www.programmingwithmosh.com/
Facebook: / programmingwithmosh
Twitter: / moshhamedani
Confused about the repository pattern? This tutorial explains it all.
There are many tutorials about the repository pattern and many of them are conflicting. If you've been confused about the proper way to implement the repository and unit of work patterns with Entity Framework, this video is for you.
00:00 What is the Repository Pattern?
00:47 Benefits of the Repository Pattern
03:50 Repository in a Nutshell
04:47 What is the Unit of Work Pattern?
05:00 Does Entity Framework Really Implement Repository and Unit of Work Patterns?
09:31 Clean Architecture
11:43 Implementing the Repository Pattern
14:09 Implementing the Unit of Work Pattern
15:09 Repository and Unit of Work Patterns in Code
22:49 Example of Repository and Unit of Work Patterns
Download the source code for this video here:
programmingwithmosh.com/wp-con...
And here are some RU-vid videos of mine you might be interested in:
Event and Delegates in C#
• C# Events and Delegate...
Generics
• C# Generics Tutorial: ...

Наука

Опубликовано:

 

8 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 869   
@mikedqin
@mikedqin 4 года назад
It's amazing that what Mosh taught here in 2015 still holds true in the first day of 2020. Cheers!
@kademmohammed6836
@kademmohammed6836 4 года назад
Mosh is a legend
@wordlifejohn1122
@wordlifejohn1122 3 года назад
True that
@dotnetdevni
@dotnetdevni 3 года назад
How is that the case ur queues will have link and lambada it would be harder
@TheDude0924
@TheDude0924 3 года назад
2021 now and still going strong
@andeslam7370
@andeslam7370 3 года назад
is this because of the undying nature of pattern?
@marc2377
@marc2377 3 года назад
Mosh, I don't know if you still read this but I wanted to say THANK YOU for taking the time to go over the arguments that EF already implements the repo pattern, etc etc, and demonstrate that this is not exactly the case. This entire debate had me so confused for quite a while, and you did a _great_ job putting it to rest (for me, at least). The demonstration is also nicely done. Thanks, really.
@kingmaduma1783
@kingmaduma1783 6 лет назад
The way you explain things is so uncomplicated you almost make it seem like this stuff is easy. Thanks a load mate. Your tutorials are excellent.
@BryanJohns
@BryanJohns 8 лет назад
Excellent. Probably the clearest, most concise, and easiest to grok tutorial on the Repository Pattern I've ever watched.
@ayaosama3839
@ayaosama3839 4 года назад
You're such an amazing instructor who can simplify the information in perfect way and make the listener understand well .. thanks Mosh and I hope you to share more videos and courses
@jobycheriyan9740
@jobycheriyan9740 8 лет назад
"simplicity is the ultimate sophistication" and your training also simple & Clear about a complex pattern.
@kevina4600
@kevina4600 2 года назад
I was about to not use repo pattern on my project because of other videos saying it was redundant, but your explanation made so much sense and was really easy to understand, thanks for taking the time to make this video.
@hardryv3719
@hardryv3719 3 года назад
I've listened to many Mosh tutorials over the years, and this one is still the most impressive to me... he says the same things as many other videos, but the cadence along with the presentation queues simply flow better here. It's why I've come back to listen to it repeatedly... it's also superior in quality vs an average college lecture.
@prajwolshrestha3462
@prajwolshrestha3462 2 года назад
Coulnd't agree more
@sohailarshed3427
@sohailarshed3427 8 лет назад
Saw dozens of tuts but literally this was awesome simple and focused, I feel confidence now. Thanks a lot!
@roylee3196
@roylee3196 8 лет назад
Just wow, you completely blew me away with your well-cut explanations. Excellent job Mosh.
@matigarimanjirungi59
@matigarimanjirungi59 6 лет назад
These is the best tutorial i have found that explains a concept i have been trying to grasp for the last 2 weeks. Very precise and explains everything in details.
@SonnyRecio
@SonnyRecio 8 лет назад
I agree with you Mosh. Repository Pattern has become a mystery to me ever since I tried to explore it and for the most part articles from different developers have different implementations of Repository Pattern in which it is hard to determine which is actually the right one.
@FabioCordeiro_RJ
@FabioCordeiro_RJ 4 года назад
The number one ! I have about 4 Mosh courses on Udemy. They all help me today. With that I gained better job opportunities. Thank you!
@pierrenilsson4179
@pierrenilsson4179 7 лет назад
Best explanation I've yet to see of the repository pattern.
@geoffjames992
@geoffjames992 8 лет назад
Mosh! Thank you so much for this video. I stumbled upon it when designing the architecture of a website I'm working on. I'd read so many conflicting views on why/not to use repositories with Entity Framework - most of them against using repositories for the same reason you said you've come across a lot, too. You've explained very clearly to me how it is beneficial, and I also picked up some tips on how to create some Generic repositories in future. Well worth a watch, and I'll be coming back in future to view more of your videos! Thanks!
@wilko6524
@wilko6524 6 лет назад
Best video I've seen on this topic, and its easily understandable from what you mention and you give course materials! Looking forward to working through your Pluralsight courses.
@siddheshswa
@siddheshswa 6 лет назад
Excellent presentation Mosh. You really made it simple.
@olawoleomotosho4836
@olawoleomotosho4836 7 лет назад
Almost 2 years and you still made a lot of sense. thanks so much. You are the best
@salimmapande4668
@salimmapande4668 8 лет назад
Thank you Mosh, Thank you for a very comprehensive demonstrations of repository pattern, I enjoyed it for sure
@ApacheGamingUK
@ApacheGamingUK Год назад
You say that your CourseRepository knows nothing about EntityFramework, however, it is strongly coupled to EntityFramework, because it's base class, Repository injects a DbContext, which is EntityFramework specific. If you remove the `System.Data.Entity` reference, and swap it with another ORM, the whole things breaks. So it's wholly dependent on EF, from the generic base class, upwards. You should really name your generic repo as `EntityFrameworkRepository`, to show that it is dependent on EF. But this will still mean the app breaks when you swap ORMs. It would just make it more clear, as to why.
@kuroki_suzume
@kuroki_suzume 4 года назад
I was searching 2 days for guide like that and everything was a trash, lessons without understanding the base... and only now youtube proposed me this video and it's perfect!!! My huge respect to author !!! Like and subscription :)
@frankhaugen
@frankhaugen 4 года назад
When I do greenfield development I always do Controller -> Service(es) -> Repositories -> EF/DbContext (and/or MongoDb). This has been a very good pattern for me/us, as it's clean and very effective. Having a repository present two different database technologies as a single source, gives a lot of freedom to concentrate on the business logic and not the database implementation
@xhiris5844
@xhiris5844 6 лет назад
Abolutely brilliant! This finally made everything 'click' on using the Repository pattern with EF6. I'd avoided it because it seemed like far too much work for too little benefit vs. using DbContext directly,
@ParamjeetSingh-vk4md
@ParamjeetSingh-vk4md 8 лет назад
Excellent video to demonstrate repository and unit of work.. Thanks a lot!!
@tvbox4387
@tvbox4387 8 лет назад
Very well presentation of Repository! Greetings from Bulgaria!
@fatemalokhandwala3477
@fatemalokhandwala3477 3 года назад
Amazing Explanation. Gained a lot of precise information in a very short span. That's what I love most about your videos.
@JodieLogan
@JodieLogan 8 лет назад
Great explanation of the Repository Pattern w/UOW Mosh! Your awesome presentation along with in-depth analysis of The Repository Pattern lays to rest the argument against using both TRP and Entity Framework that I have seen over the years from other camps of thought. Thnx!
@mykhailo-kmet
@mykhailo-kmet 3 года назад
Your "IRepository" has "Find" method with "Exspression predicate" parameter. How are you going to implement it with stored procedures?
@Thenbafan45
@Thenbafan45 8 лет назад
I have done repository before and I am new to unit of work. My repositories have save and delete methods. The way you explain unit of work here has helped me a great deal I will be implementing this pattern much more often.
@DodaGarcia
@DodaGarcia 5 месяцев назад
Right? That was so helpful
@gauravpathare208
@gauravpathare208 7 лет назад
This channel is gold mine for C# Beginners and Aspirants.
@Pest87
@Pest87 Год назад
The best video I've seen for repository pattern - clean and simple.
@BryanPollardPhotos
@BryanPollardPhotos 5 лет назад
Brilliant! Thank you for the clear and straightforward explanation of these principles.
@mryildiz702
@mryildiz702 6 лет назад
It's the best video/article/lesson I have found on the internet. Thank you :)
@VivekChandran26
@VivekChandran26 4 года назад
Brilliant and easy to learn presentation, Mosh. Hats off to you. Thanks a ton and please keep it up 🙏👍
@arechj
@arechj 2 года назад
Wonderful teaching - so useful and elegant! Thanks as always Mosh!
@david3552
@david3552 10 месяцев назад
This is, truly, the best resource on the subject that I've seen. Thank you
@smitch_yt
@smitch_yt 8 лет назад
Loving your tutorials, very clear and easy to follow Thanks!!
@milanimilivoj
@milanimilivoj 3 года назад
Find method in IRepository is breaking the rules of having the query written only once. A developer can inject any expression in it duplicating queries in different repository clients. I'd remove it from the interface and allow only named query methods. Specification pattern might be an alternative option but that in the end kind of boils down to the same thing.
5 лет назад
What if id is type of Guid, then your generic repository will not work. Here is iEntity comes to play. Your domains should inherit from iEntity, so you can query based on id
@VinayKumar-qu1eg
@VinayKumar-qu1eg 7 лет назад
Hi Mosh, Thanks for the excellent presentation. I have gone through many of the comments. Here are my consolidated queries. 1) Find method which takes a predicate as the parameter. Don't you consider this as a potential risk for leaking abstraction into layers depending on the repository a) not every Expression can be interpreted by EF b) there's no guarantee that a predicate which works with EF is going to work with a different data access library (let's take OData as an example) So the only solution here is to write Find method in each repository as per the requirement? 2) Since many of the RealWorld applications contain more than 100 Repository classes. It's now important to learn about bounded contexts in Domain Driven development so that we can architect a project by splitting it into different domain projects and use related bounded contexts. I have gone through several articles on this topic. But could you please prepare and post a video on it by covering all the concepts like (MVC, EF, DDD, BC, DI....) Because I think you are the best person who can explain the things in a clear manner. :) Thanks, Vinay
@briankarcher4418
@briankarcher4418 5 лет назад
Nice video. I tend to find for smaller projects Entity Framework is just fine for a repository and unit of work pattern as there is less if no duplication of queries. I have used your pattern before and found it to be too much for small projects. My only advice is: Do what is best for your particular project. There is no one size fits all solution. The solution posted in this video is great for a larger application.
@allwynd
@allwynd 8 лет назад
Very helpful. Great clarity in content and beautiful slides.
@RomanRajab
@RomanRajab 7 лет назад
This was very helpful, very clear explanation. Thank you very much.
@gmddmg
@gmddmg 8 лет назад
This is just great. Excelent material! Very clear, well splained and justifyed! Thanks a lot for sharing. I just would include some methods for getting objects withut tracking them, for performance, when required. But probably that might not be part of the "core" Repository pattern implementation anyway. I will check your other courses. Cheers!
@joshmonreal
@joshmonreal 7 лет назад
Very informative tutorial. Thank you so much for this.
@amanthakur1987
@amanthakur1987 6 лет назад
Excellent, I was struggling to understand repository realtime use...Now I know it.....Thanks Buddy.....
@amanthakur1987
@amanthakur1987 6 лет назад
Hi Mosh, do you have any video on Factory vs Abstract Factory scenarios??
@mrclaytron
@mrclaytron 6 лет назад
Really great video Mosh! I just brought your Udemy course for Entity Framework - love your teaching style man, very clear and well explained - this is the best explanation I have seen for the Repository pattern, hence my decision to buy the course. Cheers!
@sohailnaseer7251
@sohailnaseer7251 3 года назад
Thank you so much for very comprehensive demonstration of Repository pattern. Well explained
@ahmedschhaider4762
@ahmedschhaider4762 5 лет назад
Very much explained, thank you !!
@kamdemkakengne
@kamdemkakengne 7 лет назад
Nice and clean ! I like the way you explain things ! Thanks a lot Mosh !
@80amnesia
@80amnesia 8 лет назад
Simple and well explained example. Thanks, keep it up.
@khrazy5150
@khrazy5150 8 лет назад
This is by far the best explanation on repositories that I've seen online so far. When is your course on Pluralsight coming out?
@Siyamsalam
@Siyamsalam 8 лет назад
man..... you are just amazing. often your way of thinking reminds me how exactly I think...... keep up the good work.
@sphiwemaluleke5658
@sphiwemaluleke5658 7 лет назад
Hey Mosh, thanks for opening my eyes man,excellent video man
@CarlosAlbertoCostaBeppler
@CarlosAlbertoCostaBeppler 8 лет назад
Good video. Some things: 1) I think that the GetAll method is not so generic to be part of the interface and can be a little dangerous. Here where I work, many times people use the GetAll and then filter the results in memory (even when the original data has millions of rows). 2) I think that the Find method must hava a call to ToList (or AsEnumerable) before return the results, so an user can´t cast it back to IQueryable.
@HugRunner
@HugRunner 4 года назад
I think you should have touched a bit on ".AsNoTracking()" and perhaps also .Attach(), EntityState.Modified and EntityState.Detached, since e.g. retrieving all courses without intent to change anything, is probably the most common use case. Then you can just attach the specific course when you actually want to change it, i.e. have Entity Framework track it for you. If all retrieved objects are tracked all the time, it's a huge impact on performance.
@kevinjahr6140
@kevinjahr6140 8 лет назад
Thank you for a very clear tutorial and great video!
@malikau917
@malikau917 5 лет назад
Thanks! Clean and complex explanation, man
@otmanighoulassen6177
@otmanighoulassen6177 8 лет назад
Excellent video ! very clair and concise explanations, thanks a lot !!
@mshevroja
@mshevroja 2 года назад
My favorite video about repository pattern + unit of work video
@mikebarger
@mikebarger 6 лет назад
IMO, the “collection-like interface” is the least important and most out-dated portion of the repository pattern. The most important part of the pattern is to isolate the rest of your application from the persistence logic. You typically don’t want to reuse repository methods. Since your business logic shouldn’t be in the repository, you don’t want multiple code paths reusing a repo method like SaveArticle(Article article). Instead reuse the same service method that calls the repo so the same business logic is enforced. Too many coders try to get cute/clever with generic repositories, exposing IQueryable, etc. EF performs poorly enough all by itself. It doesn’t need help being slower by the n+1 problems created by not writing custom linq queries that specify the include. Of course it always depends on the specific constraints of the application. Thank you for your great videos.
@psyaviah
@psyaviah Год назад
Indeed! +1
@juanavelinosanlucas6129
@juanavelinosanlucas6129 8 лет назад
excelente video, man gracias (y)
@harishsappa9509
@harishsappa9509 8 лет назад
Great Video, thanks a lot.
@santoshkmrsingh
@santoshkmrsingh 4 года назад
Extremely well explained the theory is key to implementing any concept and you do well to explain the same.
@rishavkumar1
@rishavkumar1 7 лет назад
U are awesome... man.. You just cleared all my doubts regarding these...
@abdoessamsarhan6911
@abdoessamsarhan6911 7 лет назад
Excellent video very very clair and concise explanations, thanks a lot
@mohsinmemon3362
@mohsinmemon3362 3 года назад
Excellent explanation. Clearly understand about the Repository & Generic Repository pattern. Thanks alot
@TheLegoody
@TheLegoody 6 лет назад
you're a superstar mosh. this really helped me a lot
@Gojam12
@Gojam12 8 дней назад
Best explanation I have heard to date on the repository pattern. I am much closer to understanding it now
@enghazymeh9921
@enghazymeh9921 6 лет назад
A lot of thanks for your effort, it was a clear explanation .
@sanketss84
@sanketss84 6 лет назад
most awesome explanation of uow and repository pattern.
@ttatum
@ttatum 8 лет назад
Thank you Mosh. Outstanding presentation!!
@MyMohitgoyal
@MyMohitgoyal 7 лет назад
thanks to posting this amezing video. from long time i was finding a basics for it. but i was not able to understand properly. and ur this video gives me an idea about repository. Thanks. :-)
@nashanharis3398
@nashanharis3398 2 года назад
Excellent Tutorial. Thanks Mosh.
@dsmyify
@dsmyify 6 лет назад
Thanks Mosh! You're the real MVP!
@sanjeevsah2606
@sanjeevsah2606 8 лет назад
Great Job your explanation help a lot. Thanks for uploading this video.
@RomuloMagalhaesAutoTOPO
@RomuloMagalhaesAutoTOPO 2 года назад
Thank you very much. I will experiment this to understand more about.
@volodyasenchak1907
@volodyasenchak1907 6 лет назад
Really good explanation of Repository pattern.
@danteel5489
@danteel5489 8 лет назад
My first time seeing one of your videos, and I'm hooked. I learned a great deal from this presentation. Thanks for the awesome and thought out explanations!
@koemdjiev
@koemdjiev 6 лет назад
Awesome lecture! Thanks!
@VenkataReddyUK
@VenkataReddyUK 8 лет назад
Great presentation Mosh! Thanks
@dhivabanyuwigara8517
@dhivabanyuwigara8517 4 года назад
Thank you, this video saves me a lot of times :D
@Shammikk
@Shammikk 8 лет назад
Great job, great explanations. Thank you :)
@conspiritor2
@conspiritor2 8 лет назад
Phenomenal presentation, keep publishing this stuff.... :)
@bangazbyb4908
@bangazbyb4908 6 лет назад
This was very informative. Thank you.
@rk_t
@rk_t 7 лет назад
You're awesome Mosh ... Thanks
@somdev2552
@somdev2552 8 лет назад
Perfect explanation .... Learnt a lot :) Thanks
@Lolitalein
@Lolitalein 8 лет назад
Thank you so much! I'm still a newbie when it comes to developing applications on my own and due my lack of experience I've searched for a long time and read one blog post after another. In the end I was so confused about the fact, when to use what and where to write down this sh*t :) Your Video sorted out a lot questions. Keep going with your great work. Greetings from Germany :) (Please ignore my bad english)
@ReuelRamos
@ReuelRamos 4 года назад
I should advice from experience on two distinct projects which followed this idea of encapsulating EF within our own Repository + UOW: Don't do it! EF already does it and you could use Specification patterns to easily overcome the problem of repeating querying logic. Also, your entire application DOES NOT need to use only one ORM. It's fine to use EF for simple/non-performant areas/contexts and use something else like PetaPoco for areas where you need performance, etc. Another idea if you still want to have a class named Repository: Just make it a simple class where you inject the DbContext. It would sort of work as a decorator over the DbContext. On the two projects I've worked we followed this (IMO) contrived inheritance of BaseRepositoryClass + BaseRepositoryInterface + ActualRepositoryInterface + ActualBaseRepositoryClass inheriting from all of that: I have deeply regretted doing so. It is like CQRS when folks hear about: Let's implement it on all our system. No! Only do it for the relevant areas/domains/bounded contexts of your code.
@seldah
@seldah 4 года назад
the application in the video is just for a demonstration!
@murlikumarganapur2031
@murlikumarganapur2031 4 года назад
Amazing and good explanation on Repository pattern!!!
@jorgek67
@jorgek67 6 лет назад
Awesome dude, you explanation was excellent!
@runenielsen8270
@runenielsen8270 8 лет назад
Amazing thank you for uploading!
@AhmedHabbachi
@AhmedHabbachi 8 лет назад
where've you been man i was searching for you thought :p as you said in the begging i've read a lot about unit of work and repository but i didn't get it clean and with this video you've explain it well thank you very much. it helps a lot, waiting for more from you.
@warriorfb2010
@warriorfb2010 3 года назад
Fantastic video. Really sums up a lot of questions i had
@JohnDoe-xi6df
@JohnDoe-xi6df 2 года назад
Great explained! Thanks
@EvangelosLolos
@EvangelosLolos 8 лет назад
Great video, but unfortunately the arguments were not convincing enough for me. Some reasons: - How often do you see enterprise projects switching ORMs? Also, if using Dependency Injection, you only need to refactor the layer that depends on EF, same as in your case were you would need to refactor the repository implementations. - Each ORM is different and these differences mean this repository pattern can be a leaky abstraction. For example, calling ToList() on a collection is totally unpredictable in terms of lazy/eager loading depending on a) the ORM and b) the repository implementation. - You can still extend the DbSets to have methods for common queries, same as with your repositories.
@anastasiyamartynyak6440
@anastasiyamartynyak6440 8 лет назад
just million likes!!! thank you so much!!! many many thanks!!!
@irvlennert3674
@irvlennert3674 8 лет назад
I get it! And this is the first time it has really been made clear to me! I have been playing with the repository pattern for a few years now and have used it in several projects, with a degree of success but it always get messy when the project had to do tricky things or when the project got bigger. I would not end up maintaining the DRYness that I like. I could see the benefit but was still only really successful with it in small projects that I saved through calls in the interface as well. The way you use the UnitOfWork pattern is really cool! I'm also taking your Angular2 course on Udemy and you are great there as well! Thanks so much for your clarity! Irv
@RudraPratapSinghLearning
@RudraPratapSinghLearning 2 года назад
Its a really amazing and simple explanation. Thanks Mosh. I regret taking a course at Naresh IT, Hydrabad. They wasted my time and money. Thanks again Mosh.
@rajanir9761
@rajanir9761 8 лет назад
Great explanation...very clear cut....help me understand this pattern and how to implment it nicely...
Далее
Design Patterns in Plain English | Mosh Hamedani
1:20:01
Deep Dive Into the Repository Design Pattern in Python
11:56
10 Design Patterns Explained in 10 Minutes
11:04
Просмотров 2,2 млн
Repository Pattern
11:08
Просмотров 66 тыс.
Clean Code with Entity Framework Core | Brendan Richards
1:55:32
Learn TypeScript Generics In 13 Minutes
12:52
Просмотров 233 тыс.