Timeline for Why shouldn't I use the repository pattern with Entity Framework?
Current License: CC BY-SA 3.0
26 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 4, 2019 at 6:00 | comment | added | Pavel Donchev | The real problem here lies in the "new DbContext" declaration in the Repository constructor. If you have multiple repositories that depend on each other you will get all sort of problems due to the fact that EF is meant to work with single context. Not to mention multithreading. | |
| Dec 30, 2017 at 4:29 | comment | added | ColacX | @user441521 that's true if people feel comfortable with using a request-life-time-cycle on their dependency injection. personally I feel that it is prone to hard detect bugs as one can easily inject the instances incorrectly. that being said one can say that repository pattern must be used with DI to function correctly. i still feel the repo pattern is outdated a stateless service is cleaner, easier and guarantees that you cannot get stuck in a "incorrect-middle-state". | |
| Dec 28, 2017 at 17:57 | comment | added | user441521 | @ColacX With Microsoft Unity DI framework, and I would assume most DI frameworks, you can control the lifespan of these objects. By default a new instance is created each time it's needed, but you can tell it not to do that and use the 1 instance (first instance acting like a singleton of sorts) with all subsequent requests to resolve. That way you get the same/shared instance between everything that needs it for said request. | |
| Dec 25, 2017 at 23:19 | comment | added | ColacX | @user441521 no the reason why is because the DbContext creates its own instance of a DbConnection. Thus when reusing the multiple services in the same request, it actually creates multiple connections which was not the intention. Worse is when you try to fix the problem by wrapping the whole request in one DbTransaction then the transaction becomes a distributed transaction, which again was not intended. | |
| Dec 21, 2017 at 18:58 | comment | added | user441521 | @ColacX You don't use a DI framework to get the DbContext to the Service and then to get the Service to your controller? | |
| Dec 18, 2017 at 15:11 | comment | added | ColacX | @user441521 I did not write my comment previous properly my apologies, I did not mean to put all the code into the Controller layer what I mean is that you instantiate the DbContext instance and/or transactions in the Controller layer. What I do then is to pass the reference down into the Service layer. This similar to the Repo pattern the difference is that the Repo pattern creates the DbContext in the Repo layer which causes all the problems mentioned previously. You can easily mock and unit test this Service Layer without problems. | |
| Dec 6, 2017 at 20:35 | comment | added | Shyju | yea. This answer was written a long time ago. But i tend to still wrap EF DbContext code behind an interface so that i can mock easily | |
| Dec 6, 2017 at 20:33 | comment | added | user441521 | @ColacX I'd be curious of your thoughts now. I've found EF's dbcontext very easy to mock. I don't use EF directly in controllers but in a service layer instead as I treat it as a repo. Saves complexity, typing, readability, etc I find. | |
| Sep 7, 2016 at 18:41 | history | edited | Shyju | CC BY-SA 3.0 | added 966 characters in body |
| Sep 7, 2016 at 18:33 | comment | added | Kelum | @Shyju Can you refer a good article that mentioning of "Implement the Repository Pattern in an ASP NET MVC and WEB API" ? | |
| Jul 11, 2016 at 22:57 | comment | added | Rhyous | ColacX. We tried just that--DBcontext right in the controller layer--and we are reverting to the repo pattern. With the Repo pattern, the Unit Tests went from massive DbContext mocking that constantly failed. EF was was difficult to use and brittle and cost hours of research for EF nuances. We now have small simple mocks of the repo. The code is cleaner. The separation of work is clearer. I no longer agree with the crowd that EF is already a repo pattern and already unit testable. | |
| Jan 21, 2016 at 10:11 | comment | added | ColacX | I've faced plenty of problems with the repository pattern. "Distributed transactions", "Connection Pool Exhaustion", "Partial Rollback" this all stems from trying to reuse multiple repository in the same transaction, since all the code lies there. Use UnitOfWork pattern or better yet skip this overabstraction nonsense and just create the DBContext and transactions in the Controller layer. | |
| Dec 18, 2015 at 5:13 | comment | added | Thunder | But It is not possible to get migrations to work , as the connection string resides int he web.config. There is no way that migrations gets the connection string from web.config | |
| S Jan 16, 2015 at 19:55 | history | suggested | Zac Blazic | CC BY-SA 3.0 | Fixed spelling of Repository, added grammar fixes. |
| Jan 16, 2015 at 19:35 | review | Suggested edits | |||
| S Jan 16, 2015 at 19:55 | |||||
| Mar 6, 2014 at 15:21 | history | edited | Shyju | CC BY-SA 3.0 | added 408 characters in body |
| Mar 6, 2014 at 15:16 | comment | added | Eric King | I just wanted to add my perspective of how Entity Framework is not a repository | |
| Mar 5, 2014 at 23:09 | comment | added | Piotr Perak | There are many reasons not to use Repository pattern with OR Mappers. It's two years after this answer was given. Now you can find all of them easily on Google. | |
| Dec 3, 2013 at 4:34 | comment | added | Daniel Little | DbContext is already a repository, the repository is meant to be a low level abstraction. If you want to abstract different data sources create objects to represent those. | |
| Aug 30, 2013 at 19:16 | comment | added | Dave | When was the last time that most popular == correct? | |
| Dec 28, 2012 at 15:23 | history | edited | gnat | CC BY-SA 3.0 | added 5 characters in body |
| Dec 28, 2012 at 14:53 | history | migrated | from stackoverflow.com (revisions) | ||
| Dec 15, 2012 at 12:21 | comment | added | StringBuilder | Ok , since this is the most popular answer I'll chose it as Correct Answer | |
| Dec 15, 2012 at 12:21 | vote | accept | CommunityBot | ||
| Dec 12, 2012 at 21:08 | comment | added | StringBuilder | I didn't said it will not work , I am also worked with repository pattern with EF , but today I was asked why IT IS NOT GOOD to use the pattern with DataBase , application that using Database | |
| Dec 12, 2012 at 21:02 | history | answered | Shyju | CC BY-SA 3.0 |