Example #1
0
        public async Task <Optional <TAggregateRoot> > GetAsync(string identifier)
        {
            Optional <Aggregate> existingAggregate = GetAggregateFromChangeTrackerIfExits(identifier);

            if (existingAggregate.HasValue)
            {
                return(new Optional <TAggregateRoot>((TAggregateRoot)existingAggregate.Value.Root));
            }

            var snapshotStreamName = _streamNameProvider.GetSnapshotStreamName(typeof(TAggregateRoot), identifier);

            Optional <Snapshot> snapshot = await _snapshotStore.GetLastSnapshot(snapshotStreamName);

            var version = StreamPosition.Start;

            if (snapshot.HasValue)
            {
                version = snapshot.Value.Version + 1;
            }

            var aggregate = await _aggregateStore.Load <TAggregateRoot>(identifier, version);

            AttachAggregateToChangeTracker(aggregate);

            return(new Optional <TAggregateRoot>((TAggregateRoot)aggregate.Root));
        }