Generic Repository EntityFramework 6 Implementation

entity-framework entity-framework-6

Question

I have chosen to try out EF because I have been given a new project. There is no persistence in this project; all that I do is collect data. All I have to do is implement some caching.

I have seen a ton of code samples, etc. while researching repository patterns, but they all seem incorrect to me. They implement an Entity to Repository 1:1 relationship.

In my project, I only need to read data—not save it or do anything else. I cannot build 100 separate repositories because I have a large number of entities.

I've chosen to get things going slowly, so all I need is this:

public interface IRepository : IDisposable
{
    IEnumerable<T> GetAll<T>() where T : class;
    IEnumerable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class;
    T GetOne<T>(Expression<Func<T, bool>> predicate) where T : class;
}

public class Repository : IRepository
{
    public IEnumerable<T> GetAll<T>() where T : class
    {
        return ???.ToArray();
    }

    public IEnumerable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class
    {
        return ???.Where(predicate).ToArray();
    }

    public T GetOne<T>(Expression<Func<T, bool>> predicate) where T : class
    {
        return ???.Where(predicate).FirstOrDefault();
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

Where I should insert the "???" that should be my IdbSet is where I am having trouble.

How might I put my tangible repository into practice? Any ideas or a sample from a noddy test are acceptable.

thank you

1
0
12/8/2013 8:27:17 AM

Accepted Answer

You should first modify theGetAll() and Find() ways to returnIQueryable<T> in place ofIEnumerable<T> such that Linq-to-Entities would be used to enable additional dataset querying.

Try something similar to this as of the EF implementation:

public class EFRepository : DbContext, IRepository
{
    // ctor:
    // pass a full connecting-string, or "name=someName" from configuration
    public EFRepository(string connectionStringOrName) : base(connectionStringOrName)
    {
          // init sets
          this.Entities1 = this.Set<EntityOfSomeType>();
          this.Entities2 = this.Set<EntityOfOtherType>();
    }

    public IEnumerable<T> GetAll<T>() where T : class
    {
        return this.Set<T>().ToArray();
    }

    public IEnumerable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class
    {
        return this.Set<T>().Where(predicate).ToArray();
    }

    public T GetOne<T>(Expression<Func<T, bool>> predicate) where T : class
    {
        return this.Set<T>.FirstOrDefault(predicate);
    }

    public void Dispose()
    {
        base.Dispose();
    }

    // Your DbSets...
    public IDbSet<EntityOfSomeType> Entities1 { get; set; }
    public IDbSet<EntityOfAnotherType> Entities2 { get; set; }
}

(I usedDbContext assuming you're using a code-first approach)

1
12/8/2013 9:11:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow