Next Dian a simple directory structure and project dependencies as
Dian two main core custom code
1. Add custom implementation class CustomProvider
public class CustomProvider : IGrainStorage { public Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState) { throw new NotImplementedException(); } public Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState) { if(grainType== "Grain.UserGrain") { var id = grainReference.GetPrimaryKeyLong(); //模拟从数据库读出User var user = new UserState() { MobilePhone = "124214214", Name = "李四" }; grainState.State = user; } return Task.CompletedTask; } public Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState) { if (grainType== "Grain.UserGrain") { var id =grainReference.GetPrimaryKeyLong (); // analog save the database } return Task.CompletedTask; } }
2. In place of the default implementation injected silo
.ConfigureServices(services => { var providerName = "CustomProvider"; services.TryAddSingleton(sp => sp.GetServiceByName<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME)); services.AddSingletonNamedService<IGrainStorage, CustomProvider>(providerName); })
3. Enable on grain class
[StorageProvider(ProviderName = "CustomProvider")]
Two Dian results are as follows
Download sample code: SimpleStorage