Beispiel #1
0
        public VersionedRecord(byte[] versionInfoBytes, IMemoryManager memoryManager, ISnapshotData snapshotData)
        {
            if (versionInfoBytes == null)
            {
                throw new ArgumentNullException(nameof(versionInfoBytes));
            }

            if (memoryManager == null)
            {
                throw new ArgumentNullException(nameof(memoryManager));
            }

            if (snapshotData == null)
            {
                throw new ArgumentNullException(nameof(snapshotData));
            }

            using (var ms = new MemoryStream(versionInfoBytes))
            {
                while (ms.Position < ms.Length)
                {
                    _versions.Add(VersionInfo.Read(ms));
                }
            }
            _memoryManager = memoryManager;
            _snapshotData  = snapshotData;
        }
Beispiel #2
0
        public async Task <TAggregate> FetchAsync(Guid aggregateId)
        {
            var snapshot = await _context.Snapshots
                           .Find <SnapshotDao>(x => x.AggregateId == aggregateId)
                           .SortByDescending(x => x.Version)
                           .FirstOrDefaultAsync();

            int           version = -1;
            ISnapshotData data    = null;

            if (snapshot != null)
            {
                version = snapshot.Version;
                data    = snapshot.Data;
            }

            var events = await _context.Events
                         .Find <EventDao>(x => x.AggregateId == aggregateId && x.Version > version)
                         .ToListAsync();

            var domainEvents = events.Select(e => e.Data);
            var aggregate    = new TAggregate();

            aggregate.Initialize(data, domainEvents);
            return(aggregate);
        }
        public int CreateTransaction(out ISnapshotData snapshotData)
        {
            snapshotData = new SnapshotDataStub(_transactions);
            var tr = new DataTankerTransaction(this, snapshotData, _transactions.Count);

            _transactions.Add(tr);

            return(tr.Id);
        }
        public DataTankerTransaction(ITransactionInventory inventory, ISnapshotData snapshotData, int id)
        {
            if (id == int.MaxValue)
            {
                throw new DataTankerException("Transaction number overflow");
            }

            _inventory   = inventory;
            SnapshotData = snapshotData ?? throw new ArgumentNullException(nameof(snapshotData));
            Id           = id;

            State = TransactionState.Active;
        }
Beispiel #5
0
        public void Initialize(ISnapshotData snapshot, IEnumerable <IDomainEvent> changes)
        {
            if (changes.Count() == 0 && snapshot == null)
            {
                throw new InvalidOperationException("Aggregate does not exist");
            }

            if (snapshot != null)
            {
                Mutate(snapshot);
                Version++;
            }

            foreach (var change in changes)
            {
                Mutate(change);
                Version++;
            }
        }
Beispiel #6
0
 private void Mutate(ISnapshotData snapshot) => ((dynamic)this).On((dynamic)snapshot);