private void HandleMessage(CommandBusinessContext businessContext)
        {
            var message = businessContext.Message.Cmd;

            try
            {
                var call = _cache.GetOrAdd(
                    key: message.GetType(),
                    valueFactory: CreateHandleCommand);

                call(context, message);

                var unEvts = context.TrackedAggregateRoots.SelectMany(p => p.UncommittedEvents);
                businessContext.UncommittedEvents = unEvts.ToList();
                businessContext.Reply             = new ReplyMessage(message.Id)
                {
                    LastEventUTCTimestamp = unEvts?.Select(a => a.UTCTimestamp).LastOrDefault() ?? 0
                };

                foreach (var root in context.TrackedAggregateRoots)
                {
                    _contextCache.Set(root);
                }
            }
            catch (Exception ex)
            {
                //加入错误回复消息
                //清空上下文,移除缓存
                businessContext.Reply = new ReplyMessage(message.Id, ex);

                //上下文的聚合根中有事件的则说明被执行过,需要删除进行重建
                var clearRoots = context.TrackedAggregateRoots.Where(a => a.UncommittedEvents.Any());

                foreach (var root in clearRoots)
                {
                    _contextCache.Remove(root);
                }
            }
            finally
            {
                context.Clear();
            }
        }
        public override void Handle(CommandMessage[] messages, long endSequence)
        {
            CommandBusinessContext[] businessContexts = new CommandBusinessContext[messages.Length];

            for (int i = 0; i < messages.Length; i++)
            {
                CommandBusinessContext businessContext = new CommandBusinessContext();
                businessContext.Message = messages[i];
                businessContexts[i]     = businessContext;

                HandleMessage(businessContext);
            }
            //存储事件
            //发送消息到消息总线
            //通知回复消息

            SaveEvent(businessContexts);
            SendEvent(businessContexts);

            NoticeEvent(businessContexts);
        }