I recently learned ASP.NET MVC (I love it). I'm working with a company that uses dependency injection to load a Repository instance in each request, and I'm familiar with using that repository.
But now I'm writing a couple of MVC applications of my own. I don't fully understand the hows and whys of the repository my company uses, and I'm trying to decide the best approach to implement data access.
I am using C# and Entity Framework (with all the latest versions).
I see three general approaches for handling data access.
Regular DB context within a using statement each time I access data. This is simple and it works okay. However, if two locations need to read the same data within one request, the data must be read twice. (With a single repository per request, the same instance would be used in both places and I understand the second read would simply return the data from the first read.)
A typical repository pattern. For reasons I don't understand, this typical pattern involves creating a wrapper class for every table used from the database. That seems wrong to me. In fact, since they are implemented also as interfaces, I'd technically be creating two wrapper classes for each table. EF creates tables for me. I don't believe this approach makes sense.
There is also a generic repository pattern where a single repository class is created to serve all entity objects. This makes much more sense to me. But does it make sense to others? Is the link above the best approach?
I'd love to get some input from others on this topic. Are you writing your own repository, using one of those above, or doing something different altogether. Please share.
I have used a blend of #2 and #3, but I prefer a strict generic repository if possible (stricter than even suggested in the link for #3). #1 is no good because it plays poorly with unit testing.
If you have a smaller domain or need to constrict which entities that your domain allows to be queried, I suppose #2- or #3 that defines entity specific repository interfaces that themselves implement a generic repository- makes sense. However, I find it to be exhausting and unnecessary to write an interface and a concrete implementation for every entity I want to query. What good is
public interface IFooRepository : IRepository<Foo>
(again, unless I need to constrain developers to a set of allowed aggregate roots)?I just define my generic repository interface, with
Add
,Remove
,Get
,GetDeferred
,Count
, andFind
methods (Find returns anIQueryable
interface allowing LINQ), create a concrete generic implementation, and call it a day. I rely heavily onFind
and thus LINQ. If I need to use a specific query more than once, I use extension methods and write the query using LINQ.This covers 95% of my persistence needs. If I need to perform some sort of persistence action that can't be done generically, I use a home-grown
ICommand
API. For example, say I'm working with NHibernate and I need to perform a complex query as part of my domain, or perhaps I need to do a bulk command. The API looks roughly like this:Now I can create an interface to represent a specific command.
I can create a concrete implementation and use raw SQL, NHibernate HQL, whatever, and register it with my service locator.
Now in my business logic I can do something like this:
You can also use a Specification pattern with
IQuery
to build meaningful, user-input-driven queries, rather than having an interface with million confusing properties, but that assumes you don't find the specification pattern confusing in its own right ;).One last piece of the puzzle is when your repository needs to do specific pre- and -post repository operation. Now, you can very easily create an implementation of your generic repository for a specific entity, then override the relevant method(s) and do what you need to do, and update your IoC or service locator registration and be done with it.
However, sometimes this logic is cross-cutting and awkward to implement by overriding a repository method. So I created
IRepositoryBehavior
, which is basically an event sink. (Below is just a rough definition off the top of my head)Now, these behaviors can be anything. Auditing, security checking, soft-delete, enforcing domain constraints, validation, etc. I create a behavior, register it with the IoC or service locator, and modify my generic repository to take in a collection of registered
IRepositoryBehavior
s, and check each behavior against the current repository type and wrap the operation in the pre/post handlers for each applicable behavior.Here's an example soft-delete behavior (soft-delete means that when someone asks to delete an entity, we just mark it as deleted so it can't be returned again, but is never actually physically removed).
Yes, this is basically a simplified and abstracted implementation of NHibernate's event listeners, but that's why I like it. A) I can unit test a behavior without bringing NHibernate into the picture B) I can use these behaviors outside of NHibernate (say the repository is client implementation that wraps REST service calls) C) NH's event listeners can be a real pain in the ass ;)
Here we go for Best Repository Pattern in Asp.Net MVC:
The Repository pattern adds a separation layer between the data and domain layers of an application. It also makes the data access parts of an application better testable.
Database Factory (IDatabaseFactory.cs):
Database Factory Implementations (DatabaseFactory.cs):
Base Interface (IRepository.cs):
Abstract Class (Repository.cs):
How to access this repository pattern in controller:
1. You have User Model :
2. Now you have to create Repository Class of your UserModel
3. Now you have to create UserService Interface (IUserService.cs) with all CRUD Methods:
4. Now you have to create UserService Interface (UserService.cs) with all CRUD Methods:
5. Now you All set for your Repository Pattern and you can access all data in User Controller:
There is a ready to use solution at URF - Unit of Work & (extensible/generic) Repositories Framework. It will save you a lot of time. They implemented a generic repository (also there is an async repository). For extending any repository they have used extensions like this:
Some classes like QueryObject may be an overwork depending on your project but overally it is good solution to help you get up and running.
I would recommend number 1, with some caveats. Number 2 is what seems to be most common but in my experience the repository just ends up a messy dumping ground for queries. If you use a generic repository (2) it is just a thin wrapper around the DBContext, a bit pointless really unless you are planning on changing ORM's (bad idea).
But when I access DBContext directly I prefer to use a Pipes and Filters pattern so you can reuse common logic, something like
The ByPhoneNumber and By Organisation are just extension methods.