Skip to content

Commit

Permalink
Merge pull request #22 from nanotaboada/feature/async-methods
Browse files Browse the repository at this point in the history
refactor: rename async methods in Service layer according to convention
  • Loading branch information
nanotaboada authored Dec 20, 2023
2 parents d25e535 + 498ae20 commit c4d959f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public async Task GivenRetrieve_WhenInvokedTwice_ThenSecondExecutionTimeShouldBe
var service = new PlayerService(context.Object, logger.Object, memoryCache);

// Act
var first = await ExecutionTime(() => service.Retrieve());
var second = await ExecutionTime(() => service.Retrieve());
var first = await ExecutionTimeAsync(() => service.RetrieveAsync());
var second = await ExecutionTimeAsync(() => service.RetrieveAsync());

// Assert
second.Should().BeLessThan(first);
}

public async Task<long> ExecutionTime(Func<Task> awaitable)
private async Task<long> ExecutionTimeAsync(Func<Task> awaitable)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
Expand All @@ -41,4 +41,4 @@ public async Task<long> ExecutionTime(Func<Task> awaitable)

return stopwatch.ElapsedMilliseconds;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<ActionResult<Player>> PostPlayer(Player player)
}
else
{
await _playerService.Create(player);
await _playerService.CreateAsync(player);

return CreatedAtAction(nameof(GetPlayer), new { id = player.Id }, player);
}
Expand All @@ -59,7 +59,7 @@ HTTP GET
[HttpGet]
public async Task<ActionResult<IEnumerable<Player>>> GetPlayers()
{
var players = await _playerService.Retrieve();
var players = await _playerService.RetrieveAsync();

if (players.Any())
{
Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task<IActionResult> PutPlayer(long id, Player player)
}
else
{
await _playerService.Update(player);
await _playerService.UpdateAsync(player);

return NoContent();
}
Expand All @@ -127,7 +127,7 @@ public async Task<IActionResult> DeletePlayer(long id)
}
else
{
await _playerService.Delete(id);
await _playerService.DeleteAsync(id);

return NoContent();
}
Expand Down
8 changes: 4 additions & 4 deletions Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Dotnet.AspNetCore.Samples.WebApi.Services;

public interface IPlayerService
{
public Task Create(Player player);
public Task CreateAsync(Player player);

public Task<List<Player>> Retrieve();
public Task<List<Player>> RetrieveAsync();

public ValueTask<Player?> RetrieveById(long id);

public Task Update(Player player);
public Task UpdateAsync(Player player);

public Task Delete(long id);
public Task DeleteAsync(long id);
}
23 changes: 9 additions & 14 deletions Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public PlayerService(PlayerContext playerContext, ILogger<PlayerService> logger,
Create
-----------------------------------------------------------------------------------------------
*/
public async Task Create(Player player)
public async Task CreateAsync(Player player)
{
_playerContext.Players.Add(player);
_playerContext.Entry(player).State = EntityState.Added;
await _playerContext.SaveChangesAsync();
_memoryCache.Remove(MemoryCacheKey_Retrieve);
}
Expand All @@ -35,9 +35,9 @@ public async Task Create(Player player)
Retrieve
-----------------------------------------------------------------------------------------------
*/
public Task<List<Player>> Retrieve()
public async Task<List<Player>> RetrieveAsync()
{
if (_memoryCache.TryGetValue(MemoryCacheKey_Retrieve, out Task<List<Player>>? players)
if (_memoryCache.TryGetValue(MemoryCacheKey_Retrieve, out List<Player>? players)
&& players != null)
{
_logger.Log(LogLevel.Information, "Players retrieved from MemoryCache.");
Expand All @@ -47,9 +47,9 @@ public Task<List<Player>> Retrieve()
{
// Introduced on purpose to simulate a real database query with a
// delay of beteen 1 and 2 seconds.
Task.Delay(new Random().Next(1000, 2000));
await Task.Delay(new Random().Next(1000, 2000));

players = _playerContext.Players.ToListAsync();
players = await _playerContext.Players.ToListAsync();
_memoryCache.Set(MemoryCacheKey_Retrieve, players, GetMemoryCacheEntryOptions());

_logger.Log(LogLevel.Information, "Players retrieved from DbContext.");
Expand All @@ -67,7 +67,7 @@ public Task<List<Player>> Retrieve()
Update
-----------------------------------------------------------------------------------------------
*/
public async Task Update(Player player)
public async Task UpdateAsync(Player player)
{
_playerContext.Entry(player).State = EntityState.Modified;

Expand Down Expand Up @@ -96,23 +96,18 @@ public async Task Update(Player player)
Delete
-----------------------------------------------------------------------------------------------
*/
public async Task Delete(long id)
public async Task DeleteAsync(long id)
{
var player = await _playerContext.Players.FindAsync(id);

if (player != null)
{
_playerContext.Players.Remove(player);
_playerContext.Entry(player).State = EntityState.Deleted;
await _playerContext.SaveChangesAsync();
_memoryCache.Remove(MemoryCacheKey_Retrieve);
}
}

private bool PlayerExists(long id)
{
return (_playerContext.Players?.Any(e => e.Id == id)).GetValueOrDefault();
}

private MemoryCacheEntryOptions GetMemoryCacheEntryOptions()
{
return new MemoryCacheEntryOptions()
Expand Down

0 comments on commit c4d959f

Please sign in to comment.