NOT using repository pattern, use the ORM as is (EF)

I’ve gone down many paths and created many implementations of repositories on different projects and… I’ve thrown the towel in and given up on it, here’s why.

Coding for the exception

Do you code for the 1% chance your database is going to change from one technology to another? If you’re thinking about your business’s future state and say yes that’s a possibility then a) they must have a lot of money to afford to do a migration to another DB technology or b) you’re choosing a DB technology for fun or c) something has gone horribly wrong with the first technology you decided to use.

Why throw away the rich LINQ syntax?

LINQ and EF were developed so you could do neat stuff with it to read and traverse object graphs. Creating and maintain a repository that can give you the same flexibility to do that is a monstrous task. In my experience any time I’ve created a repository I’ve ALWAYS had business logic leak into the repository layer to either make queries more performant and/or reduce the number of hits to the database.

I don’t want to create a method for every single permutation of a query that I have to write. I might as well write stored procedures. I don’t want GetOrder, GetOrderWithOrderItem, GetOrderWithOrderItemWithOrderActivity, GetOrderByUserId, and so on… I just want to get the main entity and traverse and include the object graph as I so please.

Most examples of repositories are bullshit

Unless you are developing something REALLY bare-bones like a blog or something your queries are never going to be as simple as 90% of the examples you find on the internet surrounding the repository pattern. I cannot stress this enough! This is something that one has to crawl through the mud to figure out. There will always be that one query that breaks your perfectly thought out repository/solution that you’ve created, and it’s not until that point where you second guess yourself and the technical debt/erosion begins.

Don’t unit test me bro

But what about unit testing if I don’t have a repository? How will I mock? Simple, you don’t. Lets look at it from both angles:

No repository – You can mock the DbContext using an IDbContext or some other tricks but then you’re really unit testing LINQ to Objects and not LINQ to Entities because the query is determined at runtime… OK so that’s not good! So now it’s up to the integration test to cover this.

With repository – You can now mock your repositories and unit test the layer(s) in between. Great right? Well not really… In the cases above where you have to leak logic into the repository layer to make queries more performant and/or less hits to the database, how can your unit tests cover that? It’s now in the repo layer and you don’t want to test IQueryable<T> right? Also let’s be honest, your unit tests aren’t going to cover the queries that have a 20 line .Where() clause and .Include()‘s a bunch of relationships and hits the database again to do all this other stuff, blah, blah, blah anyways because the query is generated at runtime. Also since you created a repository to keep the upper layers persistence ignorant, if you now you want to change your database technology, sorry your unit tests are definitely not going to guarantee the same results at runtime, back to integration tests. So the whole point of the repository seems weird..

2 cents

We already lose a lot of functionality and syntax when using EF over plain stored procedures (bulk inserts, bulk deletes, CTEs, etc.) but I also code in C# so I don’t have to type binary. We use EF so we can have the possibility of using different providers and to work with object graphs in a nice related way amongst many things. Certain abstractions are useful and some are not.

Leave a Comment