Created
September 18, 2021 17:46
-
-
Save srdelarosa/c1f00de749c0f368e3d70467c794e2f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using NH.WikiMovies.Infrastructure.Context; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
namespace NH.WikiMovies.Infrastructure.Repositories | |
{ | |
public class GenericRepository<T> where T : class | |
{ | |
protected readonly WikiMoviesContext _context; | |
public GenericRepository(WikiMoviesContext context) | |
{ | |
_context = context; | |
} | |
public void Add(T entity) | |
{ | |
_context.Set<T>().Add(entity); | |
} | |
public void AddRange(IEnumerable<T> entities) | |
{ | |
_context.Set<T>().AddRange(entities); | |
} | |
public IEnumerable<T> Find(Expression<Func<T, bool>> expression) | |
{ | |
return _context.Set<T>().Where(expression); | |
} | |
public IEnumerable<T> List(Expression<Func<T, bool>> filter = null, | |
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "") | |
{ | |
IQueryable<T> query = _context.Set<T>(); | |
if (filter != null) | |
{ | |
query = query.Where(filter); | |
} | |
foreach (var includeProperty in includeProperties.Split | |
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) | |
{ | |
query = query.Include(includeProperty); | |
} | |
if (orderBy != null) | |
{ | |
return orderBy(query).ToList(); | |
} | |
else | |
{ | |
return query.ToList(); | |
} | |
} | |
public virtual IQueryable<T> Query(Expression<Func<T, bool>> filter = null, | |
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "") | |
{ | |
IQueryable<T> query = _context.Set<T>(); | |
if (filter != null) | |
{ | |
query = query.Where(filter); | |
} | |
foreach (var includeProperty in includeProperties.Split | |
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) | |
{ | |
query = query.Include(includeProperty); | |
} | |
if (orderBy != null) | |
{ | |
return orderBy(query).AsQueryable(); | |
} | |
else | |
{ | |
return query; | |
} | |
} | |
public T GetById(int id) | |
{ | |
return _context.Set<T>().Find(id); | |
} | |
public void Remove(T entity) | |
{ | |
_context.Set<T>().Remove(entity); | |
} | |
public void RemoveRange(IEnumerable<T> entities) | |
{ | |
_context.Set<T>().RemoveRange(entities); | |
} | |
public virtual void Update(T entityToUpdate) | |
{ | |
_context.Set<T>().Attach(entityToUpdate); | |
_context.Entry(entityToUpdate).State = EntityState.Modified; | |
} | |
public virtual void Update(T entityToUpdate, Func<T, int> getKey) | |
{ | |
var entry = _context.Entry<T>(entityToUpdate); | |
if (entry.State == EntityState.Detached) | |
{ | |
T attachedEntity = _context.Set<T>().Find(getKey(entityToUpdate)); | |
if (attachedEntity != null) | |
{ | |
var attachedEntry = _context.Entry(attachedEntity); | |
attachedEntry.CurrentValues.SetValues(entityToUpdate); | |
} | |
else | |
{ | |
entry.State = EntityState.Modified; | |
} | |
} | |
} | |
public virtual void Unmark(T entity) | |
{ | |
_context.Entry(entity).State = EntityState.Unchanged; | |
} | |
public virtual IQueryable<T> Queryable() | |
{ | |
return _context.Set<T>().AsQueryable<T>(); | |
} | |
public virtual void Save() | |
{ | |
_context.SaveChanges(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment