public async Task Find_restores_aggregate_using_memento_if_found()
        {
            // Arrange
            var      user    = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            IMemento memento = user.SaveToMemento();

            user.ChangeUsername(username: Guid.NewGuid().ToString());

            IMementoStore mementoStore = Mock.Of <IMementoStore>();

            Mock.Get(mementoStore)
            .Setup(x => x.Find <FakeUser>(user.Id, default))
            .ReturnsAsync(memento);

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(user.Id, 1, default))
            .ReturnsAsync(user.FlushPendingEvents().Skip(1))
            .Verifiable();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                mementoStore,
                FakeUser.Factory,
                FakeUser.Factory);

            // Act
            FakeUser actual = await sut.Find(user.Id, default);

            // Assert
            Mock.Get(eventStore).Verify();
            actual.ShouldBeEquivalentTo(user);
        }
        public async Task Find_loads_events()
        {
            // Arrange
            var user = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());

            user.ChangeUsername(username: Guid.NewGuid().ToString());

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(user.Id, 0, default))
            .ReturnsAsync(user.FlushPendingEvents())
            .Verifiable();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                FakeUser.Factory);

            // Act
            await sut.Find(user.Id, default);

            // Assert
            Mock.Get(eventStore).Verify();
        }
        public async Task FindIdByUniqueIndexedProperty_relays_to_event_store()
        {
            // Arrange
            string name     = _fixture.Create <string>();
            string value    = _fixture.Create <string>();
            Guid?  expected = _fixture.Create <Guid?>();

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(
                x =>
                x.FindIdByUniqueIndexedProperty <FakeUser>(
                    name,
                    value,
                    CancellationToken.None))
            .ReturnsAsync(expected);

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                Mock.Of <Func <Guid, IEnumerable <IDomainEvent>, FakeUser> >());

            // Act
            Guid?actual = await sut.FindIdByUniqueIndexedProperty(name, value, CancellationToken.None);

            // Assert
            actual.Should().Be(expected);
        }
        public async Task SaveAndPublish_saves_events()
        {
            // Arrange
            var    user          = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            string operationId   = Guid.NewGuid().ToString();
            var    correlationId = Guid.NewGuid();
            string contributor   = Guid.NewGuid().ToString();

            user.ChangeUsername(username: Guid.NewGuid().ToString());
            var pendingEvents = new List <IDomainEvent>(user.PendingEvents);

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                FakeUser.Factory);

            // Act
            await sut.SaveAndPublish(user, operationId, correlationId, contributor);

            // Assert
            Mock.Get(eventStore).Verify(
                x =>
                x.SaveEvents <FakeUser>(pendingEvents, operationId, correlationId, contributor, default),
                Times.Once());
        }
예제 #5
0
 public static ISqlEventStore WithBulkCopyAppend(this ISqlEventStore eventStore)
 {
     if (eventStore is SqlEventStoreWithBulkCopyAppend)
     {
         return(eventStore);
     }
     return(new SqlEventStoreWithBulkCopyAppend(eventStore));
 }
예제 #6
0
 public SqlEventSourcedRepository(
     ISqlEventStore eventStore,
     ISqlEventPublisher eventPublisher,
     Func <Guid, IEnumerable <IDomainEvent>, T> entityFactory)
 {
     _eventStore     = eventStore ?? throw new ArgumentNullException(nameof(eventStore));
     _eventPublisher = eventPublisher ?? throw new ArgumentNullException(nameof(eventPublisher));
     _entityFactory  = entityFactory ?? throw new ArgumentNullException(nameof(entityFactory));
 }
예제 #7
0
 public SqlEventSourcedRepository(
     ISqlEventStore eventStore,
     ISqlEventPublisher eventPublisher,
     IMementoStore mementoStore,
     Func <Guid, IEnumerable <IDomainEvent>, T> entityFactory,
     Func <Guid, IMemento, IEnumerable <IDomainEvent>, T> mementoEntityFactory)
     : this(eventStore, eventPublisher, entityFactory)
 {
     _mementoStore         = mementoStore ?? throw new ArgumentNullException(nameof(mementoStore));
     _mementoEntityFactory = mementoEntityFactory ?? throw new ArgumentNullException(nameof(mementoEntityFactory));
 }
        public static Task <IEnumerable <IDomainEvent> > LoadEvents <T>(
            this ISqlEventStore eventStore,
            Guid sourceId)
            where T : class, IEventSourced
        {
            if (eventStore == null)
            {
                throw new ArgumentNullException(nameof(eventStore));
            }

            return(eventStore.LoadEvents <T>(sourceId, default(int), CancellationToken.None));
        }
        public static Task SaveEvents <T>(
            this ISqlEventStore eventStore,
            IEnumerable <IDomainEvent> events)
            where T : class, IEventSourced
        {
            if (eventStore == null)
            {
                throw new ArgumentNullException(nameof(eventStore));
            }

            return(eventStore.SaveEvents <T>(events, null, CancellationToken.None));
        }
 public void TestInitialize()
 {
     fixture        = new Fixture().Customize(new AutoMoqCustomization());
     eventStore     = Mock.Of <ISqlEventStore>();
     eventPublisher = Mock.Of <ISqlEventPublisher>();
     mementoStore   = Mock.Of <IMementoStore>();
     sut            = new SqlEventSourcedRepository <FakeUser>(
         eventStore,
         eventPublisher,
         mementoStore,
         FakeUser.Factory,
         FakeUser.Factory);
 }
        public static Task <Guid?> FindIdByUniqueIndexedProperty <T>(
            this ISqlEventStore eventStore,
            string name,
            string value)
            where T : class, IEventSourced
        {
            if (eventStore == null)
            {
                throw new ArgumentNullException(nameof(eventStore));
            }

            return(eventStore.FindIdByUniqueIndexedProperty <T>(name, value, CancellationToken.None));
        }
        public void SaveAndPublish_does_not_saves_memento_if_fails_to_save_events()
        {
            // Arrange
            var    user          = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            string operationId   = Guid.NewGuid().ToString();
            var    correlationId = Guid.NewGuid();
            string contributor   = Guid.NewGuid().ToString();

            ISqlEventStore eventStore   = Mock.Of <ISqlEventStore>();
            IMementoStore  mementoStore = Mock.Of <IMementoStore>();

            Mock.Get(eventStore)
            .Setup(
                x =>
                x.SaveEvents <FakeUser>(
                    It.IsAny <IEnumerable <IDomainEvent> >(),
                    operationId,
                    correlationId,
                    contributor,
                    default))
            .Throws <InvalidOperationException>();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                mementoStore,
                FakeUser.Factory,
                FakeUser.Factory);

            // Act
            Func <Task> action = () => sut.SaveAndPublish(user, operationId, correlationId, contributor);

            // Assert
            action.ShouldThrow <InvalidOperationException>();
            Mock.Get(mementoStore).Verify(
                x =>
                x.Save <FakeUser>(
                    user.Id,
                    It.IsAny <IMemento>(),
                    It.IsAny <CancellationToken>()),
                Times.Never());
        }
        public async Task Find_returns_null_if_no_event()
        {
            // Arrange
            var userId = Guid.NewGuid();

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(userId, 0, default))
            .ReturnsAsync(new IDomainEvent[0]);

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                FakeUser.Factory);

            // Act
            FakeUser actual = await sut.Find(userId, default);

            // Assert
            actual.Should().BeNull();
        }
        public async Task FindIdByUniqueIndexedProperty_relays_to_event_store()
        {
            // Arrange
            string name     = Guid.NewGuid().ToString();
            string value    = Guid.NewGuid().ToString();
            Guid?  expected = Guid.NewGuid();

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.FindIdByUniqueIndexedProperty <FakeUser>(name, value, default))
            .ReturnsAsync(expected);

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                FakeUser.Factory);

            // Act
            Guid?actual = await sut.FindIdByUniqueIndexedProperty(name, value, default);

            // Assert
            actual.Should().Be(expected);
        }
예제 #15
0
 public SqlEventStoreWithBulkCopyAppend(ISqlEventStore eventStore)
     : base(eventStore)
 {
     Database = eventStore.Database;
 }
예제 #16
0
 public SqlEventReceiver(ISqlEventStore eventStore)
     : this(eventStore, TimeSpan.FromMilliseconds(200))
 {
 }
예제 #17
0
 public SqlEventReceiver(ISqlEventStore eventStore, TimeSpan pollDelay)
 {
     _eventStore = eventStore;
     _pollDelay  = pollDelay;
 }