Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Threading.Tasks;
  5. using AutoMapper;
  6.  
  7. public interface IDataGateway<T>
  8. {
  9.     Task<T> GetAsync(
  10.         Expression<Func<T, bool>> query,
  11.         string[]? navigations = null,
  12.         bool disableTracking = false,
  13.         bool throwIfNotFound = false);
  14.  
  15.     Task<T> GetSingleAsync(
  16.         Expression<Func<T, bool>> query,
  17.         string[]? navigations = null,
  18.         bool disableTracking = false,
  19.         bool throwIfNotFound = false);
  20.  
  21.     Task<List<T>> GetListAsync(
  22.         Expression<Func<T, bool>>? query = null,
  23.         string[]? navigations = null,
  24.         (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
  25.         bool disableTracking = false);
  26.  
  27.     Task<TResult> ProjectToAndGetAsync<TResult>(
  28.         Expression<Func<T, bool>> query,
  29.         string[]? navigations = null,
  30.         bool throwIfNotFound = false,
  31.         IMapper? mapper = null);
  32.  
  33.     Task<TResult> ProjectToAndGetSingleAsync<TResult>(
  34.         Expression<Func<T, bool>> query,
  35.         string[]? navigations = null,
  36.         bool throwIfNotFound = false,
  37.         IMapper? mapper = null);
  38.  
  39.     Task<List<TResult>> ProjectToAndGetListAsync<TResult>(
  40.         Expression<Func<T, bool>>? query = null,
  41.         string[]? navigations = null,
  42.         (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
  43.         IMapper? mapper = null);
  44.  
  45.     void Add(T entity);
  46.  
  47.     Task<int> SaveAsync();
  48. }
  49.  
  50. internal sealed class DataGateway<T> : IDataGateway<T> where T : class
  51. {
  52.     private readonly LSContext _context;
  53.     private readonly IMapper _mapper;
  54.  
  55.     public DataGateway(
  56.         LSContext context,
  57.         IMapper mapper)
  58.     {
  59.         _context = context
  60.             ?? throw new ArgumentNullException(nameof(context));
  61.  
  62.         _mapper = mapper
  63.             ?? throw new ArgumentNullException(nameof(mapper));
  64.     }
  65.  
  66.     public void Add(T entity)
  67.     {
  68.         _context.Add(entity);
  69.     }
  70.  
  71.     public Task<T> GetAsync(
  72.         Expression<Func<T, bool>> query,
  73.         string[]? navigations = null,
  74.         bool disableTracking = false,
  75.         bool throwIfNotFound = false)
  76.     {
  77.         IQueryable<T> baseQuery = IncludeProperties(navigations, UseTrackingBehavior(disableTracking));
  78.  
  79.         return throwIfNotFound
  80.             ? baseQuery.FirstAsync(query)
  81.             : baseQuery.FirstOrDefaultAsync(query);
  82.     }
  83.  
  84.     public Task<TResult> ProjectToAndGetAsync<TResult>(
  85.         Expression<Func<T, bool>> query,
  86.         string[]? navigations = null,
  87.         bool throwIfNotFound = false,
  88.         IMapper? mapper = null)
  89.     {
  90.         IMapper currentMapper = mapper ?? _mapper;
  91.         IQueryable<TResult> baseQuery = UseTrackingBehavior(true).Where(query)
  92.             .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>());
  93.  
  94.         return throwIfNotFound
  95.             ? baseQuery.FirstAsync()
  96.             : baseQuery.FirstOrDefaultAsync();
  97.     }
  98.  
  99.     public Task<List<T>> GetListAsync(
  100.         Expression<Func<T, bool>>? query = null,
  101.         string[]? navigations = null,
  102.         (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
  103.         bool disableTracking = false)
  104.     {
  105.         IQueryable<T> baseQuery = query is null
  106.             ? UseTrackingBehavior(disableTracking)
  107.             : UseTrackingBehavior(disableTracking).Where(query);
  108.  
  109.         baseQuery = IncludeProperties(navigations, baseQuery);
  110.  
  111.         if (orderBy != null
  112.             && orderBy.Length > 0)
  113.         {
  114.             baseQuery = orderBy.Aggregate(baseQuery, (q, e) =>
  115.             {
  116.                 return e.desc
  117.                     ? q.OrderByDescending(e.expression)
  118.                     : q.OrderBy(e.expression);
  119.             });
  120.         }
  121.  
  122.         return baseQuery.ToListAsync();
  123.     }
  124.  
  125.     public Task<List<TResult>> ProjectToAndGetListAsync<TResult>(
  126.         Expression<Func<T, bool>>? query = null,
  127.         string[]? navigations = null,
  128.         (Expression<Func<T, object>> expression, bool desc)[]? orderBy = null,
  129.         IMapper? mapper = null)
  130.     {
  131.         IMapper currentMapper = mapper ?? _mapper;
  132.         IQueryable<T> baseQuery = query is null
  133.             ? UseTrackingBehavior(true)
  134.             : UseTrackingBehavior(true).Where(query);
  135.  
  136.         if (orderBy != null
  137.             && orderBy.Length > 0)
  138.         {
  139.             baseQuery = orderBy.Aggregate(baseQuery, (q, e) =>
  140.             {
  141.                 return e.desc
  142.                     ? q.OrderByDescending(e.expression)
  143.                     : q.OrderBy(e.expression);
  144.             });
  145.         }
  146.  
  147.         return baseQuery
  148.             .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>())
  149.             .ToListAsync();
  150.     }
  151.  
  152.     public Task<T> GetSingleAsync(
  153.         Expression<Func<T, bool>> query,
  154.         string[]? navigations = null,
  155.         bool disableTracking = false,
  156.         bool throwIfNotFound = false)
  157.     {
  158.         IQueryable<T> baseQuery = IncludeProperties(navigations, UseTrackingBehavior(disableTracking));
  159.  
  160.         return throwIfNotFound
  161.             ? baseQuery.SingleAsync(query)
  162.             : baseQuery.SingleOrDefaultAsync(query);
  163.     }
  164.  
  165.     public Task<TResult> ProjectToAndGetSingleAsync<TResult>(
  166.         Expression<Func<T, bool>> query,
  167.         string[]? navigations = null,
  168.         bool throwIfNotFound = false,
  169.         IMapper? mapper = null)
  170.     {
  171.         IMapper currentMapper = mapper ?? _mapper;
  172.         IQueryable<TResult> baseQuery = UseTrackingBehavior(true).Where(query)
  173.             .ProjectTo<TResult>(currentMapper.ConfigurationProvider, null, navigations ?? Array.Empty<string>());
  174.  
  175.         return throwIfNotFound
  176.             ? baseQuery.SingleAsync()
  177.             : baseQuery.SingleOrDefaultAsync();
  178.     }
  179.  
  180.     public Task<int> SaveAsync()
  181.     {
  182.         return _context.SaveChangesAsync();
  183.     }
  184.  
  185.     private static IQueryable<T> IncludeProperties(string[]? navigations, IQueryable<T> baseQuery)
  186.     {
  187.         if (navigations != null
  188.             && navigations.Length > 0)
  189.         {
  190.             baseQuery = navigations
  191.                 .Aggregate(baseQuery, (query, path) => query.Include(path));
  192.         }
  193.  
  194.         return baseQuery;
  195.     }
  196.  
  197.     private IQueryable<T> UseTrackingBehavior(bool disableTracking)
  198.     {
  199.         return disableTracking
  200.             ? _context.Set<T>().AsNoTracking()
  201.             : _context.Set<T>();
  202.     }
  203. }
  204.  
  205.  
  206.  
  207. internal sealed class UpdateCurrencyInteractor
  208.         : IRequestHandler<UpdateCurrencyRequest, UpdateCurrencyResponse>
  209.     {
  210.         private readonly IDataGateway<Currency> _currencyGateway;
  211.         private readonly IMapper _mapper;
  212.  
  213.         public UpdateCurrencyInteractor(IDataGateway<Currency> currencyGateway,
  214.                                         IMapper mapper)
  215.         {
  216.             _currencyGateway = currencyGateway
  217.                 ?? throw new ArgumentNullException(nameof(currencyGateway));
  218.             _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
  219.         }
  220.  
  221.         public async Task<UpdateCurrencyResponse> Handle(
  222.             UpdateCurrencyRequest request, CancellationToken cancellationToken)
  223.         {
  224.             Currency source = _mapper.Map<Currency>(request.Currency);
  225.             Currency currency = await _currencyGateway
  226.                 .GetAsync(query: new GetCurrencyById(request.Currency.Id),
  227.                           navigations: GetNavigationProperties())
  228.                 .ConfigureAwait(false);
  229.  
  230.             if (currency is null)
  231.             {
  232.                 throw new InvalidOperationException("Currency not found");
  233.             }
  234.  
  235.             currency.UpdateFromSource(source);
  236.  
  237.             await _currencyGateway
  238.                 .SaveAsync()
  239.                 .ConfigureAwait(false);
  240.  
  241.             return new UpdateCurrencyResponse();
  242.         }
  243.  
  244.         private static string[] GetNavigationProperties()
  245.         {
  246.             return new string[]
  247.             {
  248.                 "Name.Translates"
  249.             };
  250.         }
  251.     }
  252.  
  253. internal sealed class GetAllCountriesInteractor
  254.         : IRequestHandler<GetAllCountriesRequest, GetAllCountriesResponse>
  255.     {
  256.         private readonly IDataGateway<Country> _countriesGateway;
  257.  
  258.         public GetAllCountriesInteractor(IDataGateway<Country> countriesGateway)
  259.         {
  260.             _countriesGateway = countriesGateway
  261.                 ?? throw new ArgumentNullException(nameof(countriesGateway));
  262.         }
  263.  
  264.         public async Task<GetAllCountriesResponse> Handle(
  265.             GetAllCountriesRequest request, CancellationToken cancellationToken)
  266.         {
  267.             List<CountryInfoDto> countries = await _countriesGateway
  268.                 .ProjectToAndGetListAsync<CountryInfoDto>(navigations: new string[] { "Name.Translates.Locale" })
  269.                 .ConfigureAwait(false);
  270.  
  271.             return new GetAllCountriesResponse(countries);
  272.         }
  273.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement