Skip to content

Commit d7e31be

Browse files
committed
new helper class for creating partition keys and row keys
1 parent 53ab9a9 commit d7e31be

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,106 @@ public void get_record_with_calculated_partition_key_from_multiple_properties()
12461246

12471247
}
12481248

1249+
[TestMethod]
1250+
public void update_record_with_calculated_partition_key_from_multiple_properties()
1251+
{
1252+
tableStore = new PocoTableStore<Employee, int, int>("TestEmployee", "UseDevelopmentStorage=true",
1253+
partitionProperty: null, rowProperty: e => e.Id, calculatedPartitionKey: e => e.CompanyId + "." + e.Department.Id, calculatedRowKey: e => e.Id.ToString(),
1254+
calculatedPartitionKeyFromParameter: x => null,
1255+
calculatedRowKeyFromParameter: x => x.ToString(),
1256+
convertPartitionKey: null, convertRowKey: int.Parse);
1257+
1258+
var employee = new Employee
1259+
{
1260+
CompanyId = 1,
1261+
Id = 1,
1262+
Name = "Mr. Jim CEO",
1263+
Department = new Department { Id = 22, Name = "Executive" }
1264+
};
1265+
tableStore.Insert(employee);
1266+
1267+
employee.Name = "Ted";
1268+
1269+
tableStore.Update(employee);
1270+
1271+
var record = tableStore.GetRecord("1.22", "1");
1272+
1273+
Assert.AreEqual(1, record.Id);
1274+
Assert.AreEqual(22, record.Department.Id);
1275+
Assert.AreEqual("Ted", record.Name);
1276+
1277+
}
1278+
1279+
[TestMethod]
1280+
public void get_record_with_calculated_partition_key_from_multiple_properties_using_extension_method()
1281+
{
1282+
KeyGenerator.DefineParitionKey(typeof(Employee), e=>$"{e.CompanyId}.{e.DepartmentId}");
1283+
KeyGenerator.DefineRowKey(typeof(Employee), e => $"{e.Id}");
1284+
1285+
tableStore = new PocoTableStore<Employee, int, int>("TestEmployee", "UseDevelopmentStorage=true",
1286+
partitionProperty: null, rowProperty: e => e.Id, calculatedPartitionKey: e => e.CompanyId + "." + e.Department.Id, calculatedRowKey: e => e.Id.ToString(),
1287+
calculatedPartitionKeyFromParameter: x => null,
1288+
calculatedRowKeyFromParameter: x => x.ToString(),
1289+
convertPartitionKey: null, convertRowKey: int.Parse);
1290+
1291+
1292+
1293+
var employee = new Employee
1294+
{
1295+
CompanyId = 1,
1296+
Id = 1,
1297+
Name = "Mr. Jim CEO",
1298+
Department = new Department { Id = 22, Name = "Executive" }
1299+
};
1300+
tableStore.Insert(employee);
1301+
1302+
var record = tableStore.GetRecord(KeyGenerator.PartitionKey<Employee>(new {CompanyId=1, DepartmentId=22}),
1303+
KeyGenerator.RowKey<Employee>(new { Id = 1 }));
1304+
1305+
Assert.AreEqual(1, record.Id);
1306+
Assert.AreEqual(22, record.Department.Id);
1307+
Assert.AreEqual("Mr. Jim CEO", record.Name);
1308+
1309+
}
1310+
1311+
1312+
[TestMethod]
1313+
public void update_record_with_calculated_partition_key_from_multiple_properties_using_extension_method()
1314+
{
1315+
KeyGenerator.DefineParitionKey(typeof(Employee), e => $"{e.CompanyId}.{e.DepartmentId}");
1316+
KeyGenerator.DefineRowKey(typeof(Employee), e => $"{e.Id}");
1317+
1318+
tableStore = new PocoTableStore<Employee, int, int>("TestEmployee", "UseDevelopmentStorage=true",
1319+
partitionProperty: null, rowProperty: e => e.Id, calculatedPartitionKey: e => e.CompanyId + "." + e.Department.Id, calculatedRowKey: e => e.Id.ToString(),
1320+
calculatedPartitionKeyFromParameter: x => null,
1321+
calculatedRowKeyFromParameter: x => x.ToString(),
1322+
convertPartitionKey: null, convertRowKey: int.Parse);
1323+
1324+
1325+
1326+
var employee = new Employee
1327+
{
1328+
CompanyId = 1,
1329+
Id = 1,
1330+
Name = "Mr. Jim CEO",
1331+
Department = new Department { Id = 22, Name = "Executive" }
1332+
};
1333+
tableStore.Insert(employee);
1334+
1335+
employee.Name = "Ted";
1336+
1337+
tableStore.Update(employee);
1338+
1339+
var record = tableStore.GetRecord(
1340+
KeyGenerator.PartitionKey<Employee>(new { CompanyId = 1, DepartmentId = 22 }),
1341+
KeyGenerator.RowKey<Employee>(new { Id = 1 }));
1342+
1343+
Assert.AreEqual(1, record.Id);
1344+
Assert.AreEqual(22, record.Department.Id);
1345+
Assert.AreEqual("Ted", record.Name);
1346+
1347+
}
1348+
12491349
[TestCleanup]
12501350
public void Cleanup()
12511351
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Reference Include="Microsoft.Azure.KeyVault.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
4343
<HintPath>..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
4444
</Reference>
45+
<Reference Include="Microsoft.CSharp" />
4546
<Reference Include="Microsoft.Data.Edm, Version=5.8.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
4647
<HintPath>..\packages\Microsoft.Data.Edm.5.8.2\lib\net40\Microsoft.Data.Edm.dll</HintPath>
4748
</Reference>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace TableStorage.Abstractions.POCO
10+
{
11+
public static class KeyGenerator
12+
{
13+
private static readonly ConcurrentDictionary<Type, Func<dynamic, string>> _partitionKeyExpressions =
14+
new ConcurrentDictionary<Type, Func<dynamic, string>>();
15+
16+
private static readonly ConcurrentDictionary<Type, Func<dynamic, string>> _rowKeyExpressions =
17+
new ConcurrentDictionary<Type, Func<dynamic, string>>();
18+
19+
public static void DefineParitionKey(Type type, Func<dynamic, string> partitionKeyExpression)
20+
{
21+
_partitionKeyExpressions.TryGetValue(type, out Func<dynamic, string> expr);
22+
if (expr == null)
23+
{
24+
_partitionKeyExpressions[type] = partitionKeyExpression;
25+
}
26+
27+
}
28+
29+
public static void DefineRowKey(Type type, Func<dynamic, string> rowKeyExpression)
30+
{
31+
_rowKeyExpressions.TryGetValue(type, out Func<dynamic, string> expr);
32+
if (expr == null)
33+
{
34+
_rowKeyExpressions[type] = rowKeyExpression;
35+
}
36+
37+
}
38+
39+
40+
public static string PartitionKey<T>(object obj)
41+
{
42+
try
43+
{
44+
return _partitionKeyExpressions[typeof(T)](obj);
45+
}
46+
catch (NullReferenceException e)
47+
{
48+
throw new Exception("Partition definition was not provided", e);
49+
}
50+
51+
}
52+
53+
public static string RowKey<T>(object obj)
54+
{
55+
try
56+
{
57+
return _rowKeyExpressions[typeof(T)](obj);
58+
}
59+
catch (NullReferenceException e)
60+
{
61+
throw new Exception("Row key definition was not provided", e);
62+
}
63+
64+
}
65+
}
66+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Reference Include="WindowsBase" />
8989
</ItemGroup>
9090
<ItemGroup>
91+
<Compile Include="KeyGenerator.cs" />
9192
<Compile Include="PocoTableStore.cs" />
9293
<Compile Include="Properties\AssemblyInfo.cs" />
9394
</ItemGroup>

0 commit comments

Comments
 (0)