2020 . AddStorageInterceptors ( )
2121 . UseGenericStorageInterceptor < Dictionary < string , string > > ( "SecretsStorage" , "secretsState" , c =>
2222 {
23- c . OnBeforeReadStateAsync = ( grainActivationContext , currentState ) =>
24- {
25- Console . WriteLine ( $ "OnBeforeReadState: { grainActivationContext . GrainIdentity . IdentityString } : Count Is { currentState . State . Count } ") ;
26- return ValueTask . FromResult ( true ) ;
27- } ;
28-
29- c . OnAfterReadStateFunc = ( grainActivationContext , currentState ) =>
30- {
31- Console . WriteLine ( $ "OnAfterReadState: { grainActivationContext . GrainIdentity . IdentityString } : Count Is { currentState . State . Count } ") ;
32-
33- // Do a deep copy
34- // Dictionary<string, string>? stateToModify = JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(currentState));
35-
36- foreach ( var ( key , value ) in currentState . State )
37- {
38- Console . WriteLine ( $ "Intercepted: { key } : { value } ") ;
39-
40- // Decrypt the data
41- currentState . State [ key ] = currentState . State [ key ] . Replace ( '3' , 'e' ) ;
42- }
43- return ValueTask . CompletedTask ;
44- } ;
4523
4624 c . OnBeforeWriteStateFunc = ( grainActivationContext , currentState ) =>
4725 {
26+ var unencryptedValues = new Dictionary < string , string > ( currentState . State . Count ) ;
4827 Console . WriteLine ( $ "OnBeforeWriteState: { grainActivationContext . GrainIdentity . IdentityString } : Count Is { currentState . State . Count } ") ;
4928 foreach ( var ( key , value ) in currentState . State )
5029 {
5130 Console . WriteLine ( $ "Intercepted: { key } : { value } ") ;
5231
32+ // Save the original state to return to the grain
33+ unencryptedValues . Add ( key , value ) ;
34+
5335 // Encrypt the data
5436 currentState . State [ key ] = currentState . State [ key ] . Replace ( 'e' , '3' ) ;
5537 }
56- return ValueTask . FromResult ( true ) ;
38+ return ValueTask . FromResult ( ( false , ( object ? ) unencryptedValues ) ) ;
5739 } ;
5840
59- c . OnAfterWriteStateFunc = ( grainActivationContext , currentState ) =>
41+ c . OnAfterWriteStateFunc = ( grainActivationContext , currentState , sharedState ) =>
6042 {
43+ var unencryptedValues = ( Dictionary < string , string > ) sharedState ! ;
6144 Console . WriteLine ( $ "OnAfterWriteState: { grainActivationContext . GrainIdentity . IdentityString } : Count Is { currentState . State . Count } ") ;
6245 foreach ( var ( key , value ) in currentState . State )
6346 {
6447 Console . WriteLine ( $ "What was actually persisted: { key } : { value } ") ;
48+
49+ currentState . State [ key ] = unencryptedValues [ key ] ;
50+ Console . WriteLine ( $ "What will be returned to grain: { key } : { unencryptedValues [ key ] } ") ;
6551 }
6652 return ValueTask . CompletedTask ;
6753 } ;
54+
55+ c . OnBeforeReadStateAsync = ( grainActivationContext , currentState ) =>
56+ {
57+ Console . WriteLine ( $ "OnBeforeReadState: { grainActivationContext . GrainIdentity . IdentityString } : Count Is { currentState . State . Count } ") ;
58+
59+ var unencryptedValues = new Dictionary < string , string > ( currentState . State . Count ) ;
60+ foreach ( var ( key , value ) in currentState . State )
61+ {
62+ Console . WriteLine ( $ "Unencrypted Values: { key } : { value } ") ;
63+
64+ // Save the original state to return to the grain
65+ unencryptedValues . Add ( key , value ) ;
66+ }
67+ return ValueTask . FromResult ( ( false , ( object ? ) unencryptedValues ) ) ;
68+ } ;
69+
70+ c . OnAfterReadStateFunc = ( grainActivationContext , currentState , sharedState ) =>
71+ {
72+ var unencryptedValues = ( Dictionary < string , string > ) sharedState ! ;
73+ if ( ! currentState . RecordExists )
74+ {
75+ return ValueTask . CompletedTask ;
76+ }
77+
78+ var list = sharedState as List < string > ;
79+ Console . WriteLine ( $ "OnAfterReadState: { grainActivationContext . GrainIdentity . IdentityString } : Count Is { currentState . State . Count } ") ;
80+
81+ foreach ( var ( key , value ) in currentState . State )
82+ {
83+ Console . WriteLine ( $ "Encrypted Values: { key } : { value } ") ;
84+
85+ // Decrypt the data
86+ currentState . State [ key ] = currentState . State [ key ] . Replace ( '3' , 'e' ) ;
87+ }
88+ return ValueTask . CompletedTask ;
89+ } ;
90+
6891 } ) )
6992 . Build ( ) ;
7093
81104var secretStore = grainFactory . GetGrain < ISecretStorageGrain > ( "friend" ) ;
82105
83106// Call the grain and print the result to the console
84- await secretStore . AddSecret ( "Password" , "Now you See the secrets and now they are seen as safe!" ) ;
107+ await secretStore . AddOrUpdateSecret ( "Password" , "Now you See the secrets and now they are seen as safe!" ) ;
85108
86109var result = await secretStore . GetSecret ( "Password" ) ;
87110
88- Console . WriteLine ( " \n \n {0} \n \n " , result ) ;
111+ Console . WriteLine ( $ "Original Value: { result } " ) ;
89112
113+ await secretStore . AddOrUpdateSecret ( "Password" , "Seeeeeeeeeecrets" ) ;
114+ result = await secretStore . GetSecret ( "Password" ) ;
115+
116+ Console . WriteLine ( $ "Updated Value: { result } ") ;
90117Console . ReadLine ( ) ;
91118
92119await host . StopAsync ( ) ;
@@ -98,18 +125,19 @@ internal class SecretStorageGrain : Grain, ISecretStorageGrain
98125 private readonly IPersistentState < Dictionary < string , string > > secrets ;
99126
100127 public SecretStorageGrain ( [ StorageInterceptor ( "SecretsStorage" , "secretsState" ) ] IPersistentState < Dictionary < string , string > > state ) => this . secrets = state ;
101- public async Task AddSecret ( string name , string value )
128+ public async Task AddOrUpdateSecret ( string name , string value )
102129 {
103- this . secrets . State . Add ( name , value ) ;
130+ this . secrets . State [ name ] = value ;
104131 await this . secrets . WriteStateAsync ( ) ;
105132 }
106133
134+
107135 public Task < string > GetSecret ( string name ) => Task . FromResult ( this . secrets . State [ name ] ) ;
108136 }
109137
110138 internal interface ISecretStorageGrain : IGrainWithStringKey
111139 {
112- Task AddSecret ( string name , string value ) ;
140+ Task AddOrUpdateSecret ( string name , string value ) ;
113141 Task < string > GetSecret ( string name ) ;
114142 }
115143}
0 commit comments