Skip to content

Commit 125fc39

Browse files
committed
calculated key mapper
1 parent 3a1accc commit 125fc39

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,57 @@ public void get_record_with_calculated_partition_key_from_multiple_properties_us
13541354

13551355
}
13561356

1357+
public class PartitionKey
1358+
{
1359+
public PartitionKey(int companyId, int departmentId)
1360+
{
1361+
CompanyId = companyId;
1362+
DepartmentId = departmentId;
1363+
}
1364+
public int CompanyId { get; }
1365+
public int DepartmentId { get; }
1366+
}
1367+
1368+
1369+
[TestMethod]
1370+
public void get_record_with_calculated_partition_key_from_multiple_properties_using_class_as_key()
1371+
{
1372+
1373+
1374+
var pKeyMapper = new CalculatedKeyMapper<Employee, PartitionKey>(e => $"{e.CompanyId}.{e.Department.Id}", key =>
1375+
{
1376+
var parts = key.Split('.');
1377+
var companyId = int.Parse(parts[0]);
1378+
var departmentId = int.Parse(parts[1]);
1379+
return new PartitionKey(companyId, departmentId);
1380+
}, key=>$"{key.CompanyId}.{key.DepartmentId}");
1381+
1382+
var rKeyMapper = new KeyMapper<Employee, int>(e => e.Id.ToString(), int.Parse, e => e.Id,
1383+
id => id.ToString());
1384+
1385+
var tableConverter = new CalculatedKeysTableConverter<Employee, PartitionKey, int>(pKeyMapper, rKeyMapper);
1386+
1387+
var tableStore2 = new PocoTableStore<Employee, PartitionKey, int>("TestEmployee", "UseDevelopmentStorage=true", tableConverter);
1388+
1389+
1390+
var employee = new Employee
1391+
{
1392+
CompanyId = 1,
1393+
Id = 1,
1394+
Name = "Mr. Jim CEO",
1395+
Department = new Department { Id = 22, Name = "Executive" }
1396+
};
1397+
tableStore2.Insert(employee);
1398+
1399+
var record = tableStore2.GetRecord(new PartitionKey(1, 22), 1);
1400+
1401+
Assert.AreEqual(1, record.Id);
1402+
Assert.AreEqual(22, record.Department.Id);
1403+
Assert.AreEqual("Mr. Jim CEO", record.Name);
1404+
1405+
}
1406+
1407+
13571408

13581409
[TestMethod]
13591410
public void update_record_with_calculated_partition_key_from_multiple_properties_using_extension_method()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace TableStorage.Abstractions.POCO
9+
{
10+
public class CalculatedKeyMapper<T, TKey> : KeyMapper<T,TKey>
11+
{
12+
public CalculatedKeyMapper(Func<T, string> toKey, Func<string, TKey> fromKey, Func<TKey, string> toKeyFromParameter) : base(toKey, fromKey, null, toKeyFromParameter)
13+
{
14+
}
15+
}
16+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@
8888
<Reference Include="WindowsBase" />
8989
</ItemGroup>
9090
<ItemGroup>
91+
<Compile Include="CalculatedKeyMapper.cs" />
9192
<Compile Include="CalculatedKeysTableConverter.cs" />
9293
<Compile Include="FixedKeyMapper.cs" />
94+
<Compile Include="KeyGenerator.cs" />
9395
<Compile Include="KeyMapper.cs" />
9496
<Compile Include="SimpleTableConverter.cs" />
9597
<Compile Include="ITableConverter.cs" />
96-
<Compile Include="KeyGenerator.cs" />
9798
<Compile Include="PocoTableStore.cs" />
9899
<Compile Include="Properties\AssemblyInfo.cs" />
99100
</ItemGroup>

0 commit comments

Comments
 (0)