Skip to content

Commit 2fef8f9

Browse files
committed
Issue 2
#2 Adding InsertOrReplace()
1 parent 637ff1c commit 2fef8f9

File tree

6 files changed

+126
-9
lines changed

6 files changed

+126
-9
lines changed

src/TableStorage.Abstractions.POCO.Tests/PocoTableStoreTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ public void insert_record()
5959
Assert.AreEqual(4, tableStore.GetRecordCount());
6060
}
6161

62+
63+
[TestMethod]
64+
public void insert_or_replace_record_inserts_when_record_is_new()
65+
{
66+
var employee = new Employee
67+
{
68+
Name = "Test",
69+
CompanyId = 99,
70+
Id = 99,
71+
Department = new Department { Id = 5, Name = "Test" }
72+
};
73+
tableStore.InsertOrReplace(employee);
74+
Assert.AreEqual(4, tableStore.GetRecordCount());
75+
}
76+
77+
78+
[TestMethod]
79+
public void insert_or_replace_record_updates_when_record_is_not_new()
80+
{
81+
var employee = new Employee
82+
{
83+
Name = "Test",
84+
CompanyId = 99,
85+
Id = 99,
86+
Department = new Department { Id = 5, Name = "Test" }
87+
};
88+
tableStore.Insert(employee);
89+
90+
employee.Name = "xxx";
91+
tableStore.InsertOrReplace(employee);
92+
Assert.AreEqual(4, tableStore.GetRecordCount());
93+
Assert.AreEqual("xxx", tableStore.GetRecord(99,99).Name);
94+
}
95+
6296
[TestMethod]
6397
public void insert_records()
6498
{
@@ -95,6 +129,39 @@ public async Task insert_record_async()
95129
Assert.AreEqual(4, tableStore.GetRecordCount());
96130
}
97131

132+
[TestMethod]
133+
public async Task insert_or_replace_record_inserts_when_record_is_new_async()
134+
{
135+
var employee = new Employee
136+
{
137+
Name = "Test",
138+
CompanyId = 99,
139+
Id = 99,
140+
Department = new Department { Id = 5, Name = "Test" }
141+
};
142+
await tableStore.InsertOrReplaceAsync(employee);
143+
Assert.AreEqual(4, tableStore.GetRecordCount());
144+
}
145+
146+
147+
[TestMethod]
148+
public async Task insert_or_replace_record_updates_when_record_is_not_new_async()
149+
{
150+
var employee = new Employee
151+
{
152+
Name = "Test",
153+
CompanyId = 99,
154+
Id = 99,
155+
Department = new Department { Id = 5, Name = "Test" }
156+
};
157+
await tableStore.InsertAsync(employee);
158+
159+
employee.Name = "xxx";
160+
await tableStore.InsertOrReplaceAsync(employee);
161+
Assert.AreEqual(4, tableStore.GetRecordCount());
162+
Assert.AreEqual("xxx", tableStore.GetRecord(99, 99).Name);
163+
}
164+
98165

99166
[TestMethod]
100167
public async Task insert_records_async()

src/TableStorage.Abstractions.POCO.Tests/TableStorage.Abstractions.POCO.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@
111111
</Reference>
112112
<Reference Include="System.Windows" />
113113
<Reference Include="System.Windows.Forms" />
114-
<Reference Include="TableStorage.Abstractions, Version=2.2.0.0, Culture=neutral, processorArchitecture=MSIL">
115-
<HintPath>..\packages\TableStorage.Abstractions.2.2.0\lib\net46\TableStorage.Abstractions.dll</HintPath>
114+
<Reference Include="TableStorage.Abstractions, Version=2.3.0.0, Culture=neutral, processorArchitecture=MSIL">
115+
<HintPath>..\packages\TableStorage.Abstractions.2.3.0\lib\net46\TableStorage.Abstractions.dll</HintPath>
116116
</Reference>
117117
<Reference Include="TableStorage.Abstractions.TableEntityConverters, Version=1.1.4.0, Culture=neutral, processorArchitecture=MSIL">
118118
<HintPath>..\packages\TableStorage.Abstractions.TableEntityConverters.1.1.4\lib\net462\TableStorage.Abstractions.TableEntityConverters.dll</HintPath>

src/TableStorage.Abstractions.POCO.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<package id="System.Spatial" version="5.8.4" targetFramework="net462" />
2727
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net462" />
2828
<package id="System.ValueTuple" version="4.5.0" targetFramework="net462" />
29-
<package id="TableStorage.Abstractions" version="2.2.0" targetFramework="net462" />
29+
<package id="TableStorage.Abstractions" version="2.3.0" targetFramework="net462" />
3030
<package id="TableStorage.Abstractions.TableEntityConverters" version="1.1.4" targetFramework="net462" />
3131
<package id="Useful.Extensions" version="2.3.0" targetFramework="net462" />
3232
<package id="WindowsAzure.Storage" version="9.3.3" targetFramework="net462" />

src/TableStorage.Abstractions.POCO/IPocoTableStore.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public interface IPocoTableStore<T, TPartitionKey, TRowKey>
2525
/// <exception cref="ArgumentNullException">record</exception>
2626
void Insert(T record);
2727

28+
/// <summary>
29+
/// Inserts the record if it does not exist, else updates the record.
30+
/// </summary>
31+
/// <param name="record">The record</param>
32+
/// <exception cref="ArgumentNullException">record</exception>
33+
void InsertOrReplace(T record);
34+
2835
/// <summary>
2936
/// Inserts the records into a table storage table.
3037
/// </summary>
@@ -186,6 +193,14 @@ PagedResult<T> GetByPartitionKeyPaged(string partitionKey, int pageSize = 100,
186193
/// <exception cref="ArgumentNullException">records</exception>
187194
Task InsertAsync(IEnumerable<T> records);
188195

196+
/// <summary>
197+
/// Inserts the record if it does not exist, else updates the record.
198+
/// </summary>
199+
/// <param name="record">The record</param>
200+
/// <returns>Task</returns>
201+
/// <exception cref="ArgumentNullException">record</exception>
202+
Task InsertOrReplaceAsync(T record);
203+
189204
/// <summary>
190205
/// Updates the record asynchronously.
191206
/// </summary>

src/TableStorage.Abstractions.POCO/PocoTableStore.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public PocoTableStore(string tableName, string storageConnectionString, Expressi
7373
/// or
7474
/// tableConverter</exception>
7575
public PocoTableStore(string tableName, string storageConnectionString,
76-
SimpleKeysConverter<T,TPartitionKey,TRowKey> keysConverter, TableStorageOptions tableStorageOptions = null)
76+
SimpleKeysConverter<T, TPartitionKey, TRowKey> keysConverter, TableStorageOptions tableStorageOptions = null)
7777
{
7878
if (tableName == null) throw new ArgumentNullException(nameof(tableName));
7979
if (storageConnectionString == null) throw new ArgumentNullException(nameof(storageConnectionString));
@@ -99,7 +99,7 @@ public PocoTableStore(string tableName, string storageConnectionString,
9999
/// or
100100
/// tableConverter</exception>
101101
public PocoTableStore(string tableName, string storageConnectionString,
102-
CalculatedKeysConverter<T,TPartitionKey,TRowKey> keysConverter, TableStorageOptions tableStorageOptions = null)
102+
CalculatedKeysConverter<T, TPartitionKey, TRowKey> keysConverter, TableStorageOptions tableStorageOptions = null)
103103
{
104104
if (tableName == null) throw new ArgumentNullException(nameof(tableName));
105105
if (storageConnectionString == null) throw new ArgumentNullException(nameof(storageConnectionString));
@@ -142,6 +142,20 @@ public void Insert(T record)
142142

143143
_tableStore.Insert(entity);
144144
}
145+
/// <summary>
146+
/// Inserts or replaces the record
147+
/// </summary>
148+
/// <param name="record"></param>
149+
/// <exception cref="ArgumentNullException">record</exception>
150+
public void InsertOrReplace(T record)
151+
{
152+
if (record == null)
153+
throw new ArgumentNullException(nameof(record));
154+
155+
var entity = CreateEntity(record);
156+
157+
_tableStore.InsertOrReplace(entity);
158+
}
145159

146160
/// <summary>
147161
/// Inserts the records into a table storage table.
@@ -384,6 +398,22 @@ public Task InsertAsync(T record)
384398
return _tableStore.InsertAsync(entity);
385399
}
386400

401+
/// <summary>
402+
/// Inserts or replaces the record
403+
/// </summary>
404+
/// <param name="record"></param>
405+
/// <returns>Task.</returns>
406+
/// <exception cref="ArgumentNullException">record</exception>
407+
public Task InsertOrReplaceAsync(T record)
408+
{
409+
if (record == null)
410+
throw new ArgumentNullException(nameof(record));
411+
412+
var entity = CreateEntity(record);
413+
414+
return _tableStore.InsertOrReplaceAsync(entity);
415+
}
416+
387417
/// <summary>
388418
/// Inserts the records asynchronously.
389419
/// </summary>
@@ -1008,5 +1038,9 @@ private string GetRowKeyString(TRowKey key)
10081038
{
10091039
return _keysConverter.RowKey(key);
10101040
}
1041+
1042+
1043+
1044+
10111045
}
10121046
}

src/TableStorage.Abstractions.POCO/TableStorage.Abstractions.POCO.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>2.0.1</Version>
5+
<Version>2.1.0</Version>
66
<Authors>Giovanni Galbo</Authors>
77
<Company>Giovanni Galbo</Company>
88
<Description>A repository wrapper for Azure Table Storage that uses POCOs (Plain Old CLR Objects) instead of objects that implemeent ITableEntity.
@@ -15,16 +15,17 @@ This simple library seeks to take care of the mapping for us, so that you can co
1515

1616
The library will convert simple properties to fields in Azure Table Storage. Complex types will serialize as json.</Description>
1717
<Copyright>© Giovanni Galbo 2019</Copyright>
18-
<PackageLicenseUrl>https://github.com/giometrix/TableStorage.Abstractions.POCO/blob/master/LICENSE</PackageLicenseUrl>
18+
<PackageLicenseUrl></PackageLicenseUrl>
1919
<PackageProjectUrl>https://github.com/giometrix/TableStorage.Abstractions.POCO</PackageProjectUrl>
2020
<RepositoryUrl>https://github.com/giometrix/TableStorage.Abstractions.POCO</RepositoryUrl>
2121
<PackageTags>table-storage azure-table-storage poco table-entities tableentity</PackageTags>
22-
<PackageReleaseNotes>Updated description</PackageReleaseNotes>
22+
<PackageReleaseNotes>Added insert or replace functionality</PackageReleaseNotes>
2323
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
24+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2425
</PropertyGroup>
2526

2627
<ItemGroup>
27-
<PackageReference Include="TableStorage.Abstractions" Version="2.2.0" />
28+
<PackageReference Include="TableStorage.Abstractions" Version="2.3.0" />
2829
<PackageReference Include="TableStorage.Abstractions.TableEntityConverters" Version="1.1.4" />
2930
</ItemGroup>
3031

0 commit comments

Comments
 (0)