Click here to Skip to main content
15,122,218 members
Articles / Programming Languages / C#
Technical Blog
Posted 22 Feb 2012

Tagged as

Stats

14.9K views
7 bookmarked

Generic Repositories – A Silly Abstraction Layer

Rate me:
Please Sign up or sign in to vote.
3.63/5 (7 votes)
22 Feb 2012LGPL31 min read
Let’s look at what generic repositories give you

Update: The post is all about GENERIC repositories as in Repository<T>, not about all types of repositories. Repositories are a great way to abstract away the data source and make your code testable and flexible. Read the comment by Joseph Webster, CISSP to find out more.

My recommendation is against generic repositories since they don't give you any additional value compared to regular repository classes which have been written specifically for the requirements that your project have.

Let’s look at what generic repositories give you:

You Can Change OR/M Implementation At Any Time

Seriously?

  1. If you find yourself having switch OR/M during a project, you have not done your homework before you started the project.
  2. The OR/M choice doesn’t matter since you have abstracted away the features of the chosen OR/M.

IMHO, you’ll stick with one OR/M during a project and switch for the next one (if you have to switch).

You Have To Write Less Code

Here is a Generic repository (NHibernate implementation) from a SO question:

C#
public interface IRepository<T> : IQueryable<T>
{
  void Add(T entity);
  T Get(Guid id);
  void Remove(T entity);
}

public class Repository<T> : IQueryable<T>
{
  private readonly ISession session;

  public Repository(ISession session)
  {
    session = session;
  }

  public Type ElementType
  {
    get { return session.Query<T>().ElementType; }
  }

  public Expression Expression
  {
    get { return session.Query<T>().Expression; }
  }

  public IQueryProvider Provider
  {
    get { return session.Query<T>().Provider; }
  }  

  public void Add(T entity)
  {
    session.Save(entity);
  }

  public T Get(Guid id)
  {
    return session.Get<T>(id);
  }

  IEnumerator IEnumerable.GetEnumerator()
  {
    return this.GetEnumerator();
  }

  public IEnumerator<T> GetEnumerator()
  {
    return session.Query<T>().GetEnumerator();
  }

  public void Remove(T entity)
  {
    session.Delete(entity);
  }
}

Take a look at the methods. All they do is to call methods in NHibernate. You do not win anything by doing so. All you get is an abstraction layer that removes the good things with nhibernate/ef/whatever.

It’s better to create a proper base class and move all repeated (DRY) functionality into it (and therefore still be able to take advantage of the features in your favorite OR/M).

Summary

Did I miss something that a generic repository gives you? Please make a comment.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder 1TCompany AB
Sweden Sweden

Comments and Discussions

 
QuestionDon't repeat yourself. Don't repeat yourself. Pin
An00bus23-Jan-13 5:44
MemberAn00bus23-Jan-13 5:44 
GeneralMy vote of 3 Pin
jim lahey24-Feb-12 5:29
Memberjim lahey24-Feb-12 5:29 
GeneralRe: My vote of 3 Pin
jgauffin25-Feb-12 21:44
Memberjgauffin25-Feb-12 21:44 
GeneralMy vote of 5 Pin
Paul Tait22-Feb-12 19:27
MemberPaul Tait22-Feb-12 19:27 
AnswerWhat about when you are not using (only) an ORM? Pin
Joseph Webster, CISSP22-Feb-12 11:33
MemberJoseph Webster, CISSP22-Feb-12 11:33 
GeneralRe: What about when you are not using (only) an ORM? Pin
jgauffin22-Feb-12 19:44
Memberjgauffin22-Feb-12 19:44 
GeneralMy vote of 5 Pin
Bad code hunter22-Feb-12 9:59
MemberBad code hunter22-Feb-12 9:59 
GeneralMy vote of 1 Pin
Dean Oliver22-Feb-12 9:40
MemberDean Oliver22-Feb-12 9:40 
too short for an article and nothing useful in this article. So many examples online of the repository pattern being implemented.
GeneralRe: My vote of 1 Pin
jgauffin22-Feb-12 19:41
Memberjgauffin22-Feb-12 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.