HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Repository for data retrieval

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
dataforrepositoryretrieval

Problem

I have created a really simple repository for some simple data retrieval and was wondering if i was going in the right direction. Has anyone got any thoughts on this?

interface

public interface IRepository where T : class {
    IEnumerable GetAll();
    T GetById(int id);
    IEnumerable Filter(Expression> filter);
    void Dispose();
}


class

```
public class JobRepository : IRepository, IDisposable{
///
/// handles all job data retrieval
/// crude but will suffice for now
///
CentralRepositoryContainer _context = null;

public JobRepository() {
CentralRepositoryContainer context =
HttpContext.Current.GetCentralRepositoryContext();
}

///
/// returns all jobs
///
/// IEnumerable
public IEnumerable GetAll() {
try {
return this._context.Jobs;
} catch (SqlException ex) {
//error so dispose of context
this.Dispose();
//wrap and rethrow back to caller
throw new CentralRepositoryException("Error getting all jobs", ex);
}

}

///
/// return job
///
/// integer to search on
/// job
public Job GetById(int id) {
try {
return this._context.Jobs.SingleOrDefault(j => j.JobId == id);
} catch (SqlException ex) {
//error so dispose of context
this.Dispose();
//wrap and rethrow back to caller
throw new CentralRepositoryException("Error getting all job by id", ex);
}
}

///
/// filters based on parsed filter expression
///
/// filter to search on
/// IQueryable
public IEnumerable Filter(System.Linq.Expressions.Expression> filter) {
try {
return this._context.Jobs.Where(filter);
} catch (SqlException ex) {
//error so dispose of context
this.Dispose();
//wrap and rethrow back to caller
throw new Cent

Solution

Especially in such simple cases you don't want to create a repository over an EF (I assume it's EF -- but the same goes for Linq2SQL) context.

The DbContext basically is an implementation of the repository pattern. Your repository on top just creates another layer of complexity without actually providing any serious benefit.

In more complicated cases wrapping the DbContext with a specific interface may make sense of course.

Context

StackExchange Code Review Q#5586, answer score: 3

Revisions (0)

No revisions yet.