Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions ManagedCode.Repository.AzureTable/AzureTableAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
using Humanizer;
using ManagedCode.Repository.Core;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;

namespace ManagedCode.Repository.AzureTable
{
public class AzureTableAdapter<T> where T : class, ITableEntity, new()
{
private readonly ILogger _logger;
private readonly bool _allowTableCreation = true;
private readonly CloudStorageAccount _cloudStorageAccount;
private readonly object _sync = new();
private CloudTableClient _cloudTableClient;
private bool _tableClientInitialized;

public AzureTableAdapter(string connectionString)
public AzureTableAdapter(ILogger logger, string connectionString)
{
_logger = logger;
_cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
}

public AzureTableAdapter(StorageCredentials tableStorageCredentials, StorageUri tableStorageUri)
public AzureTableAdapter(ILogger logger, StorageCredentials tableStorageCredentials, StorageUri tableStorageUri)
{
_logger = logger;
var cloudStorageAccount = new CloudStorageAccount(tableStorageCredentials, tableStorageUri);
_cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
}
Expand Down
13 changes: 5 additions & 8 deletions ManagedCode.Repository.AzureTable/BaseAzureTableRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ public class BaseAzureTableRepository<TId, TItem> : BaseRepository<TId, TItem>
where TId : TableId
where TItem : class, IItem<TId>, ITableEntity, new()
{
private readonly ILogger _logger;
private readonly AzureTableAdapter<TItem> _tableAdapter;

public BaseAzureTableRepository(ILogger logger, AzureTableRepositoryOptions options)
public BaseAzureTableRepository(ILogger logger, AzureTableRepositoryOptions options) : base(logger)
{
_logger = logger;

if (string.IsNullOrEmpty(options.ConnectionString))
{
_tableAdapter = new AzureTableAdapter<TItem>(options.TableStorageCredentials, options.TableStorageUri);
_tableAdapter = new AzureTableAdapter<TItem>(logger, options.TableStorageCredentials, options.TableStorageUri);
}
else
{
_tableAdapter = new AzureTableAdapter<TItem>(options.ConnectionString);
_tableAdapter = new AzureTableAdapter<TItem>(logger, options.ConnectionString);
}
}

Expand Down Expand Up @@ -314,12 +311,12 @@ protected override async Task<int> CountAsyncInternal(CancellationToken token =
return count;
}

protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
{
var count = 0;

await foreach (var item in _tableAdapter
.Query<DynamicTableEntity>(new[] {predicate}, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
.Query<DynamicTableEntity>(predicates, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
cancellationToken: token))
{
count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>ManagedCode</Authors>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.5</PackageVersion>
<PackageVersion>1.0.7</PackageVersion>
<Description>Repository for AzureTable</Description>
</PropertyGroup>

Expand Down
19 changes: 17 additions & 2 deletions ManagedCode.Repository.Core/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace ManagedCode.Repository.Core
{
public abstract class BaseRepository<TId, TItem> : IRepository<TId, TItem> where TItem : IItem<TId>
{
protected readonly ILogger Logger;

protected BaseRepository(ILogger logger)
{
Logger = logger;
}

public bool IsInitialized { get; protected set; }

public Task InitializeAsync(CancellationToken token = default)
Expand Down Expand Up @@ -385,12 +393,19 @@ public Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, Cancellatio
{
Contract.Requires(IsInitialized);
Contract.Requires(predicate != null);
return CountAsyncInternal(predicate, token);
return CountAsyncInternal(new []{predicate}, token);
}

public Task<int> CountAsync(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
{
Contract.Requires(IsInitialized);
Contract.Requires(predicates != null);
return CountAsyncInternal(predicates, token);
}

protected abstract Task<int> CountAsyncInternal(CancellationToken token = default);

protected abstract Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
protected abstract Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default);

#endregion
}
Expand Down
1 change: 1 addition & 0 deletions ManagedCode.Repository.Core/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,

Task<int> CountAsync(CancellationToken token = default);
Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
Task<int> CountAsync(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default);
}
}
25 changes: 23 additions & 2 deletions ManagedCode.Repository.Core/InMemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace ManagedCode.Repository.Core
{
public class InMemoryRepository<TId, TItem> : BaseRepository<TId, TItem> where TItem : IItem<TId>
{
private readonly Dictionary<TId, TItem> _storage = new();

public InMemoryRepository(ILogger logger) : base(logger)
{
}

protected override Task InitializeAsyncInternal(CancellationToken token = default)
{
IsInitialized = true;
Expand Down Expand Up @@ -439,15 +444,31 @@ protected override Task<int> CountAsyncInternal(CancellationToken token = defaul
}
}

protected override Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
protected override Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
{
lock (_storage)
{
var count = _storage.Values.Count(predicate.Compile());
IEnumerable<TItem> items = null;

foreach (var predicate in predicates)
{
if (items == null)
{
items = _storage.Values.Where(predicate.Compile());
}
else
{
items = items.Where(predicate.Compile());
}
}

var count = items.Count();
return Task.FromResult(count);
}
}

#endregion


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/managed-code-hub/Repository</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.6</PackageVersion>
<PackageVersion>1.0.7</PackageVersion>
<Description>Base implementation for Repository</Description>
</PropertyGroup>

Expand Down
16 changes: 11 additions & 5 deletions ManagedCode.Repository.CosmosDB/CosmosDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ public class CosmosDbRepository<TItem> : BaseRepository<string, TItem>, ICosmosD
where TItem : CosmosDbItem, IItem<string>, new()
{
private readonly CosmosDbAdapter<TItem> _cosmosDbAdapter;
private readonly ILogger _logger;
private readonly bool _splitByType;

public CosmosDbRepository(ILogger logger, [NotNull] CosmosDbRepositoryOptions options)
public CosmosDbRepository(ILogger logger, [NotNull] CosmosDbRepositoryOptions options) : base(logger)
{
_logger = logger;
_splitByType = options.SplitByType;
_cosmosDbAdapter = new CosmosDbAdapter<TItem>(options.ConnectionString, options.CosmosClientOptions, options.DatabaseName, options.CollectionName);
}
Expand Down Expand Up @@ -690,10 +688,18 @@ protected override async Task<int> CountAsyncInternal(CancellationToken token =
return await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).CountAsync(token);
}

protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
{
var container = await _cosmosDbAdapter.GetContainer();
return await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).Where(predicate).CountAsync(token);
var query = container.GetItemLinqQueryable<TItem>().Where(SplitByType());

foreach (var predicate in predicates)
{
query = query.Where(predicate);
}


return await query.CountAsync(token);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>ManagedCode</Authors>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.6</PackageVersion>
<PackageVersion>1.0.7</PackageVersion>
<Description>Repository for CosmosDB</Description>
</PropertyGroup>

Expand Down
14 changes: 9 additions & 5 deletions ManagedCode.Repository.LiteDB/LiteDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ namespace ManagedCode.Repository.LiteDB
public class LiteDbRepository<TId, TItem> : BaseRepository<TId, TItem> where TItem : class, IItem<TId>
{
private readonly LiteDatabase _database;
private readonly ILogger _logger;

public LiteDbRepository(ILogger logger, [NotNull] LiteDbRepositoryOptions options)
public LiteDbRepository(ILogger logger, [NotNull] LiteDbRepositoryOptions options) : base(logger)
{
_logger = logger;
if (options.Database != null)
{
_database = options.Database;
Expand Down Expand Up @@ -388,10 +386,16 @@ protected override async Task<int> CountAsyncInternal(CancellationToken token =
return GetDatabase().Count();
}

protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
{
await Task.Yield();
return GetDatabase().Count(predicate);
var query = GetDatabase().Query();

foreach (var predicate in predicates)
{
query = query.Where(predicate);
}
return query.Count();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Authors>ManagedCode</Authors>
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
<PackageVersion>1.0.6</PackageVersion>
<PackageVersion>1.0.7</PackageVersion>
<Description>Repository for LiteDB</Description>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions ManagedCode.Repository.Tests/InMemoryRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ManagedCode.Repository.Tests
{
public class InMemoryRepositoryTests
{
private readonly IRepository<int, InMemoryItem> _repository = new InMemoryRepository<int, InMemoryItem>();
private readonly IRepository<int, InMemoryItem> _repository = new InMemoryRepository<int, InMemoryItem>(null);

public InMemoryRepositoryTests()
{
Expand All @@ -30,7 +30,7 @@ public async Task InitializeAsync()
[Fact]
public async Task NotInitializedAsync()
{
IRepository<int, InMemoryItem> localRepository = new InMemoryRepository<int, InMemoryItem>();
IRepository<int, InMemoryItem> localRepository = new InMemoryRepository<int, InMemoryItem>(null);

localRepository.IsInitialized.Should().BeFalse();

Expand Down