Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

(#727) Make Collection a readonly property. #728

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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