Skip to content

Instantly share code, notes, and snippets.

@alaawahbah
Last active July 13, 2020 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaawahbah/8e33508b3e167e325c985f4d25ed537a to your computer and use it in GitHub Desktop.
Save alaawahbah/8e33508b3e167e325c985f4d25ed537a to your computer and use it in GitHub Desktop.
using INTDV.BASECLASSES.Shared;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace BaseRepository
{
public class GenericRepository<Entity, PrimaryKey> : IGenericRepository<Entity, PrimaryKey> where Entity : BaseEntity<PrimaryKey>
{
private readonly IBaseContext _genericContext;
private readonly ActiveContext _activeContext;
public GenericRepository(IBaseContext authenticationContext, ActiveContext activeContext)
{
_genericContext = authenticationContext;
_activeContext = activeContext;
}
public async Task<Entity> GetOne(Expression<Func<Entity, bool>> predicate, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
var data = _genericContext.Set<Entity>().AsQueryable();
if (include != null)
{
data = include(data);
}
return await data.FirstOrDefaultAsync(predicate);
}
public async Task<Entity> GetOne(Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
var data = _genericContext.Set<Entity>().AsQueryable();
if (include != null)
{
data = include(data);
}
return await data.FirstOrDefaultAsync();
}
public async Task<Entity> GetOne(PrimaryKey id, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
var data = _genericContext.Set<Entity>().AsQueryable();
if (include != null)
{
data = include(data);
}
return await data.FirstOrDefaultAsync(w => w.Id.Equals(id));
}
public IQueryable<Entity> Query(Expression<Func<Entity, bool>> predicate, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null, Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderBy = null)
{
if (predicate is null)
{
predicate = w => w.IsDeleted == false;
}
else
{
predicate = predicate.And(w => w.IsDeleted == false);
}
var query = _genericContext.Set<Entity>().Where(predicate);
if (include != null)
{
query = include(query);
}
if (orderBy != null)
{
query = orderBy(query);
}
return query;
}
public async Task<PagedList<Entity>> GetSpecificColumns(PagedQuery<Entity, PrimaryKey> query, Expression<Func<Entity, Entity>> select)
{
if (query.Predicate is null)
{
query.Predicate = w => w.IsDeleted == false;
}
else
{
query.Predicate = query.Predicate.And(w => w.IsDeleted == false);
}
var data = _genericContext.Set<Entity>().Where(query.Predicate).Select(select);
return new PagedList<Entity>
{
Count = await data.CountAsync(),
List = await data.Skip((int)(query.PageNumber - 1) * query.PageSize).Take(query.PageSize).ToListAsync(),
PageNumber = query.PageNumber,
PageSize = query.PageSize
};
}
public async Task<PagedList<Entity>> List(PagedQuery<Entity, PrimaryKey> query, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null, Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderBy = null)
{
if (query.Predicate is null)
{
query.Predicate = w => w.IsDeleted == false;
}
else
{
query.Predicate = query.Predicate.And(w => w.IsDeleted == false);
}
//var count = await _genericContext.Set<Entity>().CountAsync(query.Predicate);
var data = _genericContext.Set<Entity>().Where(query.Predicate);
if (include != null)
{
data = include(data);
}
if (orderBy != null)
{
data = orderBy(data);
}
return new PagedList<Entity>
{
Count = await data.CountAsync(),
List = await data.Skip((int)(query.PageNumber - 1) * query.PageSize).Take(query.PageSize).ToListAsync(),
PageNumber = query.PageNumber,
PageSize = query.PageSize
};
}
public async Task<PagedList<Entity>> List(PagedQueryable<Entity, PrimaryKey> query, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null, Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderBy = null)
{
var count = await query.Query.CountAsync();
var data = query.Query;
if (include != null)
{
data = include(data);
}
if (orderBy != null)
{
data = orderBy(data);
}
return new PagedList<Entity>
{
Count = count,
List = await data.Skip((int)(query.PageNumber - 1) * query.PageSize).Take(query.PageSize).ToListAsync(),
PageNumber = query.PageNumber,
PageSize = query.PageSize
};
}
public void AddOne(Entity input)
{
input.DateCreated = DateTime.Now;
input.UserCreated = _activeContext.UserId;
input.IsDeleted = false;
_genericContext.Set<Entity>().Add(input);
}
public async Task<List<Entity>> GetAll(Expression<Func<Entity, bool>> predicate = null, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
if (predicate is null)
{
predicate = w => w.IsDeleted == false;
}
else
{
predicate = predicate.And(w => w.IsDeleted == false);
}
var data = _genericContext.Set<Entity>().Where(predicate).AsQueryable();
if (include != null)
{
data = include(data);
}
return await data.ToListAsync();
}
public void AddList(IEnumerable<Entity> input)
{
foreach (var item in input)
{
item.DateCreated = DateTime.Now;
item.UserCreated = _activeContext.UserId;
item.IsDeleted = false;
}
_genericContext.Set<Entity>().AddRange(input);
}
public void Edit(Entity input)
{
input.UserUpdated = _activeContext.UserId;
input.DateUpdated = DateTime.Now;
if (!(_genericContext.Entry(input) is null))
{
_genericContext.Entry(input).Property("DateCreated").IsModified = false;
_genericContext.Entry(input).Property("UserCreated").IsModified = false;
_genericContext.Entry(input).Property("IsDeleted").IsModified = false;
}
}
public void Delete(Entity input)
{
input.UserUpdated = _activeContext.UserId;
input.DateUpdated = DateTime.Now;
input.IsDeleted = true;
}
public async Task<bool> DeleteById(PrimaryKey id)
{
var input = await GetOne(id);
if (input is null)
{
return false;
}
Delete(input);
return true;
}
public async Task<bool> CommitAsync()
{
return (await _genericContext.CommitAsync()) > 0;
}
public async Task<bool> CheckExists(Expression<Func<Entity, bool>> predicate)
{
return !((await GetOne(predicate)) is null);
}
public Task<long> Count(Expression<Func<Entity, bool>> predicate, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
var data = _genericContext.Set<Entity>().AsQueryable();
if (include != null)
data = include(data);
return data.Where(predicate).LongCountAsync();
}
public Task<TResult> Min<TResult>(Expression<Func<Entity, bool>> predicate, Expression<Func<Entity, TResult>> selector, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
var data = _genericContext.Set<Entity>().AsQueryable();
if (include != null)
data = include(data);
return data.Where(predicate).MinAsync(selector);
}
public Task<TResult> Max<TResult>(Expression<Func<Entity, bool>> predicate, Expression<Func<Entity, TResult>> selector, Func<IQueryable<Entity>, IIncludableQueryable<Entity, object>> include = null)
{
var data = _genericContext.Set<Entity>().AsQueryable();
if (include != null)
data = include(data);
return data.Where(predicate).MaxAsync(selector);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment