Exemplo n.º 1
0
        public async Task Given_instance_process_When_recovering_from_creation()
        {
            var aggregateFactory = new AggregateFactory();
            var processId        = Guid.NewGuid().ToString();

            var data         = aggregateFactory.Build <ProcessStateAggregate <SoftwareProgrammingState> >(processId);
            var process      = new SoftwareProgrammingProcess();
            var initialState = new SoftwareProgrammingState(processId, process.MakingCoffee.Name);

            var eventsToReplay = new DomainEvent[] { new ProcessManagerCreated <SoftwareProgrammingState>(initialState, processId) };

            data.ApplyEvents(eventsToReplay);

            var processManager = new SoftwareProgrammingProcess();

            //Try to transit process by message, available only in desired state
            var coffeMakeFailedEvent = new CoffeMakeFailedEvent(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            var dispatchedCommands   = await processManager.Transit(data.State, coffeMakeFailedEvent);

            //process_produce_commands_only_one_command()
            Assert.Equal(1, dispatchedCommands.Count);
            //Produced_command_has_right_person_id()
            var sleepCommand = dispatchedCommands.OfType <GoSleepCommand>().First();

            Assert.Equal(coffeMakeFailedEvent.ForPersonId, sleepCommand.PersonId);
            //Produced_command_has_right_sofa_id()
            Assert.Equal(data.State.SofaId, sleepCommand.SofaId);
            //process_produce_command_from_given_state()
            Assert.IsAssignableFrom <GoSleepCommand>(dispatchedCommands.FirstOrDefault());
        }
        /// <inheritdoc />
        public async Task <TEntity> GetAsync(object id)
        {
            var aggregateRoot = AggregateFactory.Build(typeof(TEntity));

            var snapshot = await _snapshotReader.ReadSnapshot(id, typeof(TEntity));

            if (snapshot != null)
            {
                aggregateRoot.RestoreSnapshot(snapshot);
            }

            var streamName = StreamNameResolver.Resolve(id, typeof(TEntity));

            var slice = GetEventSlice(_connection, streamName, aggregateRoot.CurrentVersion == -1 ? StreamPosition.Start : aggregateRoot.CurrentVersion + 1);

            if (slice.Status == SliceReadStatus.StreamDeleted || slice.Status == SliceReadStatus.StreamNotFound)
            {
                return(await Task.FromResult(default(TEntity)));
            }

            aggregateRoot.LoadFromHistory(slice.Events.ToDomainEvent());

            while (!slice.IsEndOfStream)
            {
                slice = GetEventSlice(_connection, streamName, slice.NextEventNumber);
                aggregateRoot.LoadFromHistory(slice.Events.ToDomainEvent());
            }

            _unitOfWork.Track(aggregateRoot);

            return((TEntity)aggregateRoot);
        }
Exemplo n.º 3
0
        public RepositoryMock MockGetById <T>(Guid id) where  T : AggregateBase
        {
            AggregateFactory factory = new AggregateFactory();

            AggregatesToGet.Add(id, factory.Build(typeof(T), id, null, null));

            return(this);
        }
Exemplo n.º 4
0
        public void ShouldCreateANewEmptyAggregate()
        {
            AggregateFactory aggregateFactory = new AggregateFactory();
            SimpleAggregate  aggregate        = new SimpleAggregate();
            IAggregate       rebuiltAggregate = aggregateFactory.Build(aggregate.GetType(), aggregate.Id, null);

            rebuiltAggregate.ShouldBeEquivalentTo(aggregate);
        }
Exemplo n.º 5
0
        public void ShouldThrowCtorMissingError()
        {
            AggregateFactory aggregateFactory = new AggregateFactory();

            Action act = () => aggregateFactory.Build(typeof(AggregateWithInsufficientCtor), Guid.NewGuid(), null);

            act.ShouldThrow <InvalidOperationException>().Where(ex => ex.Message == string.Format("Aggregate {0} cannot be created: no parameterless non-public constructor has been provided", typeof(AggregateWithInsufficientCtor).Name));
        }
        public void Aggregate_by_default_can_be_saved_as_IMemento_for_snapshot()
        {
            _aggregate   = new TestAggregate(1, Guid.NewGuid());
            var snapshot = ((IAggregate)_aggregate).GetSnapshot();
            var factory  = new AggregateFactory();

            _restoredAggregate = factory.Build <TestAggregate>(_aggregate.Id, snapshot);
        }
Exemplo n.º 7
0
        public void ShouldThrowRehydrateCtorMissingError()
        {
            AggregateFactory aggregateFactory = new AggregateFactory();
            Type             type             = typeof(AggregateWithInsufficientCtor);

            Action act = () => aggregateFactory.Build(type, Guid.NewGuid(), A.Fake <IMemento>());

            act.ShouldThrow <InvalidOperationException>().Where(ex => ex.Message == string.Format("Aggregate {0} cannot be created: no non-public constructor that accepts IMemento has been provided", type.Name));
        }
Exemplo n.º 8
0
        public void ShouldRehydrateTheAggregateFromSnapshot()
        {
            AggregateFactory aggregateFactory = new AggregateFactory();
            SimpleAggregate  aggregate        = new SimpleAggregate(Guid.NewGuid(), DateTime.Now);

            IAggregate rebuiltAggregate = aggregateFactory.Build(aggregate.GetType(), aggregate.Id, ((IMementoCreator)aggregate).CreateMemento());

            rebuiltAggregate.ShouldBeEquivalentTo(aggregate);
        }
Exemplo n.º 9
0
        public void ConstructorConventionTest()
        {
            var factory = new AggregateFactory();

            foreach (var type in AllAggregateTypes)
            {
                try
                {
                    factory.Build(type, Guid.NewGuid(), null);
                }
                catch (Exception ex)
                {
                    Assert.Fail($"Ошибка при построении агрегата {type.Name}: \r\n{ex}");
                }
            }
        }
        public void Aggregate_by_default_can_be_saved_as_IMemento_for_snapshot()
        {
            _aggregate   = new TestAggregate(1, Guid.NewGuid().ToString());
            var factory  = new AggregateFactory();
            var snapshot = factory.GetSnapshot(_aggregate);

            _restoredAggregate = factory.Build <TestAggregate>(_aggregate.Id, snapshot);

            // Restored_aggregate_is_not_null()
            Assert.NotNull(_restoredAggregate);
            // Ids_are_equal()
            Assert.Equal(_aggregate.Id, _restoredAggregate.Id);
            //Restored_aggregate_uncommitted_events_are_empty()
            Assert.Empty(((IAggregate)_restoredAggregate).GetUncommittedEvents());
            //Restored_aggregate_state_is_equal_to_origin()
            Assert.Equal(_aggregate.Value, _restoredAggregate.Value);
        }
        public void Given_instance_saga_When_recovering_from_creation()
        {
            var aggregateFactory = new AggregateFactory();
            var sagaId           = Guid.NewGuid();

            _data = aggregateFactory.Build <SagaDataAggregate <SoftwareProgrammingSagaData> >(sagaId);
            var saga         = new SoftwareProgrammingSaga();
            var initialState = new SoftwareProgrammingSagaData(saga.MakingCoffee.Name);

            var eventsToReplay = new DomainEvent[]
            {
                new SagaCreatedEvent <SoftwareProgrammingSagaData>(initialState, sagaId)
            };

            _data.ApplyEvents(eventsToReplay);

            _sagaInstance = SagaInstance.New(saga, _data);

            //Try to transit saga by message, available only in desired state
            _coffeMakeFailedEvent = new CoffeMakeFailedEvent(Guid.NewGuid(), Guid.NewGuid());
            _sagaInstance.Transit(_coffeMakeFailedEvent);
            _dispatchedCommands = _sagaInstance.CommandsToDispatch;
        }
Exemplo n.º 12
0
 public BuySubscriptionSaga Create(Guid id)
 {
     return(Create(_aggregateFactory.Build <BuySubscriptionSagaStateAggregate>(id)));
 }
 protected TAggregate CreateNewAggregate(IIdentity identity)
 {
     var aggregate = (TAggregate)AggregateFactory.Build(typeof(TAggregate), identity, null);
     SetupContext(aggregate);
     return aggregate;
 }