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

Programming with Mosh · 893K views · 2015 · 26:24

Open on YouTube

Software architecture & designDatabases & storageWeb developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when asking or answering how to structure repositories and a unit of work on top of Entity Framework, as a worked reference implementation.

Recommended by 7 people across 8 different months, 2015 to 2023.

Why Software Engineering Stack Exchange recommends it

Software Engineering Stack Exchange users cite it as a concrete C# and Entity Framework reference when asking about repositories, units of work, and what a repository should return.

Recommended by 1 people across 1 different months, 2016 to 2020.

What it is

A 26-minute tutorial by Mosh Hamedani of Programming with Mosh on implementing the repository and unit of work patterns with C# and Entity Framework. It explains what the patterns are, argues about whether EF already implements them, touches on clean architecture, and walks through a coded example with downloadable source.

What people said

Recommends Lucian Bumb · 2015-11 · Stack Overflow · on “how to Pass parameter with dynamic model class in Ntier MVC c#”

Your code is not very clear for me, if you want to make a generic repository you should follow this tutorial: [this video]

Recommends The Mean Fiddler · 2016-03 · Stack Overflow · on “Entity Framework - infrastructure in WPF/MVVM application”

I'm in the same position. I find that any persistent repository on the client side causes users not to see each others' changes. Here's a good video explaining why EF is not the same as a repository [this video] Also I found an excellent end-to-end tutorial on WPF,MVVM & EF [LINK] . In this he exposes the data through a WCF data service and detaches them from the dbcontext straight away. Hope it helps

Recommends enet · 2016-05 · Stack Overflow · on “Dependency Injection and Scope?”

If your application requires singleton behavior, use it. But in that case, you'll have to manage your object’s lifetime in the class yourself. "However, Entity Framework contexts should be added to the services container using the Scoped lifetime. This is taken care of automatically if you use the helper methods as shown" below: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<MyDbContext>(options => options.UseInMemoryDatabase() ) } Repositories that will make use of Entity Framework should use the same lifetime; that is, AddScoped... So your code should be like so: services.AddScoped<IMyCustomRepository, MyCustomRepository>(); "One of my main concerns is t

Recommends user585968 · 2017-03 · Stack Overflow · on “Should I have multiple repositories?”

Should I create an individual repository for each? For instance one repository for enquiries, another for user usage metrics? Is there any overhead or penalty for doing this? Yes. No. Generally you want to have one repository per entity type because each entity type is going to more than likely require operations specific to its type beyond the cliche CRUD operations . The aim of the repo is to eliminate duplicate data query logic that would otherwise be littered about your application. e.g. interface IRepo { CRUD } protected abstract class RepoBase<T> : IRepo { // CRUD implementation } public class PatientRepo : RepoBase<Patient> { List<IPatient> GetAllTerminallyIllPatients(); } public clas

Recommends Rakesh Guranani · 2017-05 · Stack Overflow · on “Why use Repository Pattern or please explain it to me?”

I know it is bad provide links in answer here, however wanted to share the video which explains various advantages of Repository Pattern when using it with Entity framework. Below is the link of youtube. [this video] It also provides details about how to implement Repository pattern properly.

Recommends IvanJazz · 2017-06 · Stack Overflow · on “Have I understood architectural design with ASP.NET MVC and Entity Framework?”

You're almost in the right direction. However, the ViewModel in this case should reside in the application layer, i.e. your MVC layer. The service layer should in turn return a data transfer object, more commonly known as the DTO . Think of the ViewModel as a simple POCO class that is built for the View , it can be a collection of various DTO returned by various services from your service layer. Benefits of DTO You are not directly exposing your domain entities, i.e your EntityFramework POCO classes. However, a case can be made for a project small enough to avoid DTO all together. In case in the future you decided to add an WebAPI function along your MVC project, say, for an iPhone applicati

Recommends Tomasz Strączek · 2019-07 · Stack Overflow · on “async UnitOfWork with one dbContext + Messenger - generates "A second operation started on this context...." Exception”

I'm new to async and I'm trying to create async WPF MVVM application with MVVM Light framework and have a problem with "A second operation started on this context before a previous asynchronous operation completed..." In my program, when I clik button new View is shown and if there is a message with data ViewModel receives it and fills the appropriate controls with data, if not it is ready to add new ones. Therefore in the CTOR there is method GetInitialValues() and messenger (MVVMLight). When openinig the View its ViewModel CTOR looks like in the code below So in CTOR: I provide a UOF using AutoFac. Injected UOF uses one dbContext for all Repositories per MyViewModel (acc. to the video: [VI

Recommends AgostinoX · 2020-07 · Software Engineering Stack Exchange · on “repository pattern, using the domain object as data access object”

I'm very puzzled by a specific part of the repository pattern that seems simple but turn out to be tricky. I took this great explanation of this topic by Mosh Hamedani; it's a C# implementation of repository pattern with Entity Framework. [this video] The key points is: repository should return domain objects. Not IQueryable and not other types of objects. Decoupling, fair enough. An example of a method of a repository that Mosh proposes in that video is this: public IEnumerable<Course> GetCoursesWithAuthors(int pageIndex, int pageSize) { return PlutoContext.Courses .Include(c => c.Author) .OrderBy(c => c.Name) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToList(); } So we can see that ther

Cites Mosh · 2015-10 · Stack Overflow · on “Is Unit Of Work and Repository Patterns very useful for big projects?”

Software design patterns are designed to solve specific problems with the right context, and if used inappropriately, they'll lead to extra unnecessary complexity without providing any values. So, what are the problems that the repository pattern aims to solve? 1- Minimizing duplicate query logic : in large applications, you may find many complex LINQ queries duplicated in a few places. If that's the case, you can use the repository pattern to encapsulate these queries and minimise duplication. 2- Better separation of concerns: Imagine a complex query to get the top selling courses in a given category that involves eager loading, joining, grouping, filtering, etc. When you implement such lar

Cites ajr · 2016-04 · Stack Overflow · on “How to get all entities in the model and serialise those into xml?”

Thank you for the comment. Here are some answers: I'm using repository pattern following this implementation [this video] and at the moment I am serialising every class(entity) and its data as an individual xml-file. Then while importing reconstruction all the navigation relations (might not be the best way). However, I'm quite new with this topic so examples to guide me into right direction are always highly appreciated!.