In .NET land whether to use the repository pattern over Entity Framework is a highly debated topic. I wanted to get some figures so ran a poll 📊 on my LinkedIn and Twitter profiles recently asking who uses them together.
The results are below …
We can see that on LinkedIn using the repository pattern with EF seems to be by far the more popular approach, whereas on Twitter the results are much closer. I’m not sure how to interpret this difference.
Do I use repository pattern over Entity Framework?
For me I ALMOST NEVER use the repository pattern over EF, in short I simply don’t think it’s needed or adds value in most cases as EF already has repo and UOW. So for KISS and YAGNI I just don’t use it… mostly. I would use it very occasionally in big systems with lots of developers and multiple data sources …
Some of the typical uses cases I see for using it are below…
❌ Swap out ORM – YAGNI, most likely won’t need this but if you do handle IF needed. It’s unlikely you can just swap ORMs out anyhow, it doesn’t work like this in practice, unless you’ve literally not used hardly any functionality of the ORM.. in which case why use it at all?
❌ Mocking support – better to test with REAL DB – many devs like to use repo pattern to support unit testing, which is fine but for me I much prefer to test against real DB. Our production systems don’t run on mocks, they run on real DBs.
❌ Abstraction ‘you own’ – is this of value in itself?
✅ Multiple datastores (eg SQL Server, Cosmos, File System, Web Service etc.) – use repository pattern to provide a simplified façade or wrapper over reading from all the different sources and so this gives a consistent API to devs and to consuming client code like service classes or MediatR handlers.
Do the Entity Framework team recommend using the repository pattern with Entity Framework?
Only when it adds value.
For the most part they believe it represents over-engineering.
Check out their thoughts from a recent Entity Framework community standup which dealt with EF FAQs. One of which was ‘Should I use create a repository over EF Core?‘
Do you use the repository pattern over Entity Framework?
Why?