Skip to content

Commit

Permalink
Add AsNoTracking() to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hchris1 committed Mar 27, 2024
1 parent 589bcf9 commit b9ac636
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aspnet-core/src/CoMon.Application/Assets/AssetAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public async Task<AssetDto> Get(long id)
.OrderByDescending(s => s.Time)
.Take(1))
.AsSplitQuery()
.AsNoTracking()
.SingleOrDefaultAsync()
?? throw new EntityNotFoundException("Asset not found.");

Expand All @@ -45,6 +46,7 @@ public async Task<List<AssetPreviewDto>> GetAllPreviews()
.GetAll()
.Include(a => a.Group.Parent.Parent)
.AsSplitQuery()
.AsNoTracking()
.ToListAsync();

return _objectMapper.Map<List<AssetPreviewDto>>(assets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public async Task<string> GetRecommendations(long statusId)
.Include(s => s.Package)
.ThenInclude(s => s.Asset)
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync()
?? throw new EntityNotFoundException("Status not found.");

Expand All @@ -58,6 +59,7 @@ public async Task<string> GetAssetSummary(long assetId)
.OrderByDescending(s => s.Time)
.Take(1))
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync() ?? throw new KeyNotFoundException("Asset not found.");

var statuses = asset.Packages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task<DashboardDto> Get(long id)
.Where(d => d.Id == id)
.Include(d => d.Tiles)
.AsSplitQuery()
.AsNoTracking()
.SingleOrDefaultAsync()
?? throw new EntityNotFoundException("Dashboard not found for given id.");

Expand All @@ -47,6 +48,7 @@ public async Task<List<DashboardPreviewDto>> GetAllPreviews()
.GetAll()
.Include(d => d.Tiles)
.AsSplitQuery()
.AsNoTracking()
.ToListAsync();

return dashboards.Select(d => new DashboardPreviewDto()
Expand Down
3 changes: 3 additions & 0 deletions aspnet-core/src/CoMon.Application/Groups/GroupAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public async Task<GroupDto> Get(long id)
.Include(g => g.SubGroups)
.Include(g => g.Assets)
.AsSplitQuery()
.AsNoTracking()
.SingleOrDefaultAsync()
?? throw new EntityNotFoundException("Group not found.");

Expand All @@ -74,6 +75,7 @@ public async Task<GroupPreviewDto> GetPreview(long id)
var group = await _groupRepository
.GetAll()
.Where(g => g.Id == id)
.AsNoTracking()
.SingleOrDefaultAsync()
?? throw new EntityNotFoundException("Group not found.");

Expand All @@ -93,6 +95,7 @@ public async Task<List<GroupPreviewDto>> GetAllPreviews()
.GetAll()
.Include(g => g.Parent.Parent)
.AsSplitQuery()
.AsNoTracking()
.ToListAsync();

return _objectMapper.Map<List<GroupPreviewDto>>(groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static async Task<List<Status>> GetLatestStatusesFromGroup(IRepository<Gr
.OrderByDescending(s => s.Time)
.Take(1))
.AsSplitQuery()
.AsNoTracking()
.SingleOrDefaultAsync() ?? throw new EntityNotFoundException("Group not found");

var statuses = group.Assets.Select(a => a.Packages).SelectMany(x => x).Select(p => p.Statuses).SelectMany(x => x).ToList();
Expand Down Expand Up @@ -56,6 +57,7 @@ public static async Task<List<Status>> GetAllLatestStatuses(IRepository<Group, l
.OrderByDescending(s => s.Time)
.Take(1))
.AsSplitQuery()
.AsNoTracking()
.ToListAsync();

statuses.AddRange(assets.Select(a => a.Packages).SelectMany(x => x).Select(p => p.Statuses).SelectMany(x => x).ToList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ await _packageRepository
.Take(1))
.Where(p => p.Id == id)
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync()
?? throw new EntityNotFoundException("Package not found"));
}
Expand All @@ -48,6 +49,7 @@ await _packageRepository
.ThenInclude(a => a.Group.Parent.Parent)
.Where(p => p.Id == id)
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync()
?? throw new EntityNotFoundException("Package not found"));
}
Expand Down
10 changes: 10 additions & 0 deletions aspnet-core/src/CoMon.Application/Statuses/StatusAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public async Task<StatusDto> Get(long id)
.ThenInclude(c => c.Series)
.ThenInclude(s => s.DataPoints)
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync()
?? throw new EntityNotFoundException("Status not found.");

Expand Down Expand Up @@ -84,6 +85,7 @@ public async Task<StatusPreviewDto> GetPreview(long id)
.ThenInclude(p => p.Asset)
.ThenInclude(a => a.Group.Parent.Parent)
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync()
?? throw new EntityNotFoundException("Status not found.");
status.IsLatest = await IsLatest(status);
Expand All @@ -108,13 +110,15 @@ public async Task<StatusHistoryDto> GetHistory(long id)
.Include(s => s.Package)
.Where(s => s.Id == id)
.AsSplitQuery()
.AsNoTracking()
.FirstOrDefaultAsync()
?? throw new EntityNotFoundException("Status not found.");

var latestStatus = await _statusRepository
.GetAll()
.Where(s => s.Package.Id == status.Package.Id)
.OrderByDescending(s => s.Time)
.AsNoTracking()
.FirstOrDefaultAsync();

if (latestStatus.Id == id)
Expand All @@ -125,13 +129,15 @@ public async Task<StatusHistoryDto> GetHistory(long id)
.Where(s => s.Package.Id == status.Package.Id)
.Where(s => s.Time < status.Time)
.OrderByDescending(s => s.Time)
.AsNoTracking()
.FirstOrDefaultAsync();

var nextStatus = await _statusRepository
.GetAll()
.Where(s => s.Package.Id == status.Package.Id)
.Where(s => s.Time > status.Time)
.OrderBy(s => s.Time)
.AsNoTracking()
.FirstOrDefaultAsync();

return new StatusHistoryDto()
Expand All @@ -149,12 +155,14 @@ public async Task<StatusTableOptionsDto> GetStatusTableOptions()
.GetAll()
.Include(a => a.Group.Parent.Parent)
.AsSplitQuery()
.AsNoTracking()
.ToListAsync();

var groups = await _groupRepository
.GetAll()
.Include(g => g.Parent.Parent)
.AsSplitQuery()
.AsNoTracking()
.ToListAsync();

return new StatusTableOptionsDto()
Expand Down Expand Up @@ -218,6 +226,7 @@ public async Task<PagedResultDto<StatusPreviewDto>> GetStatusTable(PagedResultRe
.Skip(request.SkipCount)
.Take(request.MaxResultCount)
.AsSplitQuery()
.AsNoTracking()
.ToList();

foreach (var status in statuses)
Expand All @@ -241,6 +250,7 @@ public async Task<StatusPreviewDto> GetLatestStatusPreview(long packageId)
.OrderBy(p => p.Key.Name)
.Select(g => g.OrderByDescending(s => s.Time).FirstOrDefault())
.AsSplitQuery()
.AsNoTracking()
.SingleOrDefaultAsync();

return _objectMapper.Map<StatusPreviewDto>(status);
Expand Down

0 comments on commit b9ac636

Please sign in to comment.