Skip to content

Commit

Permalink
Fixed issues with ApplicationDbContext when storing hits
Browse files Browse the repository at this point in the history
Closes #292
  • Loading branch information
openbullet committed Jul 2, 2021
1 parent 6dee75f commit 8fe95b9
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions OpenBullet2/Repositories/DbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenBullet2.Repositories
public class DbRepository<T> : IRepository<T> where T : Entity
{
protected readonly ApplicationDbContext context;
private readonly object dbLock = new object();
private readonly SemaphoreSlim semaphore = new(1, 1);

public DbRepository(ApplicationDbContext context)
{
Expand All @@ -20,29 +20,27 @@ public DbRepository(ApplicationDbContext context)
/// <summary>
/// Adds an entity and saves changes.
/// </summary>
public virtual async Task Add(T entity)
public async virtual Task Add(T entity)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);

await semaphore.WaitAsync();

try
{
context.Add(entity);
await context.SaveChangesAsync();
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}

/// <summary>
/// Adds multiple entities and saves changes.
/// </summary>
public virtual async Task Add(IEnumerable<T> entities)
public async virtual Task Add(IEnumerable<T> entities)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);
await semaphore.WaitAsync();

try
{
Expand All @@ -51,17 +49,16 @@ public virtual async Task Add(IEnumerable<T> entities)
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}

/// <summary>
/// Deletes an entity and saves changes.
/// </summary>
public virtual async Task Delete(T entity)
public async virtual Task Delete(T entity)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);
await semaphore.WaitAsync();

try
{
Expand All @@ -70,17 +67,16 @@ public virtual async Task Delete(T entity)
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}

/// <summary>
/// Deletes multiple entities and saves changes.
/// </summary>
public virtual async Task Delete(IEnumerable<T> entities)
public async virtual Task Delete(IEnumerable<T> entities)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);
await semaphore.WaitAsync();

try
{
Expand All @@ -89,25 +85,24 @@ public virtual async Task Delete(IEnumerable<T> entities)
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}

/// <summary>
/// Gets the entry with the specified id or null if not found.
/// </summary>
public virtual async Task<T> Get(int id)
public async virtual Task<T> Get(int id)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);
await semaphore.WaitAsync();

try
{
return await GetAll().FirstOrDefaultAsync(e => e.Id == id);
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}

Expand All @@ -120,10 +115,9 @@ public virtual IQueryable<T> GetAll()
/// <summary>
/// Updates the given entity and saves changes.
/// </summary>
public virtual async Task Update(T entity)
public async virtual Task Update(T entity)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);
await semaphore.WaitAsync();

try
{
Expand All @@ -132,17 +126,16 @@ public virtual async Task Update(T entity)
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}

/// <summary>
/// Updates multiple entities and saves changes.
/// </summary>
public virtual async Task Update(IEnumerable<T> entities)
public async virtual Task Update(IEnumerable<T> entities)
{
while (!Monitor.TryEnter(dbLock))
await Task.Delay(10);
await semaphore.WaitAsync();

try
{
Expand All @@ -151,7 +144,7 @@ public virtual async Task Update(IEnumerable<T> entities)
}
finally
{
Monitor.Exit(dbLock);
semaphore.Release();
}
}
}
Expand Down

0 comments on commit 8fe95b9

Please sign in to comment.