refactor: Exception and init of dictionary

This commit is contained in:
Timothy Schenk 2023-11-16 06:47:28 +01:00
parent 76991338f3
commit 3602d74eb4

View file

@ -10,7 +10,7 @@ namespace Server.Services;
public class ItemObjectPoolService : IHostedService public class ItemObjectPoolService : IHostedService
{ {
readonly ConcurrentDictionary<uint, ItemObject> _itemObjectPool = new(); readonly ConcurrentDictionary<uint, ItemObject> _itemObjectPool;
private readonly ItemReader _itemReader; private readonly ItemReader _itemReader;
private readonly ILogger<ItemObjectPoolService> _logger; private readonly ILogger<ItemObjectPoolService> _logger;
@ -19,6 +19,8 @@ public class ItemObjectPoolService : IHostedService
_logger = logger; _logger = logger;
_itemReader = new ItemReader(configuration.GetSection("Game").GetSection("Data").GetValue<string>("Path") ?? _itemReader = new ItemReader(configuration.GetSection("Game").GetSection("Data").GetValue<string>("Path") ??
string.Empty); string.Empty);
_itemObjectPool = new ConcurrentDictionary<uint, ItemObject>();
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
@ -30,7 +32,7 @@ public class ItemObjectPoolService : IHostedService
var result = _itemObjectPool.TryAdd(itemObject.ItemID, itemObject); var result = _itemObjectPool.TryAdd(itemObject.ItemID, itemObject);
if (!result) if (!result)
{ {
throw new Exception($"Failed to add item {itemObject.ItemID} to the item object pool"); throw new KeyNotFoundException($"Failed to add item {itemObject.ItemID} to the item object pool");
} }
_logger.LogTrace("Item with {ID} has been added", itemObject.ItemID); _logger.LogTrace("Item with {ID} has been added", itemObject.ItemID);
@ -43,7 +45,6 @@ public class ItemObjectPoolService : IHostedService
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }