internal EventSourcedEntityHandler(AnySupport anySupport, IEventSourcedContext context, IBehaviorResolver behaviorReflectionCache, Func <IEventSourcedEntityCreationContext, object> entityCreationFactory = null) { AnySupport = anySupport; Context = context; EntityCreationFactory = entityCreationFactory; BehaviorReflectionCache = behaviorReflectionCache; var explicitBehaviors = Option.None <object[]>(); var active = true; void Become(object[] behaviors) { // ReSharper disable once AccessToModifiedClosure if (!active) { throw new InvalidOperationException("Context is not active!"); } explicitBehaviors = Option.Some(ValidateBehaviors(behaviors).ToArray()); } var ctx = new EventSourcedEntityCreationContext(context, Become); var entity = EntityCreationFactory(ctx); active = false; CurrentBehaviors = explicitBehaviors.Match(behaviors => behaviors, () => new[] { entity }); }
/// <summary> /// Uses an alternative value, if no existing value is present. /// </summary> /// <param name="alternative">The alternative value.</param> /// <returns>A new optional, containing either the existing or alternative value.</returns> public Option <T> Or(T alternative) => hasValue ? this : Option.Some(alternative);
public static ValueTask <Option <T, TExceptionResult> > MapExceptionAsync <T, TException, TExceptionResult>(this Option <T, TException> option, Func <TException, ValueTask <TExceptionResult> > mapping) => option.MatchAsync( some: val => new ValueTask <Option <T, TExceptionResult> >(Option.Some <T, TExceptionResult>(val)), none: async err => Option.None <T, TExceptionResult>(await mapping(err)));