示例#1
0
 public void HandleEvent(Any @event, IEventContext context)
 {
     Unwrap(() =>
     {
         var obj = AnySupport.Decode(@event);
         if (!CurrentBehaviors
             .Any(behavior => GetCachedBehaviorReflection(behavior)
                  .GetEventHandler(obj.GetType())
                  .Match(handler =>
         {
             var active = true;
             var ctx = new EventBehaviorContext(context, behaviors =>
             {
                 // ReSharper disable once AccessToModifiedClosure
                 if (!active)
                 {
                     throw new InvalidOperationException("Context is not active!");
                 }
                 CurrentBehaviors = ValidateBehaviors(behaviors).ToArray();
             });
             handler.Invoke(behavior, obj, ctx);
             active = false;
             return(true);
         },
                         () => false)
                  )
             )
         {
             throw new CloudStateException(
                 $"No event handler [{obj.GetType()}] found for any of the current behaviors: {BehaviorsString}");
         }
     });
 }
示例#2
0
 public void HandleSnapshot(Any anySnapshot, ISnapshotContext context)
 {
     Unwrap(() =>
     {
         var snapshot = AnySupport.Decode(anySnapshot);
         if (!CurrentBehaviors.Any(behavior => BehaviorReflectionCache.GetOrAdd(behavior.GetType())
                                   .GetSnapshotHandler(snapshot.GetType())
                                   .Match(handler =>
         {
             var active = true;
             var ctx = new SnapshotBehaviorContext(context, behaviors =>
             {
                 // TODO: Check sequence number override on this context is set correctly.
                 // ReSharper disable once AccessToModifiedClosure
                 if (!active)
                 {
                     throw new InvalidOperationException("Context is not active!");
                 }
                 CurrentBehaviors = ValidateBehaviors(behaviors).ToArray();
             });
             handler.Invoke(behavior, snapshot, ctx);
             active = false;
             return(true);
         }, () => false))
             )
         {
             throw new CloudStateException(
                 $"No snapshot handler found for snapshot [{snapshot.GetType()}] on any of the current behaviors [{BehaviorsString}]"
                 );
         }
     });
 }
示例#3
0
 public Option <Any> HandleCommand(Any command, ICommandContext context)
 {
     return(Unwrap(() =>
     {
         // TODO: Need to refine this to be clear on the potential exception resulting from the cascading commandhandler / result lookup
         var result = CurrentBehaviors
                      .Select(behavior => GetCachedBehaviorReflection(behavior)
                              .CommandHandlers[context.CommandName]
                              .Invoke(behavior, command, context))
                      .FirstOrDefault(x => x.HasValue);
         if (!result.HasValue)
         {
             throw new CloudStateException(
                 $"No command handler found for command [{context.CommandName}] on any of the " +
                 $"current behaviors: {BehaviorsString}"
                 );
         }
         return result;
     }));
 }