Skip to content

Commit 3a1accc

Browse files
committed
easier to use constructors for complex mappings
1 parent d7e31be commit 3a1accc

File tree

8 files changed

+415
-303
lines changed

8 files changed

+415
-303
lines changed

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

Lines changed: 184 additions & 136 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using Microsoft.WindowsAzure.Storage.Table;
4+
using TableStorage.Abstractions.TableEntityConverters;
5+
6+
namespace TableStorage.Abstractions.POCO
7+
{
8+
public class CalculatedKeysTableConverter<T, TPartitionKey, TRowKey> : ITableConverter<T, TPartitionKey, TRowKey>
9+
where T: new()
10+
{
11+
private readonly KeyMapper<T, TPartitionKey> _partitionMapper;
12+
private readonly KeyMapper<T, TRowKey> _rowMapper;
13+
private readonly Expression<Func<T, object>>[] _ignoredProperties;
14+
15+
public CalculatedKeysTableConverter(KeyMapper<T, TPartitionKey> partitionMapper, KeyMapper<T,TRowKey> rowMapper,
16+
params Expression<Func<T, object>>[] ignoredProperties)
17+
{
18+
_partitionMapper = partitionMapper;
19+
_rowMapper = rowMapper;
20+
_ignoredProperties = ignoredProperties;
21+
}
22+
public DynamicTableEntity ToEntity(T obj)
23+
{
24+
return obj.ToTableEntity(_partitionMapper.ToKey(obj), _rowMapper.ToKey(obj), _ignoredProperties);
25+
}
26+
27+
public T FromEntity(DynamicTableEntity entity)
28+
{
29+
return entity.FromTableEntity(_partitionMapper.KeyProperty, _partitionMapper.FromKey, _rowMapper.KeyProperty,
30+
_rowMapper.FromKey);
31+
}
32+
33+
public string PartitionKey(TPartitionKey key)
34+
{
35+
return _partitionMapper.ToKeyFromParameter(key);
36+
}
37+
38+
public string RowKey(TRowKey key)
39+
{
40+
return _rowMapper.ToKeyFromParameter(key);
41+
}
42+
}
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace TableStorage.Abstractions.POCO
4+
{
5+
public class FixedKeyMapper<T, TKey> : KeyMapper<T, TKey>
6+
{
7+
public FixedKeyMapper(string key) : base(x=>key, null, null, x=>key)
8+
{
9+
}
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.WindowsAzure.Storage.Table;
2+
3+
namespace TableStorage.Abstractions.POCO
4+
{
5+
public interface ITableConverter<T, TPartitionKey, TRowKey>
6+
{
7+
DynamicTableEntity ToEntity(T obj);
8+
T FromEntity(DynamicTableEntity entity);
9+
string PartitionKey(TPartitionKey key);
10+
string RowKey(TRowKey key);
11+
}
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
4+
namespace TableStorage.Abstractions.POCO
5+
{
6+
public class KeyMapper<T, TKey>
7+
{
8+
private Func<T, string> _toKey;
9+
private Func<TKey, string> _toKeyFromParameter;
10+
private Func<string, TKey> _fromKey;
11+
private Expression<Func<T, object>> _keyProperty;
12+
public KeyMapper(Func<T, string> toKey, Func<string, TKey> fromKey, Expression<Func<T, object>> keyProperty, Func<TKey, string> toKeyFromParameter)
13+
{
14+
if (toKey == null) throw new ArgumentNullException(nameof(toKey));
15+
//if (fromKey == null) throw new ArgumentNullException(nameof(fromKey));
16+
17+
this._toKey = toKey;
18+
_fromKey = fromKey;
19+
_keyProperty = keyProperty;
20+
_toKeyFromParameter = toKeyFromParameter;
21+
}
22+
23+
public Expression<Func<T, object>> KeyProperty
24+
{
25+
get { return _keyProperty; }
26+
}
27+
28+
public string ToKey(T obj)
29+
{
30+
return _toKey(obj);
31+
}
32+
33+
public string ToKeyFromParameter(TKey key)
34+
{
35+
return _toKeyFromParameter(key);
36+
}
37+
38+
public TKey FromKey(string key)
39+
{
40+
return _fromKey(key);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)