public Task Handle(
            Envelope <CreateMovie> envelope,
            CancellationToken cancellationToken)
        {
            if (envelope == null)
            {
                throw new ArgumentNullException(nameof(envelope));
            }

            CreateMovie command = envelope.Message;
            var         movie   = new Movie(command.MovieId, command.Title);

            return(_movieRepository.SaveAndPublish(movie));
        }
        public void SaveAndPublish_with_Envelope_relays_correctly()
        {
            // Arrange
            var fixture = new Fixture();
            var factory = new MethodInvoker(new GreedyConstructorQuery());

            fixture.Customize <Envelope>(c => c.FromFactory(factory));
            IEnvelope         correlation                 = fixture.Create <Envelope>();
            FakeUser          source                      = fixture.Create <FakeUser>();
            CancellationToken cancellationToken           = new CancellationTokenSource().Token;
            IEventSourcedRepository <FakeUser> repository = Mock.Of <IEventSourcedRepository <FakeUser> >();

            // Act
            repository.SaveAndPublish(source, correlation, cancellationToken);

            // Assert
            Mock.Get(repository).Verify(
                x =>
                x.SaveAndPublish(
                    source,
                    correlation.OperationId,
                    correlation.MessageId,
                    correlation.Contributor,
                    cancellationToken),
                Times.Once());
        }
        public static Task SaveAndPublish <T>(
            this IEventSourcedRepository <T> repository,
            T source,
            IEnvelope correlation,
            CancellationToken cancellationToken = default)
            where T : class, IEventSourced
        {
            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

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

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

            return(repository.SaveAndPublish(
                       source,
                       correlation.OperationId,
                       correlation.MessageId,
                       correlation.Contributor,
                       cancellationToken));
        }
        public async Task AddScreening_command_handler_adds_Screening_correctly(
            Guid theaterId,
            string name,
            [Range(1, 20)] int seatRowCount,
            [Range(1, 20)] int seatColumnCount,
            Movie movie,
            IFixture builder)
        {
            // Arange
            IEventSourcedRepository <Theater>
                theaterRepository = GetRepository(Theater.Factory);
            var theater           = new Theater(
                theaterId, name, seatRowCount, seatColumnCount);
            await theaterRepository.SaveAndPublish(theater);

            IEventSourcedRepository <Movie>
            movieRepository = GetRepository(Movie.Factory);
            await movieRepository.SaveAndPublish(movie);

            var sut = new MovieCommandHandler(
                movieRepository, theaterRepository);

            AddScreening command = builder.Build <AddScreening>()
                                   .With(e => e.MovieId, movie.Id)
                                   .With(e => e.TheaterId, theaterId)
                                   .Create();

            // Act
            await sut.Handle(new Envelope(command));

            // Assert
            Movie actual = await movieRepository.Find(movie.Id);

            actual.Screenings
            .Should().Contain(s => s.Id == command.ScreeningId).Which
            .Should().BeEquivalentTo(new
            {
                Id = command.ScreeningId,
                command.TheaterId,
                Seats = from r in Enumerable.Range(0, seatRowCount)
                        from c in Enumerable.Range(0, seatColumnCount)
                        let isReserved = false
                                         select new Seat(row: r, column: c, isReserved),
                command.ScreeningTime,
                command.DefaultFee,
                command.ChildrenFee,
            });
        }
        public Task Handle(
            Envelope <CreateTheater> envelope,
            CancellationToken cancellationToken)
        {
            if (envelope == null)
            {
                throw new ArgumentNullException(nameof(envelope));
            }

            CreateTheater command = envelope.Message;

            var theater = new Theater(
                command.TheaterId,
                command.Name,
                command.SeatRowCount,
                command.SeatColumnCount);

            return(_repository.SaveAndPublish(theater));
        }