Пример #1
0
        public async Task <RillCommit> CommitAsync(IRillStore store, CancellationToken cancellationToken = default)
        {
            ThrowIfDisposed();

            _rillSubscription?.Dispose();
            _rillSubscription = null;

            await _sync.WaitAsync(cancellationToken);

            try
            {
                if (!TryDrainFor(TimeSpan.FromMilliseconds(250)))
                {
                    throw new InvalidOperationException("Can not commit when staged event count differs from total acked events (positive and negative).");
                }

                if (_stage.IsEmpty)
                {
                    throw new InvalidOperationException("Can not commit when no events has been intercepted.");
                }

                if (_nackCount > 0)
                {
                    throw new InvalidOperationException("Can not commit when there's knowledge about a failed event.");
                }

                var commit = RillCommit.New(
                    _rill.Reference,
                    _stage.ToImmutableList());

                await store.AppendAsync(
                    commit,
                    cancellationToken);

                _stage.Clear();
                _ackCount         = 0;
                _nackCount        = 0;
                _rillSubscription = Subscribe();

                return(commit);
            }
            finally
            {
                _sync.Release();
            }
        }
Пример #2
0
        private static async Task ShipOrderAsync(IRillStore orderStore, RillReference reference)
        {
            using var rill = RillFactory.Synchronous(reference);

            var view = new OrderView(rill);

            foreach (var c in orderStore.ReadCommits(reference))
            {
                rill.Emit(c);
            }

            using var transaction = RillTransaction.Begin(rill);

            rill.Emit(new OrderShipped(view.OrderNumber !, DateTime.UtcNow));

            view.Dump("After OrderShipped");

            var commit = await transaction.CommitAsync(orderStore);

            Console.WriteLine($"Committed {commit}");
        }
Пример #3
0
        private static async Task PlaceAndApproveOrderAsync(IRillStore orderStore, RillReference reference)
        {
            using var rill = RillFactory.Synchronous(reference);

            var view = new OrderView(rill);

            using var transaction = RillTransaction.Begin(rill);

            rill.Emit(new OrderPlaced(
                          "order#1",
                          "customer#1",
                          100M,
                          DateTime.UtcNow));

            view.Dump("After OrderPlaced");

            rill.Emit(new OrderApproved(view.OrderNumber !, DateTime.UtcNow));

            view.Dump("After OrderApproved");

            var commit = await transaction.CommitAsync(orderStore);

            Console.WriteLine($"Committed {commit}");
        }