Skip to content

Commit

Permalink
feat: support tracking and non-tracking of entities to improve applic…
Browse files Browse the repository at this point in the history
…ation performance
  • Loading branch information
CarlosPavajeau committed Aug 9, 2021
1 parent 0e8206a commit 9a35585
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Armory.Armament.Ammunition.Domain
public interface IAmmunitionRepository
{
Task Save(Ammunition ammunition);
Task<Ammunition> Find(string code);
Task<IEnumerable<Ammunition>> SearchAll();
Task<Ammunition> Find(string code, bool noTracking = true);
Task<IEnumerable<Ammunition>> SearchAll(bool noTracking = true);
Task<bool> Any(Expression<Func<Ammunition, bool>> predicate);
Task Update(Ammunition newAmmunition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ public async Task Save(Domain.Ammunition ammunition)
await _context.SaveChangesAsync();
}

public async Task<Domain.Ammunition> Find(string code)
public async Task<Domain.Ammunition> Find(string code, bool noTracking = true)
{
if (noTracking)
return await _context.Ammunition
.AsNoTracking()
.FirstOrDefaultAsync(c => c.Code == code);

return await _context.Ammunition.FindAsync(code);
}

public async Task<IEnumerable<Domain.Ammunition>> SearchAll()
public async Task<IEnumerable<Domain.Ammunition>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.Ammunition
.AsNoTracking()
.ToListAsync();

return await _context.Ammunition.ToListAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Armory.Armament.Equipments.Domain
public interface IEquipmentsRepository
{
Task Save(Equipment equipment);
Task<Equipment> Find(string code);
Task<IEnumerable<Equipment>> SearchAll();
Task<Equipment> Find(string code, bool noTracking = true);
Task<IEnumerable<Equipment>> SearchAll(bool noTracking = true);
Task<bool> Any(Expression<Func<Equipment, bool>> predicate);
Task Update(Equipment newEquipment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ public async Task Save(Equipment equipment)
await _context.SaveChangesAsync();
}

public async Task<Equipment> Find(string code)
public async Task<Equipment> Find(string code, bool noTracking = true)
{
if (noTracking)
return await _context.Equipments
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Code == code);

return await _context.Equipments.FindAsync(code);
}

public async Task<IEnumerable<Equipment>> SearchAll()
public async Task<IEnumerable<Equipment>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.Equipments
.AsNoTracking()
.ToListAsync();

return await _context.Equipments.ToListAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Armory.Armament.Explosives.Domain
public interface IExplosivesRepository
{
Task Save(Explosive explosive);
Task<Explosive> Find(string code);
Task<IEnumerable<Explosive>> SearchAll();
Task<Explosive> Find(string code, bool noTracking = true);
Task<IEnumerable<Explosive>> SearchAll(bool noTracking = true);
Task<bool> Any(Expression<Func<Explosive, bool>> predicate);
Task Update(Explosive newExplosive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ public async Task Save(Explosive explosive)
await _context.SaveChangesAsync();
}

public async Task<Explosive> Find(string code)
public async Task<Explosive> Find(string code, bool noTracking = true)
{
if (noTracking)
return await _context.Explosives
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Code == code);

return await _context.Explosives.FindAsync(code);
}

public async Task<IEnumerable<Explosive>> SearchAll()
public async Task<IEnumerable<Explosive>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.Explosives
.AsNoTracking()
.ToListAsync();

return await _context.Explosives.ToListAsync();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Armory/Armament/Weapons/Domain/IWeaponsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Armory.Armament.Weapons.Domain
public interface IWeaponsRepository
{
Task Save(Weapon weapon);
Task<Weapon> Find(string code);
Task<IEnumerable<Weapon>> SearchAll();
Task<Weapon> Find(string code, bool noTracking = true);
Task<IEnumerable<Weapon>> SearchAll(bool noTracking = true);
Task<bool> Any(Expression<Func<Weapon, bool>> predicate);
Task Update(Weapon newWeapon);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ public async Task Save(Weapon weapon)
await _context.SaveChangesAsync();
}

public async Task<Weapon> Find(string code)
public async Task<Weapon> Find(string code, bool noTracking = true)
{
if (noTracking)
return await _context.Weapons
.AsNoTracking()
.FirstOrDefaultAsync(w => w.Code == code);

return await _context.Weapons.FindAsync(code);
}

public async Task<IEnumerable<Weapon>> SearchAll()
public async Task<IEnumerable<Weapon>> SearchAll(bool noTracking = true)
{
return await _context.Weapons.ToListAsync();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Armory/Degrees/Domain/IDegreesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Armory.Degrees.Domain
public interface IDegreesRepository
{
Task Save(Degree degree);
Task<Degree> Find(int id);
Task<IEnumerable<Degree>> SearchAll();
Task<IEnumerable<Degree>> SearchAllByRank(int rankId);
Task<Degree> Find(int id, bool noTracking = true);
Task<IEnumerable<Degree>> SearchAll(bool noTracking = true);
Task<IEnumerable<Degree>> SearchAllByRank(int rankId, bool noTracking = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,34 @@ public async Task Save(Degree degree)
await _context.SaveChangesAsync();
}

public async Task<Degree> Find(int id)
public async Task<Degree> Find(int id, bool noTracking = true)
{
if (noTracking)
return await _context.Degrees
.AsNoTracking()
.FirstOrDefaultAsync(d => d.Id == id);

return await _context.Degrees.FindAsync(id);
}

public async Task<IEnumerable<Degree>> SearchAll()
public async Task<IEnumerable<Degree>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.Degrees
.AsNoTracking()
.ToListAsync();

return await _context.Degrees.ToListAsync();
}

public async Task<IEnumerable<Degree>> SearchAllByRank(int rankId)
public async Task<IEnumerable<Degree>> SearchAllByRank(int rankId, bool noTracking = true)
{
if (noTracking)
return await _context.Degrees
.AsNoTracking()
.Where(d => d.RankId == rankId)
.ToListAsync();

return await _context.Degrees.Where(d => d.RankId == rankId).ToListAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Armory.Formats.WarMaterialAndSpecialEquipmentAssignmentFormats.Domain
public interface IWarMaterialAndSpecialEquipmentAssignmentFormatsRepository
{
Task Save(WarMaterialAndSpecialEquipmentAssignmentFormat format);
Task<WarMaterialAndSpecialEquipmentAssignmentFormat> Find(int id);
Task<WarMaterialAndSpecialEquipmentAssignmentFormat> Find(int id, bool noTracking = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public async Task Save(WarMaterialAndSpecialEquipmentAssignmentFormat format)
await _context.SaveChangesAsync();
}

public async Task<WarMaterialAndSpecialEquipmentAssignmentFormat> Find(int id)
public async Task<WarMaterialAndSpecialEquipmentAssignmentFormat> Find(int id, bool noTracking = true)
{
var format = await _context.WarMaterialAndSpecialEquipmentAssignmentFormats
var query = noTracking
? _context.WarMaterialAndSpecialEquipmentAssignmentFormats.AsNoTracking()
: _context.WarMaterialAndSpecialEquipmentAssignmentFormats.AsTracking();
var format = await query
.Include(f => f.WarMaterialAndSpecialEquipmentAssignmentFormatAmmunition)
.ThenInclude(x => x.Ammunition)
.Include(f => f.WarMaterialAndSpecialEquipmentAssignmentFormatEquipments)
Expand Down
8 changes: 4 additions & 4 deletions src/Armory/People/Domain/IPeopleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public interface IPeopleRepository
{
Task Save(Person person);

Task<Person> Find(string personId);
Task<Person> FindByArmoryUserId(string armoryUserId);
Task<Person> Find(string personId, bool noTracking = true);
Task<Person> FindByArmoryUserId(string armoryUserId, bool noTracking = true);

Task<IEnumerable<Person>> SearchAll();
Task<IEnumerable<Person>> SearchAllByRole(string roleName);
Task<IEnumerable<Person>> SearchAll(bool noTracking = true);
Task<IEnumerable<Person>> SearchAllByRole(string roleName, bool noTracking = true);
Task<bool> Any(Expression<Func<Person, bool>> predicate);

Task Update(Person newPerson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,47 @@ public async Task Save(Person person)
await _context.SaveChangesAsync();
}

public async Task<Person> Find(string personId)
public async Task<Person> Find(string personId, bool noTracking = true)
{
if (noTracking)
return await _context.People
.AsNoTracking()
.FirstOrDefaultAsync(p => p.Id == personId);

return await _context.People.FindAsync(personId);
}

public async Task<Person> FindByArmoryUserId(string armoryUserId)
public async Task<Person> FindByArmoryUserId(string armoryUserId, bool noTracking = true)
{
if (noTracking)
return await _context.People
.AsNoTracking()
.FirstOrDefaultAsync(p => p.ArmoryUserId == armoryUserId);

return await _context.People.FirstOrDefaultAsync(p => p.ArmoryUserId == armoryUserId);
}

public async Task<IEnumerable<Person>> SearchAll()
public async Task<IEnumerable<Person>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.People
.AsNoTracking()
.ToListAsync();

return await _context.People.ToListAsync();
}

public async Task<IEnumerable<Person>> SearchAllByRole(string roleName)
public async Task<IEnumerable<Person>> SearchAllByRole(string roleName, bool noTracking = true)
{
var usersInRole = await _armoryUsersRepository.SearchAllUsersInRole(roleName);
var userIds = usersInRole.Select(u => u.Id);

if (noTracking)
return await _context.People
.AsNoTracking()
.Where(p => userIds.Contains(p.ArmoryUserId))
.ToListAsync();

return await _context.People
.Where(p => userIds.Contains(p.ArmoryUserId))
.ToListAsync();
Expand Down
4 changes: 2 additions & 2 deletions src/Armory/Ranks/Domain/IRanksRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Armory.Ranks.Domain
public interface IRanksRepository
{
Task Save(Rank rank);
Task<Rank> Find(int id);
Task<IEnumerable<Rank>> SearchAll();
Task<Rank> Find(int id, bool noTracking = true);
Task<IEnumerable<Rank>> SearchAll(bool noTracking = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ public async Task Save(Rank rank)
await _context.SaveChangesAsync();
}

public async Task<Rank> Find(int id)
public async Task<Rank> Find(int id, bool noTracking = true)
{
if (noTracking)
return await _context.Ranks
.AsNoTracking()
.FirstOrDefaultAsync(r => r.Id == id);

return await _context.Ranks.FindAsync(id);
}

public async Task<IEnumerable<Rank>> SearchAll()
public async Task<IEnumerable<Rank>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.Ranks
.AsNoTracking()
.ToListAsync();

return await _context.Ranks.ToListAsync();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Armory/Squadrons/Domain/ISquadronsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Armory.Squadrons.Domain
public interface ISquadronsRepository
{
Task Save(Squadron squadron);
Task<Squadron> Find(string code);
Task<Squadron> Find(string code, bool noTracking = true);
Task<bool> Any(Expression<Func<Squadron, bool>> predicate);
Task<IEnumerable<Squadron>> SearchAll();
Task<IEnumerable<Squadron>> SearchAll(bool noTracking = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ public async Task Save(Squadron squadron)
await _context.SaveChangesAsync();
}

public async Task<Squadron> Find(string code)
public async Task<Squadron> Find(string code, bool noTracking = true)
{
if (noTracking)
return await _context.Squadrons
.AsNoTracking()
.FirstOrDefaultAsync(s => s.Code == code);

return await _context.Squadrons.FindAsync(code);
}

Expand All @@ -33,8 +38,13 @@ public async Task<bool> Any(Expression<Func<Squadron, bool>> predicate)
return await _context.Squadrons.AnyAsync(predicate);
}

public async Task<IEnumerable<Squadron>> SearchAll()
public async Task<IEnumerable<Squadron>> SearchAll(bool noTracking = true)
{
if (noTracking)
return await _context.Squadrons
.AsNoTracking()
.ToListAsync();

return await _context.Squadrons.ToListAsync();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Armory/Squads/Domain/ISquadsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Armory.Squads.Domain
public interface ISquadsRepository
{
Task Save(Squad squad);
Task<Squad> Find(string code);
Task<Squad> Find(string code, bool noTracking = true);
Task<bool> Any(Expression<Func<Squad, bool>> predicate);
Task<IEnumerable<Squad>> SearchAll();
Task<IEnumerable<Squad>> SearchAll(bool noTracking = true);
}
}
Loading

0 comments on commit 9a35585

Please sign in to comment.