Skip to content

Commit a09ecfb

Browse files
authored
Merge pull request #29 from KSemenenko/main
refactoring
2 parents 6f0fc22 + 05bcdb3 commit a09ecfb

14 files changed

+89
-79
lines changed

ManagedCode.Repository.AzureTable/BaseAzureTableRepository.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ protected override async Task<int> InsertAsyncInternal(IEnumerable<TItem> items,
6868

6969
#region InsertOrUpdate
7070

71-
protected override async Task<bool> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
71+
protected override async Task<TItem> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
7272
{
7373
try
7474
{
75-
var result = await _tableAdapter.ExecuteAsync(TableOperation.InsertOrReplace(item), token);
76-
return result != null;
75+
var result = await _tableAdapter.ExecuteAsync<TItem>(TableOperation.InsertOrReplace(item), token);
76+
return result;
7777
}
7878
catch (Exception e)
7979
{
80-
return false;
80+
return default;
8181
}
8282
}
8383

@@ -97,7 +97,7 @@ protected override async Task<int> InsertOrUpdateAsyncInternal(IEnumerable<TItem
9797

9898
#region Update
9999

100-
protected override async Task<bool> UpdateAsyncInternal(TItem item, CancellationToken token = default)
100+
protected override async Task<TItem> UpdateAsyncInternal(TItem item, CancellationToken token = default)
101101
{
102102
try
103103
{
@@ -106,12 +106,12 @@ protected override async Task<bool> UpdateAsyncInternal(TItem item, Cancellation
106106
item.ETag = "*";
107107
}
108108

109-
var result = await _tableAdapter.ExecuteAsync(TableOperation.Replace(item), token);
110-
return result != null;
109+
var result = await _tableAdapter.ExecuteAsync<TItem>(TableOperation.Replace(item), token);
110+
return result;
111111
}
112112
catch (Exception e)
113113
{
114-
return false;
114+
return default;
115115
}
116116
}
117117

@@ -294,9 +294,9 @@ protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TIt
294294

295295
#region Count
296296

297-
protected override async Task<uint> CountAsyncInternal(CancellationToken token = default)
297+
protected override async Task<int> CountAsyncInternal(CancellationToken token = default)
298298
{
299-
uint count = 0;
299+
int count = 0;
300300

301301
Expression<Func<TItem, bool>> predicate = item => true;
302302

@@ -310,9 +310,9 @@ protected override async Task<uint> CountAsyncInternal(CancellationToken token =
310310
return count;
311311
}
312312

313-
protected override async Task<uint> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
313+
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
314314
{
315-
uint count = 0;
315+
int count = 0;
316316

317317
await foreach (var item in _tableAdapter
318318
.Query<DynamicTableEntity>(predicate, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),

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.4</PackageVersion>
11+
<PackageVersion>1.0.5</PackageVersion>
1212
<Description>Repository for AzureTable</Description>
1313
</PropertyGroup>
1414

ManagedCode.Repository.Core/BaseRepository.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Task<int> InsertAsync(IEnumerable<TItem> items, CancellationToken token =
4747

4848
#region Update
4949

50-
public Task<bool> UpdateAsync(TItem item, CancellationToken token = default)
50+
public Task<TItem> UpdateAsync(TItem item, CancellationToken token = default)
5151
{
5252
Contract.Requires(IsInitialized);
5353
Contract.Requires(item != null);
@@ -61,7 +61,7 @@ public Task<int> UpdateAsync(IEnumerable<TItem> items, CancellationToken token =
6161
return UpdateAsyncInternal(items, token);
6262
}
6363

64-
protected abstract Task<bool> UpdateAsyncInternal(TItem items, CancellationToken token = default);
64+
protected abstract Task<TItem> UpdateAsyncInternal(TItem items, CancellationToken token = default);
6565

6666
protected abstract Task<int> UpdateAsyncInternal(IEnumerable<TItem> items, CancellationToken token = default);
6767

@@ -126,7 +126,7 @@ public Task<bool> DeleteAllAsync(CancellationToken token = default)
126126

127127
#region InsertOrUpdate
128128

129-
public Task<bool> InsertOrUpdateAsync(TItem item, CancellationToken token = default)
129+
public Task<TItem> InsertOrUpdateAsync(TItem item, CancellationToken token = default)
130130
{
131131
Contract.Requires(IsInitialized);
132132
Contract.Requires(item != null);
@@ -140,7 +140,7 @@ public Task<int> InsertOrUpdateAsync(IEnumerable<TItem> items, CancellationToken
140140
return InsertOrUpdateAsyncInternal(items, token);
141141
}
142142

143-
protected abstract Task<bool> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default);
143+
protected abstract Task<TItem> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default);
144144

145145
protected abstract Task<int> InsertOrUpdateAsyncInternal(IEnumerable<TItem> items, CancellationToken token = default);
146146

@@ -297,22 +297,22 @@ protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TIt
297297

298298
#region Count
299299

300-
public Task<uint> CountAsync(CancellationToken token = default)
300+
public Task<int> CountAsync(CancellationToken token = default)
301301
{
302302
Contract.Requires(IsInitialized);
303303
return CountAsyncInternal(token);
304304
}
305305

306-
public Task<uint> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
306+
public Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
307307
{
308308
Contract.Requires(IsInitialized);
309309
Contract.Requires(predicate != null);
310310
return CountAsyncInternal(predicate, token);
311311
}
312312

313-
protected abstract Task<uint> CountAsyncInternal(CancellationToken token = default);
313+
protected abstract Task<int> CountAsyncInternal(CancellationToken token = default);
314314

315-
protected abstract Task<uint> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
315+
protected abstract Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
316316

317317
#endregion
318318
}

ManagedCode.Repository.Core/IRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public interface IRepository<in TId, TItem> where TItem : IItem<TId>
1515
Task<TItem> InsertAsync(TItem item, CancellationToken token = default);
1616
Task<int> InsertAsync(IEnumerable<TItem> items, CancellationToken token = default);
1717

18-
Task<bool> UpdateAsync(TItem item, CancellationToken token = default);
18+
Task<TItem> UpdateAsync(TItem item, CancellationToken token = default);
1919
Task<int> UpdateAsync(IEnumerable<TItem> items, CancellationToken token = default);
2020

21-
Task<bool> InsertOrUpdateAsync(TItem item, CancellationToken token = default);
21+
Task<TItem> InsertOrUpdateAsync(TItem item, CancellationToken token = default);
2222
Task<int> InsertOrUpdateAsync(IEnumerable<TItem> items, CancellationToken token = default);
2323

2424
Task<bool> DeleteAsync(TId id, CancellationToken token = default);
@@ -86,7 +86,7 @@ IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
8686
int skip = 0,
8787
CancellationToken token = default);
8888

89-
Task<uint> CountAsync(CancellationToken token = default);
90-
Task<uint> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
89+
Task<int> CountAsync(CancellationToken token = default);
90+
Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
9191
}
9292
}

ManagedCode.Repository.Core/InMemoryRepository.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ protected override Task<int> InsertAsyncInternal(IEnumerable<TItem> items, Cance
5757

5858
#region InsertOrUpdate
5959

60-
protected override Task<bool> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
60+
protected override Task<TItem> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
6161
{
6262
lock (_storage)
6363
{
6464
_storage[item.Id] = item;
65-
return Task.FromResult(true);
65+
return Task.FromResult(item);
6666
}
6767
}
6868

@@ -86,17 +86,17 @@ protected override Task<int> InsertOrUpdateAsyncInternal(IEnumerable<TItem> item
8686

8787
#region Update
8888

89-
protected override Task<bool> UpdateAsyncInternal(TItem item, CancellationToken token = default)
89+
protected override Task<TItem> UpdateAsyncInternal(TItem item, CancellationToken token = default)
9090
{
9191
lock (_storage)
9292
{
9393
if (_storage.TryGetValue(item.Id, out var _))
9494
{
9595
_storage[item.Id] = item;
96-
return Task.FromResult(true);
96+
return Task.FromResult(item);
9797
}
9898

99-
return Task.FromResult(false);
99+
return Task.FromResult<TItem>(default);
100100
}
101101
}
102102

@@ -392,20 +392,20 @@ protected override async IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Fu
392392

393393
#region Count
394394

395-
protected override Task<uint> CountAsyncInternal(CancellationToken token = default)
395+
protected override Task<int> CountAsyncInternal(CancellationToken token = default)
396396
{
397397
lock (_storage)
398398
{
399-
return Task.FromResult(Convert.ToUInt32(_storage.Count));
399+
return Task.FromResult(_storage.Count);
400400
}
401401
}
402402

403-
protected override Task<uint> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
403+
protected override Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
404404
{
405405
lock (_storage)
406406
{
407407
var count = _storage.Values.Count(predicate.Compile());
408-
return Task.FromResult(Convert.ToUInt32(count));
408+
return Task.FromResult(count);
409409
}
410410
}
411411

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.4</PackageVersion>
12+
<PackageVersion>1.0.5</PackageVersion>
1313
<Description>Base implementation for Repository</Description>
1414
</PropertyGroup>
1515

ManagedCode.Repository.CosmosDB/CosmosDbRepository.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ protected override async Task<int> InsertAsyncInternal(IEnumerable<TItem> items,
105105

106106
#region InsertOrUpdate
107107

108-
protected override async Task<bool> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
108+
protected override async Task<TItem> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
109109
{
110110
try
111111
{
112112
var container = await _cosmosDbAdapter.GetContainer();
113113
var result = await container.UpsertItemAsync(item, item.PartitionKey, cancellationToken: token);
114-
return result != null;
114+
return result.Resource;
115115
}
116116
catch (Exception e)
117117
{
118-
return false;
118+
return default;
119119
}
120120
}
121121

@@ -165,17 +165,17 @@ protected override async Task<int> InsertOrUpdateAsyncInternal(IEnumerable<TItem
165165

166166
#region Update
167167

168-
protected override async Task<bool> UpdateAsyncInternal(TItem item, CancellationToken token = default)
168+
protected override async Task<TItem> UpdateAsyncInternal(TItem item, CancellationToken token = default)
169169
{
170170
try
171171
{
172172
var container = await _cosmosDbAdapter.GetContainer();
173173
var result = await container.ReplaceItemAsync(item, item.Id, cancellationToken: token);
174-
return result != null;
174+
return result.Resource;
175175
}
176176
catch (Exception e)
177177
{
178-
return false;
178+
return default;
179179
}
180180
}
181181

@@ -662,16 +662,16 @@ protected override async IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Fu
662662

663663
#region Count
664664

665-
protected override async Task<uint> CountAsyncInternal(CancellationToken token = default)
665+
protected override async Task<int> CountAsyncInternal(CancellationToken token = default)
666666
{
667667
var container = await _cosmosDbAdapter.GetContainer();
668-
return Convert.ToUInt32(await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).CountAsync(token));
668+
return await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).CountAsync(token);
669669
}
670670

671-
protected override async Task<uint> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
671+
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
672672
{
673673
var container = await _cosmosDbAdapter.GetContainer();
674-
return Convert.ToUInt32(await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).Where(predicate).CountAsync(token));
674+
return await container.GetItemLinqQueryable<TItem>().Where(SplitByType()).Where(predicate).CountAsync(token);
675675
}
676676

677677
#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.4</PackageVersion>
11+
<PackageVersion>1.0.5</PackageVersion>
1212
<Description>Repository for CosmosDB</Description>
1313
</PropertyGroup>
1414

ManagedCode.Repository.LiteDB/LiteDbRepository.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ protected override async Task<int> InsertAsyncInternal(IEnumerable<TItem> items,
7373

7474
#region InsertOrUpdate
7575

76-
protected override async Task<bool> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
76+
protected override async Task<TItem> InsertOrUpdateAsyncInternal(TItem item, CancellationToken token = default)
7777
{
7878
await Task.Yield();
79-
return GetDatabase().Upsert(item);
79+
if (GetDatabase().Upsert(item))
80+
{
81+
return GetDatabase().FindById(new BsonValue(item.Id));
82+
}
83+
84+
return default;
8085
}
8186

8287
protected override async Task<int> InsertOrUpdateAsyncInternal(IEnumerable<TItem> items, CancellationToken token = default)
@@ -89,10 +94,15 @@ protected override async Task<int> InsertOrUpdateAsyncInternal(IEnumerable<TItem
8994

9095
#region Update
9196

92-
protected override async Task<bool> UpdateAsyncInternal(TItem item, CancellationToken token = default)
97+
protected override async Task<TItem> UpdateAsyncInternal(TItem item, CancellationToken token = default)
9398
{
9499
await Task.Yield();
95-
return GetDatabase().Update(item);
100+
if (GetDatabase().Update(item))
101+
{
102+
return GetDatabase().FindById(new BsonValue(item.Id));
103+
}
104+
105+
return default;
96106
}
97107

98108
protected override async Task<int> UpdateAsyncInternal(IEnumerable<TItem> items, CancellationToken token = default)
@@ -349,16 +359,16 @@ protected override async IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Fu
349359

350360
#region Count
351361

352-
protected override async Task<uint> CountAsyncInternal(CancellationToken token = default)
362+
protected override async Task<int> CountAsyncInternal(CancellationToken token = default)
353363
{
354364
await Task.Yield();
355-
return (uint) GetDatabase().Count();
365+
return GetDatabase().Count();
356366
}
357367

358-
protected override async Task<uint> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
368+
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
359369
{
360370
await Task.Yield();
361-
return (uint) GetDatabase().Count(predicate);
371+
return GetDatabase().Count(predicate);
362372
}
363373

364374
#endregion

ManagedCode.Repository.LiteDB/ManagedCode.Repository.LiteDB.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.4</PackageVersion>
11+
<PackageVersion>1.0.5</PackageVersion>
1212
<Description>Repository for LiteDB</Description>
1313
</PropertyGroup>
1414

0 commit comments

Comments
 (0)