4
4
using System . Linq ;
5
5
using System . Linq . Expressions ;
6
6
using System . Reflection ;
7
- using Microsoft . Azure . Cosmos . Table ;
7
+ using Azure . Data . Tables ;
8
8
9
9
namespace TableStorage . Abstractions . TableEntityConverters
10
10
{
@@ -22,12 +22,12 @@ public static void SetDefaultJsonSerializerSettings (JsonSerializerSettings json
22
22
_defaultJsonSerializerSettings = jsonSerializerSettings ?? new JsonSerializerSettings ( ) ;
23
23
}
24
24
25
- public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
25
+ public static TableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
26
26
params Expression < Func < T , object > > [ ] ignoredProperties )
27
27
{
28
28
return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , default , ignoredProperties ) ;
29
29
}
30
- public static DynamicTableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
30
+ public static TableEntity ToTableEntity < T > ( this T o , string partitionKey , string rowKey ,
31
31
JsonSerializerSettings jsonSerializerSettings ,
32
32
PropertyConverters < T > propertyConverters = default ,
33
33
params Expression < Func < T , object > > [ ] ignoredProperties )
@@ -39,14 +39,14 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, string partitionKey,
39
39
return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
40
40
}
41
41
42
- public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
42
+ public static TableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
43
43
Expression < Func < T , object > > rowProperty ,
44
44
params Expression < Func < T , object > > [ ] ignoredProperties )
45
45
{
46
46
return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , null , ignoredProperties ) ;
47
47
}
48
48
49
- public static DynamicTableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
49
+ public static TableEntity ToTableEntity < T > ( this T o , Expression < Func < T , object > > partitionProperty ,
50
50
Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings ,
51
51
PropertyConverters < T > propertyConverters = default ,
52
52
params Expression < Func < T , object > > [ ] ignoredProperties )
@@ -76,14 +76,14 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
76
76
return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
77
77
}
78
78
79
- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
79
+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
80
80
Expression < Func < T , object > > partitionProperty ,
81
81
Expression < Func < T , object > > rowProperty ) where T : new ( )
82
82
{
83
83
return FromTableEntity < T , TP , TR > ( entity , partitionProperty , rowProperty , _defaultJsonSerializerSettings ) ;
84
84
}
85
85
86
- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
86
+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
87
87
Expression < Func < T , object > > partitionProperty ,
88
88
Expression < Func < T , object > > rowProperty ,
89
89
JsonSerializerSettings jsonSerializerSettings ,
@@ -106,7 +106,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
106
106
rowProperty , convertRow , jsonSerializerSettings , propertyConverters ) ;
107
107
}
108
108
109
- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
109
+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
110
110
Expression < Func < T , object > > partitionProperty ,
111
111
Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
112
112
Func < string , TR > convertRowKey ) where T : new ( )
@@ -115,7 +115,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
115
115
convertRowKey , _defaultJsonSerializerSettings ) ;
116
116
}
117
117
118
- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
118
+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
119
119
Expression < Func < T , object > > partitionProperty ,
120
120
Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
121
121
Func < string , TR > convertRowKey , JsonSerializerSettings jsonSerializerSettings ,
@@ -156,12 +156,12 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
156
156
return o ;
157
157
}
158
158
159
- public static T FromTableEntity < T > ( this DynamicTableEntity entity ) where T : new ( )
159
+ public static T FromTableEntity < T > ( this TableEntity entity ) where T : new ( )
160
160
{
161
161
return FromTableEntity < T > ( entity , _defaultJsonSerializerSettings ) ;
162
162
}
163
163
164
- public static T FromTableEntity < T > ( this DynamicTableEntity entity ,
164
+ public static T FromTableEntity < T > ( this TableEntity entity ,
165
165
JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters = default ) where T : new ( )
166
166
{
167
167
_ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
@@ -188,10 +188,10 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
188
188
return name ;
189
189
}
190
190
191
- private static void SetTimestamp < T > ( DynamicTableEntity entity , T o , List < PropertyInfo > properties ) where T : new ( )
191
+ private static void SetTimestamp < T > ( TableEntity entity , T o , List < PropertyInfo > properties ) where T : new ( )
192
192
{
193
193
var timestampProperty = properties
194
- . FirstOrDefault ( p => p . Name == nameof ( DynamicTableEntity . Timestamp ) ) ;
194
+ . FirstOrDefault ( p => p . Name == nameof ( TableEntity . Timestamp ) ) ;
195
195
196
196
if ( timestampProperty != null )
197
197
{
@@ -202,7 +202,7 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
202
202
203
203
if ( timestampProperty . PropertyType == typeof ( DateTime ) )
204
204
{
205
- timestampProperty . SetValue ( o , entity . Timestamp . UtcDateTime ) ;
205
+ timestampProperty . SetValue ( o , entity . Timestamp ? . UtcDateTime ) ;
206
206
}
207
207
208
208
if ( timestampProperty . PropertyType == typeof ( string ) )
@@ -212,48 +212,48 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
212
212
}
213
213
}
214
214
215
- private static void FillProperties < T > ( DynamicTableEntity entity , T o , List < PropertyInfo > properties , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters ) where T : new ( )
215
+ private static void FillProperties < T > ( TableEntity entity , T o , List < PropertyInfo > properties , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters ) where T : new ( )
216
216
{
217
217
foreach ( var propertyInfo in properties )
218
218
{
219
- if ( propertyConverters != null && entity . Properties . ContainsKey ( propertyInfo . Name ) && propertyConverters . ContainsKey ( propertyInfo . Name ) )
219
+ if ( propertyConverters != null && entity . Keys . Contains ( propertyInfo . Name ) && propertyConverters . ContainsKey ( propertyInfo . Name ) )
220
220
{
221
- propertyConverters [ propertyInfo . Name ] . SetObjectProperty ( o , entity . Properties [ propertyInfo . Name ] ) ;
221
+ propertyConverters [ propertyInfo . Name ] . SetObjectProperty ( o , entity [ propertyInfo . Name ] ) ;
222
222
}
223
- else if ( entity . Properties . ContainsKey ( propertyInfo . Name ) && propertyInfo . Name != nameof ( DynamicTableEntity . Timestamp ) )
223
+ else if ( entity . Keys . Contains ( propertyInfo . Name ) && propertyInfo . Name != nameof ( TableEntity . Timestamp ) )
224
224
{
225
- var val = entity . Properties [ propertyInfo . Name ] . PropertyAsObject ;
225
+ var val = entity [ propertyInfo . Name ] ;
226
226
227
227
if ( val != null && ( propertyInfo . PropertyType == typeof ( DateTimeOffset ) || propertyInfo . PropertyType == typeof ( DateTimeOffset ? ) ) )
228
228
{
229
- val = entity . Properties [ propertyInfo . Name ] . DateTimeOffsetValue ;
229
+ val = entity . GetDateTimeOffset ( propertyInfo . Name ) ;
230
230
}
231
231
232
232
if ( val != null && propertyInfo . PropertyType == typeof ( double ) )
233
233
{
234
- val = entity . Properties [ propertyInfo . Name ] . DoubleValue ;
234
+ val = entity . GetDouble ( propertyInfo . Name ) ;
235
235
}
236
236
237
237
if ( val != null && propertyInfo . PropertyType == typeof ( int ) )
238
238
{
239
- val = entity . Properties [ propertyInfo . Name ] . Int32Value ;
239
+ val = entity . GetInt32 ( propertyInfo . Name ) ;
240
240
}
241
241
242
242
if ( val != null && propertyInfo . PropertyType == typeof ( long ) )
243
243
{
244
- val = entity . Properties [ propertyInfo . Name ] . Int64Value ;
244
+ val = entity . GetInt64 ( propertyInfo . Name ) ;
245
245
}
246
246
247
247
if ( val != null && propertyInfo . PropertyType == typeof ( Guid ) )
248
248
{
249
- val = entity . Properties [ propertyInfo . Name ] . GuidValue ;
249
+ val = entity . GetGuid ( propertyInfo . Name ) ;
250
250
}
251
251
252
252
propertyInfo . SetValue ( o , val ) ;
253
253
}
254
- else if ( entity . Properties . ContainsKey ( $ "{ propertyInfo . Name } Json") )
254
+ else if ( entity . Keys . Contains ( $ "{ propertyInfo . Name } Json") )
255
255
{
256
- var val = entity . Properties [ $ "{ propertyInfo . Name } Json"] . StringValue ;
256
+ var val = entity . GetString ( $ "{ propertyInfo . Name } Json") ;
257
257
if ( val != null )
258
258
{
259
259
var propVal = JsonConvert . DeserializeObject ( val , propertyInfo . PropertyType , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ;
@@ -263,15 +263,15 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
263
263
}
264
264
}
265
265
266
- private static DynamicTableEntity CreateTableEntity < T > ( object o , List < PropertyInfo > properties ,
266
+ private static TableEntity CreateTableEntity < T > ( object o , List < PropertyInfo > properties ,
267
267
string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters )
268
268
{
269
- var entity = new DynamicTableEntity ( partitionKey , rowKey ) ;
269
+ var entity = new TableEntity ( partitionKey , rowKey ) ;
270
270
foreach ( var propertyInfo in properties )
271
271
{
272
272
var name = propertyInfo . Name ;
273
273
var val = propertyInfo . GetValue ( o ) ;
274
- EntityProperty entityProperty ;
274
+ object entityProperty ;
275
275
if ( propertyConverters != null && propertyConverters . ContainsKey ( name ) )
276
276
{
277
277
entityProperty = propertyConverters [ name ] . ToTableEntityProperty ( ( T ) o ) ;
@@ -281,50 +281,50 @@ private static DynamicTableEntity CreateTableEntity<T>(object o, List<PropertyIn
281
281
switch ( val )
282
282
{
283
283
case int x :
284
- entityProperty = new EntityProperty ( x ) ;
284
+ entityProperty = x ;
285
285
break ;
286
286
case short x :
287
- entityProperty = new EntityProperty ( x ) ;
287
+ entityProperty = x ;
288
288
break ;
289
289
case byte x :
290
- entityProperty = new EntityProperty ( x ) ;
290
+ entityProperty = x ;
291
291
break ;
292
292
case string x :
293
- entityProperty = new EntityProperty ( x ) ;
293
+ entityProperty = x ;
294
294
break ;
295
295
case double x :
296
- entityProperty = new EntityProperty ( x ) ;
296
+ entityProperty = x ;
297
297
break ;
298
298
case DateTime x :
299
- entityProperty = new EntityProperty ( x ) ;
299
+ entityProperty = x ;
300
300
break ;
301
301
case DateTimeOffset x :
302
- entityProperty = new EntityProperty ( x ) ;
302
+ entityProperty = x ;
303
303
break ;
304
304
case bool x :
305
- entityProperty = new EntityProperty ( x ) ;
305
+ entityProperty = x ;
306
306
break ;
307
307
case byte [ ] x :
308
- entityProperty = new EntityProperty ( x ) ;
308
+ entityProperty = x ;
309
309
break ;
310
310
case long x :
311
- entityProperty = new EntityProperty ( x ) ;
311
+ entityProperty = x ;
312
312
break ;
313
313
case Guid x :
314
- entityProperty = new EntityProperty ( x ) ;
314
+ entityProperty = x ;
315
315
break ;
316
316
case null :
317
- entityProperty = new EntityProperty ( ( int ? ) null ) ;
317
+ entityProperty = null ;
318
318
break ;
319
319
default :
320
320
name += "Json" ;
321
- entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val ,
322
- jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ) ;
321
+ entityProperty = JsonConvert . SerializeObject ( val ,
322
+ jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ;
323
323
break ;
324
324
}
325
325
}
326
326
327
- entity . Properties [ name ] = entityProperty ;
327
+ entity [ name ] = entityProperty ;
328
328
}
329
329
return entity ;
330
330
}
0 commit comments