Ejemplo n.º 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}");
         }
     });
 }
Ejemplo n.º 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}]"
                 );
         }
     });
 }