Last active 1 year ago

GenericRepository.cs Raw
1public 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 Raw
1public 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 Raw
1public 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 Raw
1public 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 Raw
1public 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}
SortDirection.cs Raw
1public enum SortDirection
2{
3 Ascending,
4 Descending
5}