Skip to content

Commit

Permalink
(#727) Make Collection a readonly property. (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall committed Aug 28, 2023
1 parent d323b63 commit ffe17c1
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class LiteDbRepository<TEntity> : IRepository<TEntity> where TEntity : Li
{
private readonly object writeLock = new();
private readonly LiteDatabase connection;
private readonly ILiteCollection<TEntity> collection;

/// <summary>
/// Creates a new <see cref="LiteDbRepository{TEntity}"/>, using the provided database
Expand All @@ -31,10 +30,12 @@ public LiteDbRepository(LiteDatabase databaseConnection, string collectionName =
collectionName ??= typeof(TEntity).Name.ToLowerInvariant() + 's';

connection = databaseConnection ?? throw new ArgumentNullException(nameof(databaseConnection));
collection = connection.GetCollection<TEntity>(collectionName);
collection.EnsureIndex(x => x.UpdatedAt);
Collection = connection.GetCollection<TEntity>(collectionName);
Collection.EnsureIndex(x => x.UpdatedAt);
}

public ILiteCollection<TEntity> Collection { get; }

/// <summary>
/// Updates the system properties for the provided entity on write.
/// </summary>
Expand All @@ -55,7 +56,7 @@ private static void UpdateEntity(TEntity entity)
/// <param name="token">A cancellation token</param>
/// <returns>An <see cref="IQueryable{T}"/> for the entities in the data store</returns>
public IQueryable<TEntity> AsQueryable()
=> collection.FindAll().AsQueryable();
=> Collection.FindAll().AsQueryable();

/// <summary>
/// Create a new entity within the backend data store. If the entity does not
Expand Down Expand Up @@ -106,7 +107,7 @@ public Task<TEntity> ReadAsync(string id, CancellationToken token = default)
throw new BadRequestException();
}

TEntity entity = collection.FindById(id);
TEntity entity = Collection.FindById(id);
return Task.FromResult(entity);
}

Expand Down Expand Up @@ -150,13 +151,13 @@ internal void CreateEntity(TEntity entity)

lock (writeLock)
{
TEntity existingEntity = collection.FindById(entity.Id);
TEntity existingEntity = Collection.FindById(entity.Id);
if (existingEntity != null)
{
throw new ConflictException(existingEntity);
}
UpdateEntity(entity);
collection.Insert(entity);
Collection.Insert(entity);
}
}

Expand All @@ -177,7 +178,7 @@ internal void DeleteEntity(string id, byte[] version = null)

lock (writeLock)
{
TEntity existingEntity = collection.FindById(id);
TEntity existingEntity = Collection.FindById(id);
if (existingEntity == null)
{
throw new NotFoundException();
Expand All @@ -187,7 +188,7 @@ internal void DeleteEntity(string id, byte[] version = null)
{
throw new PreconditionFailedException(existingEntity);
}
collection.Delete(id);
Collection.Delete(id);
}
}

Expand All @@ -213,7 +214,7 @@ internal void ReplaceEntity(TEntity entity, byte[] version = null)

lock (writeLock)
{
TEntity existingEntity = collection.FindById(entity.Id);
TEntity existingEntity = Collection.FindById(entity.Id);
if (existingEntity == null)
{
throw new NotFoundException();
Expand All @@ -224,7 +225,7 @@ internal void ReplaceEntity(TEntity entity, byte[] version = null)
throw new PreconditionFailedException(existingEntity);
}
UpdateEntity(entity);
collection.Update(entity);
Collection.Update(entity);
}
}
#endregion
Expand Down

0 comments on commit ffe17c1

Please sign in to comment.