Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using AutoMapper;
- public interface IDataGateway<T>
- {
- Task<T> GetAsync(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool disableTracking = false,
- bool throwIfNotFound = false);
- Task<T> GetSingleAsync(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool disableTracking = false,
- bool throwIfNotFound = false);
- 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);
- Task<TResult> ProjectToAndGetAsync<TResult>(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool throwIfNotFound = false,
- IMapper? mapper = null);
- Task<TResult> ProjectToAndGetSingleAsync<TResult>(
- Expression<Func<T, bool>> query,
- string[]? navigations = null,
- bool throwIfNotFound = false,
- IMapper? mapper = null);
- 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);
- void Add(T entity);
- Task<int> SaveAsync();
- }
- 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 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>();
- }
- }
- internal sealed class UpdateCurrencyInteractor
- : IRequestHandler<UpdateCurrencyRequest, UpdateCurrencyResponse>
- {
- private readonly IDataGateway<Currency> _currencyGateway;
- private readonly IMapper _mapper;
- public UpdateCurrencyInteractor(IDataGateway<Currency> currencyGateway,
- IMapper mapper)
- {
- _currencyGateway = currencyGateway
- ?? throw new ArgumentNullException(nameof(currencyGateway));
- _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
- }
- public async Task<UpdateCurrencyResponse> Handle(
- UpdateCurrencyRequest request, CancellationToken cancellationToken)
- {
- Currency source = _mapper.Map<Currency>(request.Currency);
- Currency currency = await _currencyGateway
- .GetAsync(query: new GetCurrencyById(request.Currency.Id),
- navigations: GetNavigationProperties())
- .ConfigureAwait(false);
- if (currency is null)
- {
- throw new InvalidOperationException("Currency not found");
- }
- currency.UpdateFromSource(source);
- await _currencyGateway
- .SaveAsync()
- .ConfigureAwait(false);
- return new UpdateCurrencyResponse();
- }
- private static string[] GetNavigationProperties()
- {
- return new string[]
- {
- "Name.Translates"
- };
- }
- }
- internal sealed class GetAllCountriesInteractor
- : IRequestHandler<GetAllCountriesRequest, GetAllCountriesResponse>
- {
- private readonly IDataGateway<Country> _countriesGateway;
- public GetAllCountriesInteractor(IDataGateway<Country> countriesGateway)
- {
- _countriesGateway = countriesGateway
- ?? throw new ArgumentNullException(nameof(countriesGateway));
- }
- public async Task<GetAllCountriesResponse> Handle(
- GetAllCountriesRequest request, CancellationToken cancellationToken)
- {
- List<CountryInfoDto> countries = await _countriesGateway
- .ProjectToAndGetListAsync<CountryInfoDto>(navigations: new string[] { "Name.Translates.Locale" })
- .ConfigureAwait(false);
- return new GetAllCountriesResponse(countries);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement