slim service base

This commit is contained in:
Collin Barrett 2018-08-17 15:32:50 -05:00
parent faa898a2f3
commit c6d9b56e0d
4 changed files with 18 additions and 29 deletions

View file

@ -13,18 +13,16 @@ namespace FilterLists.Services.FilterList
[UsedImplicitly]
public class FilterListService : Service
{
public FilterListService(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider) :
base(dbContext, configurationProvider)
public FilterListService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig)
: base(dbContext, mapConfig)
{
}
public async Task<IEnumerable<ListSummaryDto>> GetAllSummariesAsync() =>
await DbContext.FilterLists.OrderBy(l => l.Name)
.ProjectTo<ListSummaryDto>(ConfigurationProvider)
.ToListAsync();
await DbContext.FilterLists.OrderBy(l => l.Name).ProjectTo<ListSummaryDto>(MapConfig).ToListAsync();
public async Task<ListDetailsDto> GetDetailsAsync(uint id) =>
await DbContext.FilterLists.ProjectTo<ListDetailsDto>(ConfigurationProvider)
await DbContext.FilterLists.ProjectTo<ListDetailsDto>(MapConfig)
.FirstOrDefaultAsync(x => x.Id == id)
.FilterParentListFromMaintainerAdditionalLists();
}

View file

@ -13,27 +13,28 @@ namespace FilterLists.Services.Seed
[UsedImplicitly]
public class SeedService : Service
{
public SeedService(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider)
: base(dbContext, configurationProvider)
public SeedService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig)
: base(dbContext, mapConfig)
{
}
public async Task<IEnumerable<TSeedDto>> GetAllAsync<TEntity, TSeedDto>() where TEntity : class =>
await DbContext.Set<TEntity>().ProjectTo<TSeedDto>(ConfigurationProvider).ToArrayAsync();
await DbContext.Set<TEntity>().ProjectTo<TSeedDto>(MapConfig).ToArrayAsync();
public async Task<IEnumerable<TSeedDto>> GetAllAsync<TEntity, TSeedDto>(PropertyInfo primarySort)
where TEntity : class =>
await DbContext.Set<TEntity>()
.OrderBy(x => primarySort.GetValue(x, null))
.ProjectTo<TSeedDto>(ConfigurationProvider)
.ProjectTo<TSeedDto>(MapConfig)
.ToArrayAsync();
public async Task<IEnumerable<TSeedDto>> GetAllAsync<TEntity, TSeedDto>(PropertyInfo primarySort,
PropertyInfo secondarySort) where TEntity : class =>
PropertyInfo secondarySort)
where TEntity : class =>
await DbContext.Set<TEntity>()
.OrderBy(x => primarySort.GetValue(x, null))
.ThenBy(x => secondarySort.GetValue(x, null))
.ProjectTo<TSeedDto>(ConfigurationProvider)
.ProjectTo<TSeedDto>(MapConfig)
.ToArrayAsync();
}
}

View file

@ -7,24 +7,15 @@ namespace FilterLists.Services
[UsedImplicitly]
public class Service
{
protected readonly IConfigurationProvider ConfigurationProvider;
protected readonly FilterListsDbContext DbContext;
protected readonly EmailService EmailService;
protected readonly IConfigurationProvider MapConfig;
public Service(FilterListsDbContext dbContext) => DbContext = dbContext;
public Service(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider)
public Service(FilterListsDbContext dbContext, IConfigurationProvider mapConfig)
{
DbContext = dbContext;
ConfigurationProvider = configurationProvider;
}
public Service(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider,
EmailService emailService)
{
DbContext = dbContext;
ConfigurationProvider = configurationProvider;
EmailService = emailService;
MapConfig = mapConfig;
}
}
}

View file

@ -12,13 +12,12 @@ namespace FilterLists.Services.Snapshot
{
public class SnapshotService : Service
{
private readonly EmailService emailService;
private readonly DateTime yesterday = DateTime.UtcNow.AddDays(-1);
public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider,
EmailService emailService)
: base(dbContext, configurationProvider, emailService)
{
}
: base(dbContext, configurationProvider) => this.emailService = emailService;
public async Task CaptureAsync(int batchSize)
{
@ -42,11 +41,11 @@ await DbContext
.OrderByDescending(d => d)
.FirstOrDefault())
.Take(batchSize)
.ProjectTo<FilterListViewUrlDto>(ConfigurationProvider)
.ProjectTo<FilterListViewUrlDto>(MapConfig)
.ToListAsync();
private IEnumerable<SnapshotDe> CreateSnapshots(IEnumerable<FilterListViewUrlDto> lists) =>
lists.Select(l => new SnapshotDe(DbContext, EmailService, l));
lists.Select(l => new SnapshotDe(DbContext, emailService, l));
private static async Task SaveSnapshots(IEnumerable<SnapshotDe> snapshots)
{