Created
August 22, 2020 00:15
-
-
Save PizzaConsole/0e765d8a83364ed6ac059834a60e07b2 to your computer and use it in GitHub Desktop.
Generic Repository
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 System; | |
using System.Collections.Generic; | |
namespace Project.Repository | |
{ | |
public interface IRepository<TEntity> where TEntity : class | |
{ | |
IEnumerable<TEntity> GetAll(); | |
IEnumerable<TEntity> GetAllByExpression(Func<TEntity, bool> expression); | |
TEntity GetByExpression(Func<TEntity, bool> expression); | |
TEntity GetById(int id); | |
TEntity AddNew(TEntity model); | |
TEntity Update(TEntity model); | |
bool Delete(int id); | |
void SaveDb(); | |
} | |
} |
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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
namespace Project.Repository | |
{ | |
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class | |
{ | |
private readonly AppDbContext _appDb; | |
public Repository(AppDbContext appDb) | |
{ | |
try | |
{ | |
appDb.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; | |
} | |
catch (Exception) | |
{ | |
} | |
_appDb = appDb; | |
} | |
private DbSet<TEntity> EntityTable => _appDb.Set<TEntity>(); | |
private string PrimaryKey => _appDb.Model.FindEntityType(typeof(TEntity)).FindPrimaryKey().Properties.Select(x => x.Name).FirstOrDefault(); | |
private Func<TEntity, bool> LinqExpression(object Pk) | |
{ | |
var entity = Expression.Parameter(typeof(TEntity)); | |
Expression keyValue = Expression.Property(entity, PrimaryKey); | |
Expression pkValue = Expression.Constant(Pk, keyValue.Type); | |
Expression body = Expression.Equal(keyValue, pkValue); | |
var lambda = Expression.Lambda<Func<TEntity, bool>>(body, entity); | |
return lambda.Compile(); | |
} | |
public IEnumerable<TEntity> GetAll() | |
{ | |
var all = EntityTable.ToList(); | |
return all; | |
} | |
public IEnumerable<TEntity> GetAllByExpression(Func<TEntity, bool> expression) | |
{ | |
var all = EntityTable.Where(expression).ToList(); | |
return all; | |
} | |
public TEntity GetByExpression(Func<TEntity, bool> expression) | |
{ | |
TEntity entity = EntityTable.FirstOrDefault(expression); | |
return entity; | |
} | |
public TEntity GetById(int id) | |
{ | |
TEntity entity = EntityTable.FirstOrDefault(LinqExpression(id)); | |
return entity; | |
} | |
public TEntity AddNew(TEntity entity) | |
{ | |
EntityTable.Add(entity); | |
SaveDb(); | |
return entity; | |
} | |
public TEntity Update(TEntity entity) | |
{ | |
_appDb.Update(entity); | |
SaveDb(); | |
return entity; | |
} | |
public bool Delete(int id) | |
{ | |
var entity = EntityTable.FirstOrDefault(LinqExpression(id)); | |
if (entity != null) | |
{ | |
EntityTable.Remove(entity); | |
SaveDb(); | |
return true; | |
} | |
else return false; | |
} | |
public void SaveDb() | |
{ | |
_appDb.SaveChanges(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment