Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading.Tasks;
  6.  
  7. using AutoMapper;
  8. using AutoMapper.QueryableExtensions;
  9.  
  10. using LinguaSchools.Domain.Gateways;
  11.  
  12. using Microsoft.EntityFrameworkCore;
  13.  
  14. namespace LinguaSchools.Infrastructure.Db.Gateways
  15. {
  16.     internal sealed class DataGateway<T> : IDataGateway<T> where T : class
  17.     {
  18.         private readonly LSContext _context;
  19.         private readonly IMapper _mapper;
  20.  
  21.         public DataGateway(
  22.             LSContext context,
  23.             IMapper mapper)
  24.         {
  25.             _context = context
  26.                 ?? throw new ArgumentNullException(nameof(context));
  27.  
  28.             _mapper = mapper
  29.                 ?? throw new ArgumentNullException(nameof(mapper));
  30.         }
  31.  
  32.         public void Add(T entity)
  33.         {
  34.             _context.Add(entity);
  35.         }
  36.  
  37.         public void Remove(T entity)
  38.         {
  39.             _context.Remove(entity);
  40.         }
  41.  
  42.         public Task<T> GetAsync(
  43.             Expression<Func<T, bool>> query,
  44.             string[]? navigations = null,
  45.             bool disableTracking = false,
  46.             bool throwIfNotFound = false)
  47.         {
  48.             IQueryable<T> baseQuery = IncludeProperties(navigations, UseTrackingBehavior(disableTracking));
  49.  
  50.             return throwIfNotFound
  51.                 ? baseQuery.FirstAsync(query)
  52.                 : baseQuery.FirstOrDefaultAsync(query);
  53.         }
  54.  
  55.         public Task<TResult> ProjectToAndGetAsync<TResult>(
  56.             Expression<Func<T, bool>> query,
  57.             string[]? navigations = null,
  58.             bool throwIfNotFound = false,
  59.             IMapper? mapper = null)
  60.         {
  61.             IMapper currentMapper = mapper ?? _mapper;
  62.             IQueryable<TResult> baseQuery = UseTrackingBehavior(true).Where(query)
  63.                 .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>());
  64.  
  65.             return throwIfNotFound
  66.                 ? baseQuery.FirstAsync()
  67.                 : baseQuery.FirstOrDefaultAsync();
  68.         }
  69.  
  70.         public Task<List<T>> GetListAsync(
  71.             Expression<Func<T, bool>>? query = null,
  72.             string[]? navigations = null,
  73.             (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
  74.             bool disableTracking = false)
  75.         {
  76.             IQueryable<T> baseQuery = query is null
  77.                 ? UseTrackingBehavior(disableTracking)
  78.                 : UseTrackingBehavior(disableTracking).Where(query);
  79.  
  80.             baseQuery = IncludeProperties(navigations, baseQuery);
  81.  
  82.             if (orderBy != null
  83.                 && orderBy.Length > 0)
  84.             {
  85.                 baseQuery = orderBy.Aggregate(baseQuery, (q, e) =>
  86.                 {
  87.                     return e.desc
  88.                         ? q.OrderByDescending(e.expression)
  89.                         : q.OrderBy(e.expression);
  90.                 });
  91.             }
  92.  
  93.             return baseQuery.ToListAsync();
  94.         }
  95.  
  96.         public Task<List<TResult>> ProjectToAndGetListAsync<TResult>(
  97.             Expression<Func<T, bool>>? query = null,
  98.             string[]? navigations = null,
  99.             (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
  100.             IMapper? mapper = null)
  101.         {
  102.             IMapper currentMapper = mapper ?? _mapper;
  103.             IQueryable<T> baseQuery = query is null
  104.                 ? UseTrackingBehavior(true)
  105.                 : UseTrackingBehavior(true).Where(query);
  106.  
  107.             if (orderBy != null
  108.                 && orderBy.Length > 0)
  109.             {
  110.                 baseQuery = orderBy.Aggregate(baseQuery, (q, e) =>
  111.                 {
  112.                     return e.desc
  113.                         ? q.OrderByDescending(e.expression)
  114.                         : q.OrderBy(e.expression);
  115.                 });
  116.             }
  117.  
  118.             return baseQuery
  119.                 .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>())
  120.                 .ToListAsync();
  121.         }
  122.  
  123.         public Task<T> GetSingleAsync(
  124.             Expression<Func<T, bool>> query,
  125.             string[]? navigations = null,
  126.             bool disableTracking = false,
  127.             bool throwIfNotFound = false)
  128.         {
  129.             IQueryable<T> baseQuery = IncludeProperties(navigations, UseTrackingBehavior(disableTracking));
  130.  
  131.             return throwIfNotFound
  132.                 ? baseQuery.SingleAsync(query)
  133.                 : baseQuery.SingleOrDefaultAsync(query);
  134.         }
  135.  
  136.         public Task<TResult> ProjectToAndGetSingleAsync<TResult>(
  137.             Expression<Func<T, bool>> query,
  138.             string[]? navigations = null,
  139.             bool throwIfNotFound = false,
  140.             IMapper? mapper = null)
  141.         {
  142.             IMapper currentMapper = mapper ?? _mapper;
  143.             IQueryable<TResult> baseQuery = UseTrackingBehavior(true).Where(query)
  144.                 .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>());
  145.  
  146.             return throwIfNotFound
  147.                 ? baseQuery.SingleAsync()
  148.                 : baseQuery.SingleOrDefaultAsync();
  149.         }
  150.  
  151.         public Task<int> SaveAsync()
  152.         {
  153.             return _context.SaveChangesAsync();
  154.         }
  155.  
  156.         private static IQueryable<T> IncludeProperties(string[]? navigations, IQueryable<T> baseQuery)
  157.         {
  158.             if (navigations != null
  159.                 && navigations.Length > 0)
  160.             {
  161.                 baseQuery = navigations
  162.                     .Aggregate(baseQuery, (query, path) => query.Include(path));
  163.             }
  164.  
  165.             return baseQuery;
  166.         }
  167.  
  168.         private IQueryable<T> UseTrackingBehavior(bool disableTracking)
  169.         {
  170.             return disableTracking
  171.                 ? _context.Set<T>().AsNoTracking()
  172.                 : _context.Set<T>();
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement