Advertisement
wingman007

RepositorySuggestedImplementation

Sep 28th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. // https://www.youtube.com/watch?v=rtXpYpZdOzM&t=1159s
  2. using Queries.Core.Repositories;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8.  
  9. namespace Queries.Persistence.Repositories
  10. {
  11.     public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
  12.     {
  13.         protected readonly DbContext Context;
  14.  
  15.         public Repository(DbContext context)
  16.         {
  17.             Context = context;
  18.         }
  19.  
  20.         public TEntity Get(int id)
  21.         {
  22.             // Here we are working with a DbContext, not PlutoContext. So we don't have DbSets
  23.             // such as Courses or Authors, and we need to use the generic Set() method to access them.
  24.             return Context.Set<TEntity>().Find(id);
  25.         }
  26.  
  27.         public IEnumerable<TEntity> GetAll()
  28.         {
  29.             // Note that here I've repeated Context.Set<TEntity>() in every method and this is causing
  30.             // too much noise. I could get a reference to the DbSet returned from this method in the
  31.             // constructor and store it in a private field like _entities. This way, the implementation
  32.             // of our methods would be cleaner:
  33.             //
  34.             // _entities.ToList();
  35.             // _entities.Where();
  36.             // _entities.SingleOrDefault();
  37.             //
  38.             // I didn't change it because I wanted the code to look like the videos. But feel free to change
  39.             // this on your own.
  40.             return Context.Set<TEntity>().ToList();
  41.         }
  42.  
  43.         public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
  44.         {
  45.             return Context.Set<TEntity>().Where(predicate);
  46.         }
  47.  
  48.         public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
  49.         {
  50.             return Context.Set<TEntity>().SingleOrDefault(predicate);
  51.         }
  52.  
  53.         public void Add(TEntity entity)
  54.         {
  55.             Context.Set<TEntity>().Add(entity);
  56.         }
  57.  
  58.         public void AddRange(IEnumerable<TEntity> entities)
  59.         {
  60.             Context.Set<TEntity>().AddRange(entities);
  61.         }
  62.  
  63.         public void Remove(TEntity entity)
  64.         {
  65.             Context.Set<TEntity>().Remove(entity);
  66.         }
  67.  
  68.         public void RemoveRange(IEnumerable<TEntity> entities)
  69.         {
  70.             Context.Set<TEntity>().RemoveRange(entities);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement