Repository Pattern With Entity Framework Core | Clean Architecture, .NET 6

preview_player
Показать описание

The Repository Pattern is a popular design pattern in the software engineering community. Some developers love it, and some developers hate it. Some will say that the Repository Pattern is an unnecessary abstraction if you are using an ORM. I like to be pragmatic and use this pattern where I see fit. From my experience, we can use the Repository Pattern with great success for the Command side of a CQRS implementation.

Join my weekly .NET newsletter:

Subscribe for more:

Chapters
0:00 What is the Repository Pattern?
1:10 How We Can Use the Repository Pattern
2:44 Benefits of Repository Pattern
3:28 Unnecessary Abstraction?
4:21 Testability
4:43 Implementing the Repository Pattern
7:28 Repository per Aggregate Root
Рекомендации по теме
Комментарии
Автор

I really like the repository pattern. I usually bundle all of them up as public fields in an service to inject into classes.

I also like implementing a basic implentation of IRepository as an abstract class that provides basic crud for entities and inheritors do the queries while the Use Case classes handle transactions. It has worked nice so far 👍

MrKyuubiJesus
Автор

you are making me a better programmer, really. Thank you s2

feitoza.system
Автор

Best Repository Pattern video I've seen in a while. Well done!

thecodeman_
Автор

Love this quality DDD content, is small nuggets, well explained.... Great Channel

unhandledexception
Автор

Hi Milan. Is it OK to return DTO object from the repository method? My repository method executes the Stored Procedure, which uses a dynamic pivot inside. If I return entities it will be too difficult to implement (dynamic pivot query) with LINQ in the Application layer (QueryHandler). Thank you.

owgyxjv
Автор

Hi Milan! When you are injecting the dbcontext in your queries is this not breaking the ddd constraints?

Repectful
Автор

I have a very large entity with 10+ columns. Is it okay to return a small subset of these columns from the Repository as a DTO or should we always return domain objects from the Repository. If I always return domain objects then SQL queries will unnecessarily fetch all columns. But with a DTO I can get just the required columns.

kahanbhalani
Автор

Actually, the only reason why I prefer to wrap DbContext with Repo abstraction is to limit access to IQueryable. OFC, if we speak about read queries, then I am ok with using db context directly in controller =)

VoroninPavel
Автор

I’ve seen a lot of talk about using the DbContext directly in queries etc, but nobody seems to describe how you then go about unit testing that class/method. Do you have any insights into that, or do you rely on integration tests to satisfy the testing requirements?

andrewgreen
Автор

Hi, Milan. Thank you for yet another awesome video! I have learnt a lot from all of your videos! I saw you always use dbContext.Set<Entity>() method instead of dbContext.Entity, there are a difference between them I don't know or they are doing the same thing?

kirillhorn
Автор

Waiting for your next esp. on how to apply Specification Pattern.

zutafe
Автор

What if instead of returning iquerayable the function has some more paramters like expression of orderby, filters, inner joins.
If any of that has a value we will add it to iqueryable and return list at last?

techwithameer
Автор

Hello, Milan :)
I like all your videos! Congratulations for making understanding complex subjects in general really simple to be put to practice, mostly.
Do you have some content in which you explain the Unit of Work Pattern and how you would implement it by using EFCore and the IUnitOfWork interface? Any pointers? I would very much like to hear it from you!

Best regards and keep rockin'!

vamvdotnet
Автор

Hello, Milan
Nice video, expecting the Generic Repository pattern soon.

subashbarik
Автор

You mention querying the database context directly in your query handlers, but that would violate the separation of the application layer from the infrastructure layer in that it then ties directly to the persistance store. Wouldn't it make more sense to encapsulate this in the repository service, then your implementation of the query can still be performant albeit in the infrastructure layer?

daedwill
Автор

Do you guys have a sample repository? I cant find one online

Metronicle
Автор

Hi Milan, I have one question:
Is better to have one method in repository that return the entire entity and then in controller split in many methods for every different dto of the same entity returned or to have one method either in api and repository and then in presentation layer split up the service layer in different methods for every viewmodel/dto needed?

Another option would be to not have a repository but directly a service layer that interact directly with dbcontext and return different dtos from different methods to different endpoints controller, is this the better options?

Thanks in advance.

alfonsdeda
Автор

Milan, thank you for the video! I have a question about architecture. For example, I have 2 aggregate Roots (AR): "Company Customer" and "Individual Customer" which depended from abstract Customer class. So, as I understand I need to have 2 repository interfaces for each AR which will return appropriate ARs objects. But if I need to get all Customers, what should I do? In one hand I can use two requests (1 to get all Company Customers and 1 to get all Individual Customers) and combine them - it looks ugly. In other hand I can move repository interfaces to Infrastructure layer, create some common repository and CustomerDalDto - but here my application layer will be dependent from infrastructure layer... How to solve such task?

alexkanunnikov
Автор

I've been thinking a lot about how to prevent other developers from coding against constraints. Loved the unit test/reflection idea.

herewegotoday
Автор

Hi Milan! First of all, thank you for another amazing content.
I´m trying to use FindAsync(id) on my GetById method but it´s always returning null. Of course, the Id is correct and the data exists in the database. I´m just not figuring out what exactly is happening. This behavior also occurs when I try to use FirstOrDefaultAsync(). What am I missing?

By the way...
- I´m using .NET 6 and EF Core 6.
- My repositories implements the IRepository which contains all the common CRUD operations.
- Basically, my flow is Controller > Service > Repository.
- In my entities, the Id property is a GUID.
- My repo method GetAllAsync() works like a charm.

public async Task<TEntity> GetByIdAsync(Guid id) => await

Thank you!

RodrigoAbib