-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStorageContextAttributeMapping.cs
More file actions
135 lines (113 loc) · 5.35 KB
/
StorageContextAttributeMapping.cs
File metadata and controls
135 lines (113 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using CoreHelpers.WindowsAzure.Storage.Table.Attributes;
using System.Collections.Generic;
using System.Reflection;
using CoreHelpers.WindowsAzure.Storage.Table.Extensions;
namespace CoreHelpers.WindowsAzure.Storage.Table
{
public partial class StorageContext : IStorageContext
{
private Dictionary<Type, StorageEntityMapper> _entityMapperRegistry { get; set; } = new Dictionary<Type, StorageEntityMapper>();
public void AddEntityMapper(Type entityType, StorageEntityMapper entityMapper)
=> _entityMapperRegistry.Add(entityType, entityMapper);
public void AddEntityMapper(Type entityType, string partitionKeyFormat, string rowKeyFormat, string tableName)
{
AddEntityMapper(entityType, partitionKeyFormat, rowKeyFormat, nVirtualValueEncoding.None, tableName);
}
public void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, nVirtualValueEncoding rowKeyEncoding, String tableName)
{
_entityMapperRegistry.Add(entityType, new StorageEntityMapper()
{
PartitionKeyFormat = partitionKeyFormat,
RowKeyFormat = rowKeyFormat,
RowKeyEncoding = rowKeyEncoding,
TableName = tableName
});
}
public void RemoveEntityMapper(Type entityType)
{
if (_entityMapperRegistry.ContainsKey(entityType))
_entityMapperRegistry.Remove(entityType);
}
public void AddAttributeMapper()
{
AddAttributeMapper(Assembly.GetEntryAssembly());
AddAttributeMapper(Assembly.GetCallingAssembly());
}
internal void AddAttributeMapper(Assembly assembly)
{
var typesWithAttribute = assembly.GetTypesWithAttribute(typeof(StorableAttribute));
foreach (var type in typesWithAttribute)
{
AddAttributeMapper(type);
}
}
public void AddAttributeMapper<T>(String optionalTablenameOverride = null) where T : class
{
AddAttributeMapper(typeof(T), optionalTablenameOverride);
}
public void AddAttributeMapper(Type type)
{
AddAttributeMapper(type, string.Empty);
}
public void AddAttributeMapper(Type type, String optionalTablenameOverride)
{
string typeField = null;
// get the concrete attribute
var storableAttribute = type.GetTypeInfo().GetCustomAttribute<StorableAttribute>();
if (String.IsNullOrEmpty(storableAttribute.Tablename))
{
storableAttribute.Tablename = type.Name;
}
if (!String.IsNullOrEmpty(storableAttribute.TypeField))
{
typeField = storableAttribute.TypeField;
}
// store the neded properties
string partitionKeyFormat = null;
string rowKeyFormat = null;
var rowKeyEncoding = nVirtualValueEncoding.None;
// get the partitionkey property & rowkey property
var properties = type.GetRuntimeProperties();
foreach (var property in properties)
{
if (partitionKeyFormat != null && rowKeyFormat != null)
break;
if (partitionKeyFormat == null && property.GetCustomAttribute<PartitionKeyAttribute>() != null)
partitionKeyFormat = property.Name;
if (rowKeyFormat == null && property.GetCustomAttribute<RowKeyAttribute>() != null)
rowKeyFormat = property.Name;
}
// virutal partition key property
var virtualPartitionKeyAttribute = type.GetTypeInfo().GetCustomAttribute<VirtualPartitionKeyAttribute>();
if (virtualPartitionKeyAttribute != null && !String.IsNullOrEmpty(virtualPartitionKeyAttribute.PartitionKeyFormat))
partitionKeyFormat = virtualPartitionKeyAttribute.PartitionKeyFormat;
// virutal row key property
var virtualRowKeyAttribute = type.GetTypeInfo().GetCustomAttribute<VirtualRowKeyAttribute>();
if (virtualRowKeyAttribute != null && !String.IsNullOrEmpty(virtualRowKeyAttribute.RowKeyFormat))
rowKeyFormat = virtualRowKeyAttribute.RowKeyFormat;
if (virtualRowKeyAttribute != null && virtualRowKeyAttribute.Encoding != nVirtualValueEncoding.None)
rowKeyEncoding = virtualRowKeyAttribute.Encoding;
// check
if (partitionKeyFormat == null || rowKeyFormat == null)
throw new Exception("Missing Partition or RowKey Attribute");
// build the mapper
AddEntityMapper(type, new StorageEntityMapper()
{
TableName = String.IsNullOrEmpty(optionalTablenameOverride) ? storableAttribute.Tablename : optionalTablenameOverride,
PartitionKeyFormat = partitionKeyFormat,
RowKeyFormat = rowKeyFormat,
RowKeyEncoding = rowKeyEncoding,
TypeField = typeField,
});
}
public IEnumerable<Type> GetRegisteredMappers()
{
return _entityMapperRegistry.Keys;
}
public StorageEntityMapper GetEntityMapper<T>()
{
return _entityMapperRegistry[typeof(T)];
}
}
}