This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed
tests/ServiceStack.Redis.Tests Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ public RedisClient()
42
42
43
43
public static Func < RedisClient > NewFactoryFn = ( ) => new RedisClient ( ) ;
44
44
45
+ public static Func < object , Dictionary < string , string > > ConvertToHashFn =
46
+ x => x . ToJson ( ) . FromJson < Dictionary < string , string > > ( ) ;
47
+
45
48
/// <summary>
46
49
/// Creates a new instance of the Redis Client from NewFactoryFn.
47
50
/// </summary>
@@ -736,7 +739,8 @@ public T GetFromHash<T>(object id)
736
739
public void StoreAsHash < T > ( T entity )
737
740
{
738
741
var key = UrnKey ( entity ) ;
739
- SetRangeInHash ( key , entity . ToJson ( ) . FromJson < Dictionary < string , string > > ( ) ) ;
742
+ var hash = ConvertToHashFn ( entity ) ;
743
+ SetRangeInHash ( key , hash ) ;
740
744
RegisterTypeId ( entity ) ;
741
745
}
742
746
Original file line number Diff line number Diff line change 2
2
using System . Collections . Generic ;
3
3
using NUnit . Framework ;
4
4
using ServiceStack . Common ;
5
+ using ServiceStack . Common . Tests . Models ;
6
+ using ServiceStack . Redis . Tests . Generic ;
7
+ using ServiceStack . Text ;
5
8
6
9
namespace ServiceStack . Redis . Tests
7
10
{
@@ -268,6 +271,35 @@ public void Can_hash_multi_set_and_get()
268
271
}
269
272
}
270
273
271
- }
274
+ public class HashTest
275
+ {
276
+ public int Id { get ; set ; }
277
+ public string Name { get ; set ; }
278
+ }
279
+
280
+ [ Test ]
281
+ public void Can_store_as_Hash ( )
282
+ {
283
+ var dto = new HashTest { Id = 1 } ;
284
+ Redis . StoreAsHash ( dto ) ;
285
+
286
+ var storedHash = Redis . GetHashKeys ( dto . ToUrn ( ) ) ;
287
+ Assert . That ( storedHash , Is . EquivalentTo ( new [ ] { "Id" } ) ) ;
288
+
289
+ var hold = RedisClient . ConvertToHashFn ;
290
+ RedisClient . ConvertToHashFn = o =>
291
+ {
292
+ var map = new Dictionary < string , string > ( ) ;
293
+ o . ToObjectDictionary ( ) . Each ( x => map [ x . Key ] = ( x . Value ?? "" ) . ToJsv ( ) ) ;
294
+ return map ;
295
+ } ;
296
+
297
+ Redis . StoreAsHash ( dto ) ;
298
+ storedHash = Redis . GetHashKeys ( dto . ToUrn ( ) ) ;
299
+ Assert . That ( storedHash , Is . EquivalentTo ( new [ ] { "Id" , "Name" } ) ) ;
300
+
301
+ RedisClient . ConvertToHashFn = hold ;
302
+ }
303
+ }
272
304
273
305
}
You can’t perform that action at this time.
0 commit comments