44using System . Linq ;
55using System . Linq . Expressions ;
66using System . Reflection ;
7- using Microsoft . Azure . Cosmos . Table ;
7+ using Azure . Data . Tables ;
88
99namespace TableStorage . Abstractions . TableEntityConverters
1010{
@@ -22,12 +22,12 @@ public static void SetDefaultJsonSerializerSettings (JsonSerializerSettings json
2222 _defaultJsonSerializerSettings = jsonSerializerSettings ?? new JsonSerializerSettings ( ) ;
2323 }
2424
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 ,
2626 params Expression < Func < T , object > > [ ] ignoredProperties )
2727 {
2828 return ToTableEntity ( o , partitionKey , rowKey , _defaultJsonSerializerSettings , default , ignoredProperties ) ;
2929 }
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 ,
3131 JsonSerializerSettings jsonSerializerSettings ,
3232 PropertyConverters < T > propertyConverters = default ,
3333 params Expression < Func < T , object > > [ ] ignoredProperties )
@@ -39,14 +39,14 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, string partitionKey,
3939 return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
4040 }
4141
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 ,
4343 Expression < Func < T , object > > rowProperty ,
4444 params Expression < Func < T , object > > [ ] ignoredProperties )
4545 {
4646 return ToTableEntity ( o , partitionProperty , rowProperty , _defaultJsonSerializerSettings , null , ignoredProperties ) ;
4747 }
4848
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 ,
5050 Expression < Func < T , object > > rowProperty , JsonSerializerSettings jsonSerializerSettings ,
5151 PropertyConverters < T > propertyConverters = default ,
5252 params Expression < Func < T , object > > [ ] ignoredProperties )
@@ -76,14 +76,14 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
7676 return CreateTableEntity ( o , properties , partitionKey , rowKey , jsonSerializerSettings , propertyConverters ) ;
7777 }
7878
79- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
79+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
8080 Expression < Func < T , object > > partitionProperty ,
8181 Expression < Func < T , object > > rowProperty ) where T : new ( )
8282 {
8383 return FromTableEntity < T , TP , TR > ( entity , partitionProperty , rowProperty , _defaultJsonSerializerSettings ) ;
8484 }
8585
86- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
86+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
8787 Expression < Func < T , object > > partitionProperty ,
8888 Expression < Func < T , object > > rowProperty ,
8989 JsonSerializerSettings jsonSerializerSettings ,
@@ -106,7 +106,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
106106 rowProperty , convertRow , jsonSerializerSettings , propertyConverters ) ;
107107 }
108108
109- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
109+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
110110 Expression < Func < T , object > > partitionProperty ,
111111 Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
112112 Func < string , TR > convertRowKey ) where T : new ( )
@@ -115,7 +115,7 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
115115 convertRowKey , _defaultJsonSerializerSettings ) ;
116116 }
117117
118- public static T FromTableEntity < T , TP , TR > ( this DynamicTableEntity entity ,
118+ public static T FromTableEntity < T , TP , TR > ( this TableEntity entity ,
119119 Expression < Func < T , object > > partitionProperty ,
120120 Func < string , TP > convertPartitionKey , Expression < Func < T , object > > rowProperty ,
121121 Func < string , TR > convertRowKey , JsonSerializerSettings jsonSerializerSettings ,
@@ -156,12 +156,12 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
156156 return o ;
157157 }
158158
159- public static T FromTableEntity < T > ( this DynamicTableEntity entity ) where T : new ( )
159+ public static T FromTableEntity < T > ( this TableEntity entity ) where T : new ( )
160160 {
161161 return FromTableEntity < T > ( entity , _defaultJsonSerializerSettings ) ;
162162 }
163163
164- public static T FromTableEntity < T > ( this DynamicTableEntity entity ,
164+ public static T FromTableEntity < T > ( this TableEntity entity ,
165165 JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters = default ) where T : new ( )
166166 {
167167 _ = jsonSerializerSettings ?? throw new ArgumentNullException ( nameof ( jsonSerializerSettings ) ) ;
@@ -188,10 +188,10 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
188188 return name ;
189189 }
190190
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 ( )
192192 {
193193 var timestampProperty = properties
194- . FirstOrDefault ( p => p . Name == nameof ( DynamicTableEntity . Timestamp ) ) ;
194+ . FirstOrDefault ( p => p . Name == nameof ( TableEntity . Timestamp ) ) ;
195195
196196 if ( timestampProperty != null )
197197 {
@@ -202,7 +202,7 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
202202
203203 if ( timestampProperty . PropertyType == typeof ( DateTime ) )
204204 {
205- timestampProperty . SetValue ( o , entity . Timestamp . UtcDateTime ) ;
205+ timestampProperty . SetValue ( o , entity . Timestamp ? . UtcDateTime ) ;
206206 }
207207
208208 if ( timestampProperty . PropertyType == typeof ( string ) )
@@ -212,48 +212,48 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
212212 }
213213 }
214214
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 ( )
216216 {
217217 foreach ( var propertyInfo in properties )
218218 {
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 ) )
220220 {
221- propertyConverters [ propertyInfo . Name ] . SetObjectProperty ( o , entity . Properties [ propertyInfo . Name ] ) ;
221+ propertyConverters [ propertyInfo . Name ] . SetObjectProperty ( o , entity [ propertyInfo . Name ] ) ;
222222 }
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 ) )
224224 {
225- var val = entity . Properties [ propertyInfo . Name ] . PropertyAsObject ;
225+ var val = entity [ propertyInfo . Name ] ;
226226
227227 if ( val != null && ( propertyInfo . PropertyType == typeof ( DateTimeOffset ) || propertyInfo . PropertyType == typeof ( DateTimeOffset ? ) ) )
228228 {
229- val = entity . Properties [ propertyInfo . Name ] . DateTimeOffsetValue ;
229+ val = entity . GetDateTimeOffset ( propertyInfo . Name ) ;
230230 }
231231
232232 if ( val != null && propertyInfo . PropertyType == typeof ( double ) )
233233 {
234- val = entity . Properties [ propertyInfo . Name ] . DoubleValue ;
234+ val = entity . GetDouble ( propertyInfo . Name ) ;
235235 }
236236
237237 if ( val != null && propertyInfo . PropertyType == typeof ( int ) )
238238 {
239- val = entity . Properties [ propertyInfo . Name ] . Int32Value ;
239+ val = entity . GetInt32 ( propertyInfo . Name ) ;
240240 }
241241
242242 if ( val != null && propertyInfo . PropertyType == typeof ( long ) )
243243 {
244- val = entity . Properties [ propertyInfo . Name ] . Int64Value ;
244+ val = entity . GetInt64 ( propertyInfo . Name ) ;
245245 }
246246
247247 if ( val != null && propertyInfo . PropertyType == typeof ( Guid ) )
248248 {
249- val = entity . Properties [ propertyInfo . Name ] . GuidValue ;
249+ val = entity . GetGuid ( propertyInfo . Name ) ;
250250 }
251251
252252 propertyInfo . SetValue ( o , val ) ;
253253 }
254- else if ( entity . Properties . ContainsKey ( $ "{ propertyInfo . Name } Json") )
254+ else if ( entity . Keys . Contains ( $ "{ propertyInfo . Name } Json") )
255255 {
256- var val = entity . Properties [ $ "{ propertyInfo . Name } Json"] . StringValue ;
256+ var val = entity . GetString ( $ "{ propertyInfo . Name } Json") ;
257257 if ( val != null )
258258 {
259259 var propVal = JsonConvert . DeserializeObject ( val , propertyInfo . PropertyType , jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ;
@@ -263,15 +263,15 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
263263 }
264264 }
265265
266- private static DynamicTableEntity CreateTableEntity < T > ( object o , List < PropertyInfo > properties ,
266+ private static TableEntity CreateTableEntity < T > ( object o , List < PropertyInfo > properties ,
267267 string partitionKey , string rowKey , JsonSerializerSettings jsonSerializerSettings , PropertyConverters < T > propertyConverters )
268268 {
269- var entity = new DynamicTableEntity ( partitionKey , rowKey ) ;
269+ var entity = new TableEntity ( partitionKey , rowKey ) ;
270270 foreach ( var propertyInfo in properties )
271271 {
272272 var name = propertyInfo . Name ;
273273 var val = propertyInfo . GetValue ( o ) ;
274- EntityProperty entityProperty ;
274+ object entityProperty ;
275275 if ( propertyConverters != null && propertyConverters . ContainsKey ( name ) )
276276 {
277277 entityProperty = propertyConverters [ name ] . ToTableEntityProperty ( ( T ) o ) ;
@@ -281,50 +281,50 @@ private static DynamicTableEntity CreateTableEntity<T>(object o, List<PropertyIn
281281 switch ( val )
282282 {
283283 case int x :
284- entityProperty = new EntityProperty ( x ) ;
284+ entityProperty = x ;
285285 break ;
286286 case short x :
287- entityProperty = new EntityProperty ( x ) ;
287+ entityProperty = x ;
288288 break ;
289289 case byte x :
290- entityProperty = new EntityProperty ( x ) ;
290+ entityProperty = x ;
291291 break ;
292292 case string x :
293- entityProperty = new EntityProperty ( x ) ;
293+ entityProperty = x ;
294294 break ;
295295 case double x :
296- entityProperty = new EntityProperty ( x ) ;
296+ entityProperty = x ;
297297 break ;
298298 case DateTime x :
299- entityProperty = new EntityProperty ( x ) ;
299+ entityProperty = x ;
300300 break ;
301301 case DateTimeOffset x :
302- entityProperty = new EntityProperty ( x ) ;
302+ entityProperty = x ;
303303 break ;
304304 case bool x :
305- entityProperty = new EntityProperty ( x ) ;
305+ entityProperty = x ;
306306 break ;
307307 case byte [ ] x :
308- entityProperty = new EntityProperty ( x ) ;
308+ entityProperty = x ;
309309 break ;
310310 case long x :
311- entityProperty = new EntityProperty ( x ) ;
311+ entityProperty = x ;
312312 break ;
313313 case Guid x :
314- entityProperty = new EntityProperty ( x ) ;
314+ entityProperty = x ;
315315 break ;
316316 case null :
317- entityProperty = new EntityProperty ( ( int ? ) null ) ;
317+ entityProperty = null ;
318318 break ;
319319 default :
320320 name += "Json" ;
321- entityProperty = new EntityProperty ( JsonConvert . SerializeObject ( val ,
322- jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ) ;
321+ entityProperty = JsonConvert . SerializeObject ( val ,
322+ jsonSerializerSettings ?? _defaultJsonSerializerSettings ) ;
323323 break ;
324324 }
325325 }
326326
327- entity . Properties [ name ] = entityProperty ;
327+ entity [ name ] = entityProperty ;
328328 }
329329 return entity ;
330330 }
0 commit comments