optimize async with automapper

This commit is contained in:
Collin M. Barrett 2018-01-24 20:09:49 -06:00
parent 9cb441ba9b
commit b74bebf1fe
3 changed files with 13 additions and 42 deletions

View file

@ -1,26 +0,0 @@
using AutoMapper.Configuration;
using FilterLists.Data.Entities;
using FilterLists.Services.Models;
using FilterLists.Services.Models.Seed;
namespace FilterLists.Services
{
public class MappingProfile : MapperConfigurationExpression
{
public MappingProfile()
{
CreateMap<FilterList, FilterListSummaryDto>();
MapSeedDtos();
}
private void MapSeedDtos()
{
CreateMap<FilterList, FilterListSeedDto>();
CreateMap<Language, LanguageSeedDto>();
CreateMap<License, LicenseSeedDto>();
CreateMap<Maintainer, MaintainerSeedDto>();
CreateMap<Software, SoftwareSeedDto>();
CreateMap<Syntax, SyntaxSeedDto>();
}
}
}

View file

@ -1,26 +1,25 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FilterLists.Data;
using FilterLists.Data.Entities;
using FilterLists.Services.Models;
using Microsoft.EntityFrameworkCore;
namespace FilterLists.Services.Services
{
public class FilterListService
{
private readonly FilterListsDbContext filterListsDbContext;
private readonly IMapper mapper;
public FilterListService(FilterListsDbContext filterListsDbContext, IMapper mapper)
public FilterListService(FilterListsDbContext filterListsDbContext)
{
this.filterListsDbContext = filterListsDbContext;
this.mapper = mapper;
}
public async Task<IEnumerable<FilterListSummaryDto>> GetAllSummaries()
{
return await Task.FromResult(
mapper.Map<IEnumerable<FilterListSummaryDto>>(filterListsDbContext.FilterLists));
return await filterListsDbContext.Set<FilterList>().ProjectTo<FilterListSummaryDto>().ToListAsync();
}
}
}

View file

@ -1,9 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FilterLists.Data;
using FilterLists.Data.Entities;
using FilterLists.Services.Models.Seed;
using Microsoft.EntityFrameworkCore;
namespace FilterLists.Services.Services
{
@ -20,36 +22,32 @@ public SeedService(FilterListsDbContext filterListsDbContext, IMapper mapper)
public async Task<IEnumerable<FilterListSeedDto>> GetAllFilterLists()
{
return await Task.FromResult(
mapper.Map<IEnumerable<FilterListSeedDto>>(filterListsDbContext.Set<FilterList>()));
return await filterListsDbContext.Set<FilterList>().ProjectTo<FilterListSeedDto>().ToListAsync();
}
public async Task<IEnumerable<LanguageSeedDto>> GetAllLanguages()
{
return await Task.FromResult(
mapper.Map<IEnumerable<LanguageSeedDto>>(filterListsDbContext.Set<Language>()));
return await filterListsDbContext.Set<Language>().ProjectTo<LanguageSeedDto>().ToListAsync();
}
public async Task<IEnumerable<LicenseSeedDto>> GetAllLicenses()
{
return await Task.FromResult(mapper.Map<IEnumerable<LicenseSeedDto>>(filterListsDbContext.Set<License>()));
return await filterListsDbContext.Set<License>().ProjectTo<LicenseSeedDto>().ToListAsync();
}
public async Task<IEnumerable<MaintainerSeedDto>> GetAllMaintainers()
{
return await Task.FromResult(
mapper.Map<IEnumerable<MaintainerSeedDto>>(filterListsDbContext.Set<Maintainer>()));
return await filterListsDbContext.Set<Maintainer>().ProjectTo<MaintainerSeedDto>().ToListAsync();
}
public async Task<IEnumerable<SoftwareSeedDto>> GetAllSoftware()
{
return await Task.FromResult(
mapper.Map<IEnumerable<SoftwareSeedDto>>(filterListsDbContext.Set<Software>()));
return await filterListsDbContext.Set<Software>().ProjectTo<SoftwareSeedDto>().ToListAsync();
}
public async Task<IEnumerable<SyntaxSeedDto>> GetAllSyntaxes()
{
return await Task.FromResult(mapper.Map<IEnumerable<SyntaxSeedDto>>(filterListsDbContext.Set<Syntax>()));
return await filterListsDbContext.Set<Syntax>().ProjectTo<SyntaxSeedDto>().ToListAsync();
}
}
}