Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using AutoMapper;
- using AutoMapper.QueryableExtensions;
- using LinguaSchools.Domain.Gateways;
- using Microsoft.EntityFrameworkCore;
- namespace LinguaSchools.Infrastructure.Db.Gateways
- {
- internal sealed class DataGateway<T> : IDataGateway<T> where T : class
- {
- private readonly LSContext _context;
- private readonly IMapper _mapper;
- public DataGateway(
- LSContext context,
- IMapper mapper)
- {
- _context = context
- ?? throw new ArgumentNullException(nameof(context));
- _mapper = mapper
- ?? throw new ArgumentNullException(nameof(mapper));
- }
- public void Add(T entity)
- {
- _context.Add(entity);
- }
- public void Remove(T entity)
- {
- _context.Remove(entity);
- }
- public Task<T> GetAsync(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool disableTracking = false,
- bool throwIfNotFound = false)
- {
- IQueryable<T> baseQuery = IncludeProperties(navigations, UseTrackingBehavior(disableTracking));
- return throwIfNotFound
- ? baseQuery.FirstAsync(query)
- : baseQuery.FirstOrDefaultAsync(query);
- }
- public Task<TResult> ProjectToAndGetAsync<TResult>(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool throwIfNotFound = false,
- IMapper? mapper = null)
- {
- IMapper currentMapper = mapper ?? _mapper;
- IQueryable<TResult> baseQuery = UseTrackingBehavior(true).Where(query)
- .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>());
- return throwIfNotFound
- ? baseQuery.FirstAsync()
- : baseQuery.FirstOrDefaultAsync();
- }
- public Task<List<T>> GetListAsync(
- Expression<Func<T, bool>>? query = null,
- string[]? navigations = null,
- (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
- bool disableTracking = false)
- {
- IQueryable<T> baseQuery = query is null
- ? UseTrackingBehavior(disableTracking)
- : UseTrackingBehavior(disableTracking).Where(query);
- baseQuery = IncludeProperties(navigations, baseQuery);
- if (orderBy != null
- && orderBy.Length > 0)
- {
- baseQuery = orderBy.Aggregate(baseQuery, (q, e) =>
- {
- return e.desc
- ? q.OrderByDescending(e.expression)
- : q.OrderBy(e.expression);
- });
- }
- return baseQuery.ToListAsync();
- }
- public Task<List<TResult>> ProjectToAndGetListAsync<TResult>(
- Expression<Func<T, bool>>? query = null,
- string[]? navigations = null,
- (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
- IMapper? mapper = null)
- {
- IMapper currentMapper = mapper ?? _mapper;
- IQueryable<T> baseQuery = query is null
- ? UseTrackingBehavior(true)
- : UseTrackingBehavior(true).Where(query);
- if (orderBy != null
- && orderBy.Length > 0)
- {
- baseQuery = orderBy.Aggregate(baseQuery, (q, e) =>
- {
- return e.desc
- ? q.OrderByDescending(e.expression)
- : q.OrderBy(e.expression);
- });
- }
- return baseQuery
- .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>())
- .ToListAsync();
- }
- public Task<T> GetSingleAsync(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool disableTracking = false,
- bool throwIfNotFound = false)
- {
- IQueryable<T> baseQuery = IncludeProperties(navigations, UseTrackingBehavior(disableTracking));
- return throwIfNotFound
- ? baseQuery.SingleAsync(query)
- : baseQuery.SingleOrDefaultAsync(query);
- }
- public Task<TResult> ProjectToAndGetSingleAsync<TResult>(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool throwIfNotFound = false,
- IMapper? mapper = null)
- {
- IMapper currentMapper = mapper ?? _mapper;
- IQueryable<TResult> baseQuery = UseTrackingBehavior(true).Where(query)
- .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>());
- return throwIfNotFound
- ? baseQuery.SingleAsync()
- : baseQuery.SingleOrDefaultAsync();
- }
- public Task<int> SaveAsync()
- {
- return _context.SaveChangesAsync();
- }
- private static IQueryable<T> IncludeProperties(string[]? navigations, IQueryable<T> baseQuery)
- {
- if (navigations != null
- && navigations.Length > 0)
- {
- baseQuery = navigations
- .Aggregate(baseQuery, (query, path) => query.Include(path));
- }
- return baseQuery;
- }
- private IQueryable<T> UseTrackingBehavior(bool disableTracking)
- {
- return disableTracking
- ? _context.Set<T>().AsNoTracking()
- : _context.Set<T>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement