Refactor BaseItemRepository for ordered DTO materialization
Refactored BaseItemRepository to use a new helper method, MaterializeOrderedDtos, ensuring DTOs are returned in the order of precomputed IDs and improving code reuse. Updated comments to clarify ordering. Cleaned up scenario.json properties and reformatted rebuild-solution.ps1 without changing its logic.
This commit is contained in:
@@ -424,17 +424,13 @@ public sealed class BaseItemRepository
|
||||
return result;
|
||||
}
|
||||
|
||||
// Load full entities with navigations
|
||||
// Load full entities with navigations and preserve the precomputed id order.
|
||||
var dbQueryWithNavs = ApplyNavigations(context.BaseItems.Where(e => pagedIds.Contains(e.Id)), filter);
|
||||
var items = await dbQueryWithNavs
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
result.Items = items
|
||||
.Where(e => e is not null)
|
||||
.Select(w => DeserializeBaseItem(w, filter.SkipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToArray()!;
|
||||
result.Items = MaterializeOrderedDtos(items, pagedIds, filter.SkipDeserialization);
|
||||
result.StartIndex = filter.StartIndex ?? 0;
|
||||
return result;
|
||||
}
|
||||
@@ -497,24 +493,15 @@ public sealed class BaseItemRepository
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var itemsById = items
|
||||
.Select(w => DeserializeBaseItem(w, filter.SkipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToDictionary(i => i!.Id);
|
||||
|
||||
return pagedIds.Where(itemsById.ContainsKey).Select(id => itemsById[id]).ToArray()!;
|
||||
return MaterializeOrderedDtos(items, pagedIds, filter.SkipDeserialization);
|
||||
}
|
||||
|
||||
// Load full entities with navigations
|
||||
// Load full entities with navigations and preserve the precomputed id order.
|
||||
var results = await ApplyNavigations(context.BaseItems.Where(e => pagedIds.Contains(e.Id)), filter)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return results
|
||||
.Where(e => e is not null)
|
||||
.Select(w => DeserializeBaseItem(w, filter.SkipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToArray()!;
|
||||
return MaterializeOrderedDtos(results, pagedIds, filter.SkipDeserialization);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -601,16 +588,12 @@ public sealed class BaseItemRepository
|
||||
return Array.Empty<BaseItem>();
|
||||
}
|
||||
|
||||
// Load full entities with navigations
|
||||
// Load full entities with navigations and preserve the precomputed id order.
|
||||
var results = await ApplyNavigations(context.BaseItems.Where(e => pagedIds.Contains(e.Id)), filter)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return results
|
||||
.Where(e => e is not null)
|
||||
.Select(w => DeserializeBaseItem(w, filter.SkipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToArray()!;
|
||||
return MaterializeOrderedDtos(results, pagedIds, filter.SkipDeserialization);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -655,16 +638,12 @@ public sealed class BaseItemRepository
|
||||
return Array.Empty<BaseItem>();
|
||||
}
|
||||
|
||||
// Load full entities with navigations
|
||||
// Load full entities with navigations and preserve the precomputed id order.
|
||||
var results = await ApplyNavigations(context.BaseItems.Where(e => pagedIds.Contains(e.Id)), filter)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return results
|
||||
.Where(e => e is not null)
|
||||
.Select(w => DeserializeBaseItem(w, filter.SkipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToArray()!;
|
||||
return MaterializeOrderedDtos(results, pagedIds, filter.SkipDeserialization);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -840,6 +819,21 @@ public sealed class BaseItemRepository
|
||||
return paged.ToList();
|
||||
}
|
||||
|
||||
private IReadOnlyList<BaseItemDto> MaterializeOrderedDtos(IReadOnlyList<BaseItemEntity> entities, IReadOnlyList<Guid> orderedIds, bool skipDeserialization)
|
||||
{
|
||||
if (orderedIds.Count == 0 || entities.Count == 0)
|
||||
{
|
||||
return Array.Empty<BaseItemDto>();
|
||||
}
|
||||
|
||||
var itemsById = entities
|
||||
.Select(w => DeserializeBaseItem(w, skipDeserialization))
|
||||
.Where(dto => dto is not null)
|
||||
.ToDictionary(i => i!.Id);
|
||||
|
||||
return orderedIds.Where(itemsById.ContainsKey).Select(id => itemsById[id]).ToArray()!;
|
||||
}
|
||||
|
||||
private IQueryable<BaseItemEntity> ApplyQueryPaging(IQueryable<BaseItemEntity> dbQuery, InternalItemsQuery filter)
|
||||
{
|
||||
if (filter.StartIndex.HasValue && filter.StartIndex.Value > 0)
|
||||
|
||||
Reference in New Issue
Block a user