Skip to content

Commit c66d77e

Browse files
authored
Merge pull request #31 from KSemenenko/main
refactoring
2 parents dbf664b + aa6691b commit c66d77e

File tree

12 files changed

+78
-30
lines changed

12 files changed

+78
-30
lines changed

ManagedCode.Repository.AzureTable/AzureTableAdapter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@
88
using Humanizer;
99
using ManagedCode.Repository.Core;
1010
using Microsoft.Azure.Cosmos.Table;
11+
using Microsoft.Extensions.Logging;
1112

1213
namespace ManagedCode.Repository.AzureTable
1314
{
1415
public class AzureTableAdapter<T> where T : class, ITableEntity, new()
1516
{
17+
private readonly ILogger _logger;
1618
private readonly bool _allowTableCreation = true;
1719
private readonly CloudStorageAccount _cloudStorageAccount;
1820
private readonly object _sync = new();
1921
private CloudTableClient _cloudTableClient;
2022
private bool _tableClientInitialized;
2123

22-
public AzureTableAdapter(string connectionString)
24+
public AzureTableAdapter(ILogger logger, string connectionString)
2325
{
26+
_logger = logger;
2427
_cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
2528
}
2629

27-
public AzureTableAdapter(StorageCredentials tableStorageCredentials, StorageUri tableStorageUri)
30+
public AzureTableAdapter(ILogger logger, StorageCredentials tableStorageCredentials, StorageUri tableStorageUri)
2831
{
32+
_logger = logger;
2933
var cloudStorageAccount = new CloudStorageAccount(tableStorageCredentials, tableStorageUri);
3034
_cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
3135
}

ManagedCode.Repository.AzureTable/BaseAzureTableRepository.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@ public class BaseAzureTableRepository<TId, TItem> : BaseRepository<TId, TItem>
1414
where TId : TableId
1515
where TItem : class, IItem<TId>, ITableEntity, new()
1616
{
17-
private readonly ILogger _logger;
1817
private readonly AzureTableAdapter<TItem> _tableAdapter;
1918

20-
public BaseAzureTableRepository(ILogger logger, AzureTableRepositoryOptions options)
19+
public BaseAzureTableRepository(ILogger logger, AzureTableRepositoryOptions options) : base(logger)
2120
{
22-
_logger = logger;
23-
2421
if (string.IsNullOrEmpty(options.ConnectionString))
2522
{
26-
_tableAdapter = new AzureTableAdapter<TItem>(options.TableStorageCredentials, options.TableStorageUri);
23+
_tableAdapter = new AzureTableAdapter<TItem>(logger, options.TableStorageCredentials, options.TableStorageUri);
2724
}
2825
else
2926
{
30-
_tableAdapter = new AzureTableAdapter<TItem>(options.ConnectionString);
27+
_tableAdapter = new AzureTableAdapter<TItem>(logger, options.ConnectionString);
3128
}
3229
}
3330

@@ -314,12 +311,12 @@ protected override async Task<int> CountAsyncInternal(CancellationToken token =
314311
return count;
315312
}
316313

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

321318
await foreach (var item in _tableAdapter
322-
.Query<DynamicTableEntity>(new[] {predicate}, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
319+
.Query<DynamicTableEntity>(predicates, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
323320
cancellationToken: token))
324321
{
325322
count++;

ManagedCode.Repository.AzureTable/ManagedCode.Repository.AzureTable.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Authors>ManagedCode</Authors>
99
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
1010
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
11-
<PackageVersion>1.0.5</PackageVersion>
11+
<PackageVersion>1.0.7</PackageVersion>
1212
<Description>Repository for AzureTable</Description>
1313
</PropertyGroup>
1414

ManagedCode.Repository.Core/BaseRepository.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
using System.Linq.Expressions;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using Microsoft.Extensions.Logging;
78

89
namespace ManagedCode.Repository.Core
910
{
1011
public abstract class BaseRepository<TId, TItem> : IRepository<TId, TItem> where TItem : IItem<TId>
1112
{
13+
protected readonly ILogger Logger;
14+
15+
protected BaseRepository(ILogger logger)
16+
{
17+
Logger = logger;
18+
}
19+
1220
public bool IsInitialized { get; protected set; }
1321

1422
public Task InitializeAsync(CancellationToken token = default)
@@ -385,12 +393,19 @@ public Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, Cancellatio
385393
{
386394
Contract.Requires(IsInitialized);
387395
Contract.Requires(predicate != null);
388-
return CountAsyncInternal(predicate, token);
396+
return CountAsyncInternal(new []{predicate}, token);
397+
}
398+
399+
public Task<int> CountAsync(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
400+
{
401+
Contract.Requires(IsInitialized);
402+
Contract.Requires(predicates != null);
403+
return CountAsyncInternal(predicates, token);
389404
}
390405

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

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

395410
#endregion
396411
}

ManagedCode.Repository.Core/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,6 @@ IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
125125

126126
Task<int> CountAsync(CancellationToken token = default);
127127
Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
128+
Task<int> CountAsync(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default);
128129
}
129130
}

ManagedCode.Repository.Core/InMemoryRepository.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
using System.Runtime.CompilerServices;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Logging;
89

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

16+
public InMemoryRepository(ILogger logger) : base(logger)
17+
{
18+
}
19+
1520
protected override Task InitializeAsyncInternal(CancellationToken token = default)
1621
{
1722
IsInitialized = true;
@@ -439,15 +444,31 @@ protected override Task<int> CountAsyncInternal(CancellationToken token = defaul
439444
}
440445
}
441446

442-
protected override Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
447+
protected override Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
443448
{
444449
lock (_storage)
445450
{
446-
var count = _storage.Values.Count(predicate.Compile());
451+
IEnumerable<TItem> items = null;
452+
453+
foreach (var predicate in predicates)
454+
{
455+
if (items == null)
456+
{
457+
items = _storage.Values.Where(predicate.Compile());
458+
}
459+
else
460+
{
461+
items = items.Where(predicate.Compile());
462+
}
463+
}
464+
465+
var count = items.Count();
447466
return Task.FromResult(count);
448467
}
449468
}
450469

451470
#endregion
471+
472+
452473
}
453474
}

ManagedCode.Repository.Core/ManagedCode.Repository.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageProjectUrl>https://github.com/managed-code-hub/Repository</PackageProjectUrl>
1010
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
1111
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
12-
<PackageVersion>1.0.6</PackageVersion>
12+
<PackageVersion>1.0.7</PackageVersion>
1313
<Description>Base implementation for Repository</Description>
1414
</PropertyGroup>
1515

ManagedCode.Repository.CosmosDB/CosmosDbRepository.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ public class CosmosDbRepository<TItem> : BaseRepository<string, TItem>, ICosmosD
1616
where TItem : CosmosDbItem, IItem<string>, new()
1717
{
1818
private readonly CosmosDbAdapter<TItem> _cosmosDbAdapter;
19-
private readonly ILogger _logger;
2019
private readonly bool _splitByType;
2120

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

693-
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
691+
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
694692
{
695693
var container = await _cosmosDbAdapter.GetContainer();
696-
return await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).Where(predicate).CountAsync(token);
694+
var query = container.GetItemLinqQueryable<TItem>().Where(SplitByType());
695+
696+
foreach (var predicate in predicates)
697+
{
698+
query = query.Where(predicate);
699+
}
700+
701+
702+
return await query.CountAsync(token);
697703
}
698704

699705
#endregion

ManagedCode.Repository.CosmosDB/ManagedCode.Repository.CosmosDB.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Authors>ManagedCode</Authors>
99
<PackageLicenseUrl>https://github.com/managed-code-hub/Repository/blob/main/LICENSE</PackageLicenseUrl>
1010
<RepositoryUrl>https://github.com/managed-code-hub/Repository</RepositoryUrl>
11-
<PackageVersion>1.0.6</PackageVersion>
11+
<PackageVersion>1.0.7</PackageVersion>
1212
<Description>Repository for CosmosDB</Description>
1313
</PropertyGroup>
1414

ManagedCode.Repository.LiteDB/LiteDbRepository.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ namespace ManagedCode.Repository.LiteDB
1414
public class LiteDbRepository<TId, TItem> : BaseRepository<TId, TItem> where TItem : class, IItem<TId>
1515
{
1616
private readonly LiteDatabase _database;
17-
private readonly ILogger _logger;
1817

19-
public LiteDbRepository(ILogger logger, [NotNull] LiteDbRepositoryOptions options)
18+
public LiteDbRepository(ILogger logger, [NotNull] LiteDbRepositoryOptions options) : base(logger)
2019
{
21-
_logger = logger;
2220
if (options.Database != null)
2321
{
2422
_database = options.Database;
@@ -388,10 +386,16 @@ protected override async Task<int> CountAsyncInternal(CancellationToken token =
388386
return GetDatabase().Count();
389387
}
390388

391-
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
389+
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>>[] predicates, CancellationToken token = default)
392390
{
393391
await Task.Yield();
394-
return GetDatabase().Count(predicate);
392+
var query = GetDatabase().Query();
393+
394+
foreach (var predicate in predicates)
395+
{
396+
query = query.Where(predicate);
397+
}
398+
return query.Count();
395399
}
396400

397401
#endregion

0 commit comments

Comments
 (0)