GenericRepository.cs
· 2.4 KiB · C#
Raw
public class GenericRepository<T, TKey> : IGenericRepository<T, TKey>
where T : class
where TKey : IEquatable<TKey>
{
protected readonly ApplicationDbContext Context;
protected readonly IMapper Mapper;
protected GenericRepository(ApplicationDbContext context, IMapper mapper)
{
Context = context;
Mapper = mapper;
}
public async Task<T> GetAsync(TKey id)
{
if (id == null) return null;
return await Context.Set<T>().FindAsync(id);
}
public async Task<TResult> GetAsync<TResult>(TKey id)
{
if (id == null) return default;
// return await Context.Set<T>().FindAsync(id);
return Mapper.Map<TResult>(await Context.Set<T>().FindAsync(id));
}
public async Task<List<T>> GetAllAsync()
{
return await Context.Set<T>().ToListAsync();
}
public async Task<PagedResponse<TResult>> GetAllAsync<TResult>(QueryParameters queryParameters)
where TResult : class
{
var totalCount = await Context.Set<T>().CountAsync();
var totalPages = (int)Math.Ceiling((decimal)totalCount / queryParameters.PageSize);
var items = await Context.Set<T>().AsQueryable()
.Order(queryParameters.SortField, queryParameters.SortDirection)
.Skip((queryParameters.Page - 1) * queryParameters.PageSize)
.Take(queryParameters.PageSize)
.ProjectTo<TResult>(Mapper.ConfigurationProvider)
.ToListAsync();
return new PagedResponse<TResult>
{
Items = items, TotalCount = totalCount,
TotalPages = totalPages,
Page = queryParameters.Page,
PageSize = queryParameters.PageSize
};
}
public async Task<T> AddAsync(T entity)
{
await Context.AddAsync(entity);
await Context.SaveChangesAsync();
return entity;
}
public async Task UpdateAsync(T entity)
{
Context.Update(entity);
Context.Entry(entity).State = EntityState.Modified;
await Context.SaveChangesAsync();
}
public async Task DeleteAsync(TKey id)
{
var entity = await GetAsync(id);
if (entity == null) return;
Context.Set<T>().Remove(entity);
await Context.SaveChangesAsync();
}
public async Task<bool> ExistsAsync(TKey id)
{
var entity = await GetAsync(id);
return entity != null;
}
}
| 1 | public class GenericRepository<T, TKey> : IGenericRepository<T, TKey> |
| 2 | where T : class |
| 3 | where TKey : IEquatable<TKey> |
| 4 | { |
| 5 | protected readonly ApplicationDbContext Context; |
| 6 | protected readonly IMapper Mapper; |
| 7 | |
| 8 | protected GenericRepository(ApplicationDbContext context, IMapper mapper) |
| 9 | { |
| 10 | Context = context; |
| 11 | Mapper = mapper; |
| 12 | } |
| 13 | |
| 14 | public async Task<T> GetAsync(TKey id) |
| 15 | { |
| 16 | if (id == null) return null; |
| 17 | return await Context.Set<T>().FindAsync(id); |
| 18 | } |
| 19 | |
| 20 | public async Task<TResult> GetAsync<TResult>(TKey id) |
| 21 | { |
| 22 | if (id == null) return default; |
| 23 | // return await Context.Set<T>().FindAsync(id); |
| 24 | return Mapper.Map<TResult>(await Context.Set<T>().FindAsync(id)); |
| 25 | } |
| 26 | |
| 27 | public async Task<List<T>> GetAllAsync() |
| 28 | { |
| 29 | return await Context.Set<T>().ToListAsync(); |
| 30 | } |
| 31 | |
| 32 | public async Task<PagedResponse<TResult>> GetAllAsync<TResult>(QueryParameters queryParameters) |
| 33 | where TResult : class |
| 34 | { |
| 35 | var totalCount = await Context.Set<T>().CountAsync(); |
| 36 | var totalPages = (int)Math.Ceiling((decimal)totalCount / queryParameters.PageSize); |
| 37 | |
| 38 | var items = await Context.Set<T>().AsQueryable() |
| 39 | .Order(queryParameters.SortField, queryParameters.SortDirection) |
| 40 | .Skip((queryParameters.Page - 1) * queryParameters.PageSize) |
| 41 | .Take(queryParameters.PageSize) |
| 42 | .ProjectTo<TResult>(Mapper.ConfigurationProvider) |
| 43 | .ToListAsync(); |
| 44 | |
| 45 | return new PagedResponse<TResult> |
| 46 | { |
| 47 | Items = items, TotalCount = totalCount, |
| 48 | TotalPages = totalPages, |
| 49 | Page = queryParameters.Page, |
| 50 | PageSize = queryParameters.PageSize |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | public async Task<T> AddAsync(T entity) |
| 55 | { |
| 56 | await Context.AddAsync(entity); |
| 57 | await Context.SaveChangesAsync(); |
| 58 | return entity; |
| 59 | } |
| 60 | |
| 61 | public async Task UpdateAsync(T entity) |
| 62 | { |
| 63 | Context.Update(entity); |
| 64 | Context.Entry(entity).State = EntityState.Modified; |
| 65 | await Context.SaveChangesAsync(); |
| 66 | } |
| 67 | |
| 68 | public async Task DeleteAsync(TKey id) |
| 69 | { |
| 70 | var entity = await GetAsync(id); |
| 71 | if (entity == null) return; |
| 72 | Context.Set<T>().Remove(entity); |
| 73 | await Context.SaveChangesAsync(); |
| 74 | } |
| 75 | |
| 76 | public async Task<bool> ExistsAsync(TKey id) |
| 77 | { |
| 78 | var entity = await GetAsync(id); |
| 79 | return entity != null; |
| 80 | } |
| 81 | } |
IGenericRepository.cs
· 449 B · C#
Raw
public interface IGenericRepository<T, in TKey> where T : class where TKey : IEquatable<TKey>
{
Task<T> GetAsync(TKey id);
Task<TResult> GetAsync<TResult>(TKey id);
Task<List<T>> GetAllAsync();
Task<PagedResponse<TResult>> GetAllAsync<TResult>(QueryParameters queryParameters) where TResult : class;
Task<T> AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(TKey id);
Task<bool> ExistsAsync(TKey id);
}
| 1 | public interface IGenericRepository<T, in TKey> where T : class where TKey : IEquatable<TKey> |
| 2 | { |
| 3 | Task<T> GetAsync(TKey id); |
| 4 | Task<TResult> GetAsync<TResult>(TKey id); |
| 5 | Task<List<T>> GetAllAsync(); |
| 6 | Task<PagedResponse<TResult>> GetAllAsync<TResult>(QueryParameters queryParameters) where TResult : class; |
| 7 | Task<T> AddAsync(T entity); |
| 8 | Task UpdateAsync(T entity); |
| 9 | Task DeleteAsync(TKey id); |
| 10 | Task<bool> ExistsAsync(TKey id); |
| 11 | } |
PagedResponse.cs
· 225 B · C#
Raw
public class PagedResponse<T>
{
public List<T> Items { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
}
| 1 | public class PagedResponse<T> |
| 2 | { |
| 3 | public List<T> Items { get; set; } |
| 4 | public int TotalCount { get; set; } |
| 5 | public int TotalPages { get; set; } |
| 6 | |
| 7 | public int Page { get; set; } |
| 8 | public int PageSize { get; set; } |
| 9 | } |
QueryParameters.cs
· 246 B · C#
Raw
public class QueryParameters
{
public int Page { get; set; } = 1;
public int PageSize { get; set; } = 15;
public string SortField { get; set; } = "Id";
public SortDirection SortDirection { get; set; } = SortDirection.Ascending;
}
| 1 | public class QueryParameters |
| 2 | { |
| 3 | public int Page { get; set; } = 1; |
| 4 | public int PageSize { get; set; } = 15; |
| 5 | |
| 6 | public string SortField { get; set; } = "Id"; |
| 7 | public SortDirection SortDirection { get; set; } = SortDirection.Ascending; |
| 8 | } |
QueryableExtensions.cs
· 790 B · C#
Raw
public static class QueryableExtensions
{
public static IOrderedQueryable<T> Order<T>(this IQueryable<T> source, string propertyName, SortDirection descending, bool anotherLevel = false)
{
var param = Expression.Parameter(typeof(T), string.Empty);
var property = Expression.PropertyOrField(param, propertyName);
var sort = Expression.Lambda(property, param);
var call = Expression.Call(
typeof(Queryable),
(!anotherLevel ? "OrderBy" : "ThenBy") + (descending == SortDirection.Descending ? "Descending" : string.Empty),
new[] { typeof(T), property.Type },
source.Expression,
Expression.Quote(sort)
);
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call);
}
}
| 1 | public static class QueryableExtensions |
| 2 | { |
| 3 | public static IOrderedQueryable<T> Order<T>(this IQueryable<T> source, string propertyName, SortDirection descending, bool anotherLevel = false) |
| 4 | { |
| 5 | var param = Expression.Parameter(typeof(T), string.Empty); |
| 6 | var property = Expression.PropertyOrField(param, propertyName); |
| 7 | var sort = Expression.Lambda(property, param); |
| 8 | var call = Expression.Call( |
| 9 | typeof(Queryable), |
| 10 | (!anotherLevel ? "OrderBy" : "ThenBy") + (descending == SortDirection.Descending ? "Descending" : string.Empty), |
| 11 | new[] { typeof(T), property.Type }, |
| 12 | source.Expression, |
| 13 | Expression.Quote(sort) |
| 14 | ); |
| 15 | return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call); |
| 16 | } |
| 17 | } |
| 1 | public enum SortDirection |
| 2 | { |
| 3 | Ascending, |
| 4 | Descending |
| 5 | } |