Created
September 20, 2020 12:13
-
-
Save dhavalmpanchal/14f678798725f75130558c940ce4a8cf to your computer and use it in GitHub Desktop.
IRepository and UnitOfWork
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Text; | |
namespace Taste.DataAccess.Data.Repository.IRepository | |
{ | |
public interface IRepository<T> where T : class | |
{ | |
T Get(int Id); | |
IEnumerable<T> GetAll( | |
Expression<Func<T,bool>> filter = null, | |
Func<IQueryable<T>,IOrderedQueryable<T>> orderBy = null, | |
string includeProperties = null | |
); | |
T GetFirstOrDefault( | |
Expression<Func<T, bool>> filter = null, | |
string includeProperties = null); | |
void Add(T entity); | |
void Remove(int Id); | |
void Remove(T entity); | |
} | |
} |
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Text; | |
using Taste.DataAccess.Data.Repository.IRepository; | |
namespace Taste.DataAccess.Data.Repository | |
{ | |
public class Repository<T> : IRepository<T> where T : class | |
{ | |
protected readonly DbContext Context; | |
internal DbSet<T> dbSet; | |
public Repository(DbContext context) | |
{ | |
Context = context; | |
this.dbSet = context.Set<T>(); | |
} | |
public void Add(T entity) | |
{ | |
dbSet.Add(entity); | |
} | |
public T Get(int Id) | |
{ | |
return dbSet.Find(Id); | |
} | |
public IEnumerable<T> GetAll(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null) | |
{ | |
IQueryable<T> query = dbSet; | |
if (filter != null) | |
{ | |
query = query.Where(filter); | |
} | |
if (includeProperties != null) | |
{ | |
foreach (var item in includeProperties.Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries)) | |
{ | |
query = query.Include(item); | |
} | |
} | |
if (orderBy != null) | |
{ | |
return orderBy(query).ToList(); | |
} | |
return query.ToList(); | |
} | |
public T GetFirstOrDefault(Expression<Func<T, bool>> filter = null, string includeProperties = null) | |
{ | |
IQueryable<T> query = dbSet; | |
if (filter != null) | |
{ | |
query = query.Where(filter); | |
} | |
if (includeProperties != null) | |
{ | |
foreach (var item in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) | |
{ | |
query = query.Include(item); | |
} | |
} | |
return query.FirstOrDefault(); | |
} | |
public void Remove(int Id) | |
{ | |
T entityToRemove = dbSet.Find(Id); | |
dbSet.Remove(entityToRemove); | |
} | |
public void Remove(T entity) | |
{ | |
dbSet.Remove(entity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment