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 }); }
private Option <BySlugResult, Error> MapSlugResultByArticleOrError(Article entity) { try { var view = _mapper.Map <ArticleView>(entity); return(new BySlugResult { HasResult = true, View = view }.Some <BySlugResult, Error>()); } catch (Exception) { return(Option.None <BySlugResult, Error>( Error.Critical("Unexpected error occured when mapping the article to view."))); } }
public IFieldRenderingResult Render(object parameters = null) { var renderFieldArgs = new RenderFieldArgs() { Item = item, FieldName = fieldName }; if (parameters != null) { renderFieldArgs.ApplyParameters(parameters); } CorePipeline.Run(renderFieldPipeline, renderFieldArgs); var result = renderFieldArgs.Result; var beginResult = result?.FirstPart; var endResult = result?.LastPart; var renderingResult = new FieldRenderingResult(beginResult, endResult); RenderingResult = renderingResult.Some<IFieldRenderingResult>(); return renderingResult; }
private static long CalculateActualStreamLength(Option<Commit> lastCommitInStream) { return lastCommitInStream .Map(commit => commit.EventIndexInStreamStartsFrom + commit.EventIds.Count()) .ValueOr(0); }
private static Commit CreateCommit( Guid streamId, Option<Commit> lastCommitInStream, Option<Commit> lastCommitAllStreams, IEnumerable<EventContainer> identifiedEvents) { var indexInStream = lastCommitInStream .Map(commit => commit.IndexInStream + 1) .ValueOr(0); var indexInAllStreams = lastCommitAllStreams .Map(commit => commit.IndexInAllStreams + 1) .ValueOr(0); var eventsIds = identifiedEvents.Select(identifiedEvent => identifiedEvent.Id); var eventIndexInStream = lastCommitInStream .Map(commit => commit.EventIndexInStreamStartsFrom + commit.EventIds.Count()) .ValueOr(0); var eventIndexInAllStreams = lastCommitAllStreams .Map(commit => commit.EventIndexInAllStreamsStartsFrom + commit.EventIds.Count()) .ValueOr(0); return new Commit( streamId: streamId, indexInStream: indexInStream, indexInAllStreams: indexInAllStreams, eventIndexInStreamStartsFrom: eventIndexInStream, eventIndexInAllStreamsStartsFrom: eventIndexInAllStreams, eventIds: eventsIds); }
/// <summary> /// Empties an optional if the value is null. /// </summary> /// <returns>The filtered optional.</returns> public Option <T> NotNull() => hasValue && value == null?Option.None <T>() : this;
/// <summary> /// Empties an optional if a specified condition /// is not satisfied. /// </summary> /// <param name="condition">The condition.</param> /// <returns>The filtered optional.</returns> public Option <T> Filter(bool condition) => hasValue && !condition?Option.None <T>() : this;
/// <summary> /// Uses an alternative optional, if no existing value is present. /// </summary> /// <param name="alternativeOption">The alternative optional.</param> /// <returns>The alternative optional, if no value is present, otherwise itself.</returns> public Option <T> Else(Option <T> alternativeOption) => hasValue ? this : alternativeOption;
/// <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);