Class CharacterBuffService
Character buff service with async operations, atomic SQL, and DTO pattern. Uses repository pattern with EF Core and raw SQL for race-condition-prone operations. Implements execution strategies for automatic retry on transient database failures. Returns DatabaseResult for consistent, safe error handling with sanitized messages. Follows SOLID principles: SRP, OCP, LSP, ISP, DIP.
public sealed class CharacterBuffService : BaseService<CharacterBuffEntity>, ICharacterBuffService, IDeleteByKeyVersionedAction<long>, IFetchCollectionByKeyAction<long, CharacterBuffData>
- Inheritance
-
CharacterBuffService
- Implements
- Inherited Members
Remarks
All methods that use ExecuteSqlRawAsync are wrapped in execution strategies to provide automatic retry logic (up to 3 attempts) for transient database failures such as connection timeouts, deadlocks, or network interruptions.
Exception Handling Strategy:
- Catches specific exceptions (NpgsqlException, DbUpdateException, TimeoutException)
- Converts to custom DatabaseException hierarchy with sanitized messages
- Returns DatabaseResult for safe, typed error handling
- Preserves detailed error information for logging while exposing safe messages to clients
Writes happen only through ReplaceSetsAsync(NpgsqlDbContext, BuffSetRows, CancellationToken), which CharacterService runs inside its own character-row transaction; see ICharacterBuffService.
Constructors
CharacterBuffService(INpgsqlDbContextFactory)
Initializes a new instance of the CharacterBuffService class.
public CharacterBuffService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactoryFactory for creating database contexts.
Exceptions
- ArgumentNullException
Thrown when dbContextFactory is null.
Methods
DeleteAsync(long, long, CancellationToken)
Deletes the entity identified by the given key if incomingVersion is newer.
public Task<DatabaseResult> DeleteAsync(long characterId, long incomingVersion, CancellationToken cancellationToken = default)
Parameters
characterIdlongincomingVersionlongThe authoritative, monotonic version for this delete operation.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
FetchAsync(long, CancellationToken)
Fetches a collection of items for the given key.
public Task<DatabaseResult<IReadOnlyList<CharacterBuffData>>> FetchAsync(long characterId, CancellationToken cancellationToken = default)
Parameters
characterIdlongcancellationTokenCancellationTokenToken to cancel the operation.
Returns
FlattenSets(IReadOnlyList<(long CharacterID, long Version, IReadOnlyList<CharacterBuffData> Buffs)>)
Flattens buff sets into the arrays ReplaceSetsAsync(NpgsqlDbContext, BuffSetRows, CancellationToken) binds. Pure.
public static CharacterBuffService.BuffSetRows FlattenSets(IReadOnlyList<(long CharacterID, long Version, IReadOnlyList<CharacterBuffData> Buffs)> sets)
Parameters
setsIReadOnlyList<(long CharacterID, long Version, IReadOnlyList<CharacterBuffData> Buffs)>The sets to write: the character, its snapshot version, and its complete set of buffs (never null; empty means none).
Returns
- CharacterBuffService.BuffSetRows
The column arrays, or null when
setsnames nobody.
Remarks
The set decides whose rows they are, and which version they carry. Every row is written under its set's character and its set's version, whatever the DTO says: a set is written only after its character row has passed that row's version and ownership checks in the same transaction, so the row's character and version are the ones proven, and a row naming anyone else must not ride on that proof.
A template named twice in one set keeps its first occurrence. The capture reads a
dictionary keyed by template, so a duplicate is a caller defect — and an
INSERT ... ON CONFLICT that meets one key twice fails the whole statement, which
would take the character row down with it. A character named by two sets keeps the one
with the higher version, for the reason PersistManyAsync keeps the newer of two
snapshots.