Beispiel #1
0
        public SaveBookPagesCommandAggregate(RepositoryContext context, BookPagesInputDto bookPages) : base(context)
        {
            RootEntity = new BookEntity
            {
                Id    = bookPages.Id,
                Title = bookPages.Title
            };

            Enqueue(
                new SaveEntityCommandOperation <BookEntity>(RootEntity)
                );

            if (bookPages.Pages?.Any() == true)
            {
                foreach (var page in bookPages.Pages)
                {
                    var pageEntity = new PageEntity
                    {
                        Id    = page.Id,
                        Index = page.Index
                    };

                    Pages.Add(pageEntity);

                    if (pageEntity.Id != null)
                    {
                        pageEntity.BookId = RootEntity.Id.Value;

                        Enqueue(
                            new UpdateEntityCommandOperation <PageEntity>(pageEntity)
                            );
                    }
                    else
                    {
                        Enqueue(
                            new AddLinkedEntityCommandOperation <BookEntity, PageEntity>(
                                RootEntity,
                                getLinkedEntity: () => pageEntity
                                )
                            );
                    }
                }
            }
        }
        public void Book_Aggregate_With_Pages_Book_Only_Tests()
        {
            var context = new RepositoryContext(connectionName);

            context.RegisterCommandRepositoryFactory <BookEntity>(() => new BookCommandRepository());

            context.RegisterCommandRepositoryFactory <PageEntity>(() => new PageCommandRepository());

            // Insert

            var inputDto = new BookPagesInputDto
            {
                Title = "Programming Java"
            };

            var saveAggregate = new SaveBookPagesCommandAggregate(context, inputDto);

            saveAggregate.Save();

            var bookEntity = saveAggregate.RootEntity;

            Assert.IsNotNull(bookEntity.Id);

            var bookId = bookEntity.Id;

            var pages = saveAggregate.Pages;

            Assert.AreEqual(0, pages.Count());

            // Read

            context.RegisterQueryRepository <BookEntity>(new BookQueryRepository());

            context.RegisterQueryRepository <PageEntity>(new PageQueryRepository());

            var bookQueryAggregate = new BookPagesQueryAggregate(context);

            bookQueryAggregate.Get(bookId);

            bookEntity = bookQueryAggregate.RootEntity;

            Assert.AreEqual(bookId, bookEntity.Id);

            Assert.AreEqual("Programming Java", bookEntity.Title);

            Assert.AreEqual(0, bookQueryAggregate.Pages.Count());

            // Update

            bookEntity = saveAggregate.RootEntity;

            bookEntity.Title = "Programming Java 2nd Ed.";

            saveAggregate.Save();

            // Read after update

            bookQueryAggregate.Get(bookId);

            bookEntity = bookQueryAggregate.RootEntity;

            Assert.AreEqual(bookId, bookEntity.Id);

            Assert.AreEqual("Programming Java 2nd Ed.", bookEntity.Title);

            Assert.AreEqual(0, bookQueryAggregate.Pages.Count());

            // Delete
            var deleteAggregate = new DeleteBookPagesCommandAggregate(context, bookId);

            deleteAggregate.Save();

            bookQueryAggregate.Get(bookId);

            Assert.IsNull(bookQueryAggregate.RootEntity);
        }