那样的网站,免费发布推广信息的平台,上海网站公司哪家好,购买天猫店铺去哪个平台ESGrain生命周期Ray中ESGrain继承自Grain扩展了Grain的生命周期。Grain的生命周期参加文档附录#xff1a;1-Grain生命周期-译注.mdESGrain重写了Grain的OnActivateAsync方法。ESGrain的初始化过程如下#xff1a;初始化ESGrain中的State调用ReadSnapshotAsync()读快照。如果… ESGrain生命周期Ray中ESGrain继承自Grain扩展了Grain的生命周期。Grain的生命周期参加文档附录1-Grain生命周期-译注.mdESGrain重写了Grain的OnActivateAsync方法。ESGrain的初始化过程如下初始化ESGrain中的State调用ReadSnapshotAsync()读快照。如果没有获得快照调用InitState()根据InitState()中代码初始化ESGrainInitState()是虚方法可以被具体的ESGrain重写以自定义初始化。读取事件库重放事件获得最新的State。小技巧在实际开发中可以重写InitState()在其中根据关系型数据库中的数据自定义state的初始化。使用StateESGrain的数据存储在State中当ESGrain被激活后State数据存储在内存中持久化会存储为快照。定义ESGrain时需要定义State实现IState接口序列化默认使用protocol bufferState类要添加protocol buffer特性。IState接口定义的是State的基础部分即示例中的base部分base之外的是当前actor需要的要存储的数据。示例代码 [ProtoContract(ImplicitFields ImplicitFields.AllFields)] public class AccountState : IStatestring { #region base public string StateId { get; set; } public uint Version { get; set; } public uint DoingVersion { get; set; } public DateTime VersionTime { get; set; } #endregion public decimal Balance { get; set; } }EventESGrain之间通过Event传递数据Event编写请参考Event编写.mdEventHandles使用ESGrain引发事件一般出于两种考虑1.传递数据到Handler2.修改State中的数据。修改ESGrain中的数据通过EventHandle中的代码实现。使用实现IEventHandle在Apply中实现定义要处理的事件。示例代码public class AccountEventHandle : IEventHandle{ public void Apply(object state, IEvent evt) { if (state is AccountState actorState) { switch (evt) { case AmountAddEvent value: AmountAddEventHandle(actorState, value); break; case AmountTransferEvent value: AmountTransferEventHandle(actorState, value); break; default: break; } } } private void AmountTransferEventHandle(AccountState state, AmountTransferEvent evt) { state.Balance evt.Balance; } private void AmountAddEventHandle(AccountState state, AmountAddEvent evt) { state.Balance evt.Balance; }}State中的数据存储在内存中大量的数据存在State中在某种角度可以将State看做内存数据库。Ray中修改State的数据要通过EventHandle实现只有一种方式。ESGrain种类Ray默认提供了MongoESGrain和SqlGrain两类。ESGrainK, S, W说明KStateId的类型。SESGrain的State。WMessageInfo。完整ESGrain示例编写ESGrain时明确RabbitPub。明确MongoStorage。继承MongoESGrain或SqlGrain。实现ESGrain接口。如果需要重写OnActivateAsync。编写感兴趣的Actor方法如果需要发送事件1.定义事件2.编写EventHandler。[RabbitMQ.RabbitPub(Account, account)][MongoStorage(Test, Account)]public sealed class Account : MongoESGrainString, AccountState, IGrains.MessageInfo, IAccount{ protected override string GrainId this.GetPrimaryKeyString(); static IEventHandle _eventHandle new AccountEventHandle(); protected override IEventHandle EventHandle _eventHandle; public override Task OnActivateAsync() { return base.OnActivateAsync(); } public Task Transfer(string toAccountId, decimal amount) { var evt new AmountTransferEvent(toAccountId, amount, this.State.Balance - amount); return RaiseEvent(evt).AsTask(); } public Task AddAmount(decimal amount, string uniqueId null) { var evt new AmountAddEvent(amount, this.State.Balance amount); return RaiseEvent(evt, uniqueId: uniqueId).AsTask(); } [AlwaysInterleave] public Taskdecimal GetBalance() { return Task.FromResult(this.State.Balance); }}ESRepGrainESGrain默认是主Actor当单个Actor压力过大时可以实现该actor的副本actor副本actor主要用来处理1.读的操作2.其他非写的异步操作。主actor与副本actor之间保持同步的机制主actor引发事件在CoreHandler里将消息传递给副本actor在副本actor里面重放该事件。主actor与副本actor持久化的是同一个快照库、事件库。也会从同一个库里激活。生命周期与主actor类似。使用与ESGrain类似对比如下ESGrainESRepGrain明确RabbitPub不需要明确MongoStorage明确MongoStorage继承MongoESGrain或SqlGrain继承MongoESRepGrain或SqlRepGrain实现ESGrain接口自定义的副本Actor接口如果需要重写OnActivateAsync如果需要重写OnActivateAsync编写感兴趣的Actor方法编写感兴趣的Actor方法如果需要发送事件1.定义事件2.编写EventHandler不会引发事件示例[MongoStorage(Test, Account)]public sealed class AccountRep : MongoESRepGrainString, AccountState, MessageInfo, IAccountRep{ protected override string GrainId this.GetPrimaryKeyString(); static IEventHandle _eventHandle new AccountEventHandle(); protected override IEventHandle EventHandle _eventHandle; public Taskdecimal GetBalance() { return Task.FromResult(this.State.Balance); }}原文地址:http://www.cnblogs.com/CharlesZHENG/p/8438057.html.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com