Ejemplo n.º 1
0
        public void ShouldThrowExceptionConflictEvents()
        {
            var rootId       = Guid.NewGuid();
            var eventBus     = new MemoryEventBus(new MemoryResolver());
            var appendOnly   = new MemoryAppendOnlyStore(eventBus);
            var eventStore   = new EventStore(appendOnly, eventBus);
            var snapShotRepo = new SnapshotRepository();
            var factory      = new AggregateFactory(eventStore, snapShotRepo);

            var productAggregate = factory.Create <ProductCatalogAggregate>(rootId);

            productAggregate.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Acer Aspire 3", "Notebook Acer Aspire 3 A315-53-348W Intel Core i3-6006U  RAM de 4GB HD de 1TB Tela de 15.6” HD Windows 10"));
            eventStore.AppendToStream <ProductCatalogAggregate>(productAggregate.Id, productAggregate.Version, productAggregate.Changes, productAggregate.DomainEvents.ToArray());

            var user1 = factory.Load <ProductCatalogAggregate>(rootId);
            var user2 = factory.Load <ProductCatalogAggregate>(rootId);


            user1.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Asus Vivobook", "Notebook Asus Vivobook X441B-CBA6A de 14 Con AMD A6-9225/4GB Ram/500GB HD/W10"));
            eventStore.AppendToStream <ProductCatalogAggregate>(user1.Id, user1.Version, user1.Changes, user1.DomainEvents.ToArray());

            user2.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook 2 em 1 Dell", "Notebook 2 em 1 Dell Inspiron i14-5481-M11F 8ª Geração Intel Core i3 4GB 128GB SSD 14' Touch Windows 10 Office 365 McAfe"));

            Assert.Throws <EventStoreConcurrencyException>(()
                                                           => eventStore.AppendToStream <ProductCatalogAggregate>(user2.Id, user2.Version, user2.Changes, user2.DomainEvents.ToArray())
                                                           );
        }
Ejemplo n.º 2
0
        public void ShouldCreateMultiplesSnapshots()
        {
            var rootId       = Guid.NewGuid();
            var eventBus     = new MemoryEventBus();
            var appendOnly   = new MemoryAppendOnlyStore(eventBus);
            var eventStore   = new EventStore(appendOnly);
            var snapShotRepo = new SnapshotRepository();
            var factory      = new AggregateFactory(eventStore, snapShotRepo);

            var productAggregate = factory.Create <ProductCatalogAggregate>(rootId);

            productAggregate.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Acer Aspire 3", "Notebook Acer Aspire 3 A315-53-348W Intel Core i3-6006U  RAM de 4GB HD de 1TB Tela de 15.6” HD Windows 10"));
            eventStore.AppendToStream <ProductCatalogAggregate>(productAggregate.Id, productAggregate.Version, productAggregate.Changes);
            var stream = eventStore.LoadEventStream(rootId);

            snapShotRepo.SaveSnapshot(rootId, productAggregate, stream.Version);

            productAggregate = factory.Load <ProductCatalogAggregate>(rootId);
            productAggregate.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Asus Vivobook", "Notebook Asus Vivobook X441B-CBA6A de 14 Con AMD A6-9225/4GB Ram/500GB HD/W10"));
            eventStore.AppendToStream <ProductCatalogAggregate>(productAggregate.Id, productAggregate.Version, productAggregate.Changes);
            stream = eventStore.LoadEventStream(rootId);
            snapShotRepo.SaveSnapshot(rootId, productAggregate, stream.Version);

            productAggregate = factory.Load <ProductCatalogAggregate>(rootId);
            productAggregate.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook 2 em 1 Dell", "Notebook 2 em 1 Dell Inspiron i14-5481-M11F 8ª Geração Intel Core i3 4GB 128GB SSD 14' Touch Windows 10 Office 365 McAfe"));
            eventStore.AppendToStream <ProductCatalogAggregate>(productAggregate.Id, productAggregate.Version, productAggregate.Changes);
            stream = eventStore.LoadEventStream(rootId);
            snapShotRepo.SaveSnapshot(rootId, productAggregate, stream.Version);

            productAggregate = factory.Load <ProductCatalogAggregate>(rootId);
            productAggregate.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Gamer Dell", "Notebook Gamer Dell G3-3590-M60P 9ª Geração Intel Core i7 8GB 512GB SSD Placa Vídeo NVIDIA 1660Ti 15.6' Windows 10"));
            eventStore.AppendToStream <ProductCatalogAggregate>(productAggregate.Id, productAggregate.Version, productAggregate.Changes);
            stream = eventStore.LoadEventStream(rootId);
            snapShotRepo.SaveSnapshot(rootId, productAggregate, stream.Version);

            productAggregate = factory.Load <ProductCatalogAggregate>(rootId);
            productAggregate.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Lenovo 2 em 1 ideapad C340", "Notebook Lenovo 2 em 1 ideapad C340 i7-8565U 8GB 256GB SSD Win10 14' FHD IPS - 81RL0001BR"));
            eventStore.AppendToStream <ProductCatalogAggregate>(productAggregate.Id, productAggregate.Version, productAggregate.Changes);
            stream = eventStore.LoadEventStream(rootId);
            snapShotRepo.SaveSnapshot(rootId, productAggregate, stream.Version);

            var aggregateReloadedFromSnapshot = factory.Load <ProductCatalogAggregate>(rootId);

            ProductCatalogAggregate root;
            long snapshotVersion = 0;
            var  snap            = snapShotRepo.TryGetSnapshotById(rootId, out root, out snapshotVersion);

            stream = eventStore.LoadEventStreamAfterVersion(rootId, snapshotVersion);

            Assert.True(snap);
            Assert.True(root != null);
            Assert.True(snapshotVersion == 6);
            Assert.True(stream.Events.Count == 0);
            Assert.True(aggregateReloadedFromSnapshot.Changes.Count == 0);
            Assert.True(aggregateReloadedFromSnapshot.Id == rootId);
            Assert.True(aggregateReloadedFromSnapshot.CountProducts() == 5);
        }
Ejemplo n.º 3
0
        public void ShouldIncreaseVersionCorrectly()
        {
            var eventBus   = new MemoryEventBus(new MemoryResolver());
            var appendOnly = new MemoryAppendOnlyStore(eventBus);
            var eventStore = new EventStore(appendOnly, eventBus);
            var rootId     = Guid.NewGuid();
            var factory    = new AggregateFactory(eventStore);
            var root       = factory.Create <ProductCatalogAggregate>(rootId);

            eventStore.AppendToStream <ProductCatalogAggregate>(root.Id, root.Version, root.Changes, root.DomainEvents.ToArray());

            root = factory.Load <ProductCatalogAggregate>(rootId);
            root.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook", "Dell Inspiron 15000"));
            root.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook Asus Vivobook", "Notebook Asus Vivobook X441B-CBA6A de 14 Con AMD A6-9225/4GB Ram/500GB HD/W10"));
            root.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook 2 em 1 Dell", "Notebook 2 em 1 Dell Inspiron i14-5481-M11F 8ª Geração Intel Core i3 4GB 128GB SSD 14' Touch Windows 10 Office 365 McAfe"));
            eventStore.AppendToStream <ProductCatalogAggregate>(root.Id, root.Version, root.Changes, root.DomainEvents.ToArray());

            root = factory.Load <ProductCatalogAggregate>(rootId);

            Assert.True(4 == root.Version);
        }
        public Result <object> When(CreateProductCommand cmd)
        {
            while (true)
            {
                var productCatalog = _factory.Load <ProductCatalogAggregate>(cmd.RootId);
                productCatalog.CreateProduct(cmd);

                try
                {
                    _eventStore.AppendToStream <ProductCatalogAggregate>(cmd.RootId, productCatalog.Version,
                                                                         productCatalog.Changes);
                    return(new Result <object>(null, new List <Exception>()));
                }
                catch (EventStoreConcurrencyException ex)
                {
                    HandleConcurrencyException(ex, productCatalog);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        public CommandEvent When(CreateProductCommand cmd)
        {
            var productCatalog = _factory.Load <ProductCatalogAggregate>(cmd.RootId);

            productCatalog.CreateProduct(cmd);

            try
            {
                _eventStore.AppendToStream <ProductCatalogAggregate>(cmd.RootId, productCatalog.Version,
                                                                     productCatalog.Changes, productCatalog.DomainEvents.ToArray());

                //_publisher.Publish(arry<IDomainEvent> event)
                return(new CommandEvent(OperationStatus.Success));
            }
            catch (EventStoreConcurrencyException ex)
            {
                HandleConcurrencyException(ex, productCatalog);
                return(new CommandEvent(OperationStatus.Success));
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 6
0
        public void ShouldAggregateLoadStream()
        {
            var rootId       = Guid.NewGuid();
            var queueService = new MemoryQueueService();
            var appendOnly   = new MemoryAppendOnlyStore(queueService);
            var eventStore   = new EventStore(appendOnly);
            var factory      = new AggregateFactory(eventStore);

            var rootToSave = factory.Create <ProductCatalogAggregate>(rootId);

            eventStore.AppendToStream <ProductCatalogAggregate>(rootToSave.Id, 1, rootToSave.Changes);

            var root = factory.Load <ProductCatalogAggregate>(rootId);

            Assert.True(0 == root.Changes.Count);
            Assert.Equal(rootId, root.Id);
        }
Ejemplo n.º 7
0
        public void ShouldAggregateLoadStream()
        {
            var rootId     = Guid.NewGuid();
            var eventBus   = new MemoryEventBus(new MemoryResolver());
            var appendOnly = new MemoryAppendOnlyStore(eventBus);
            var eventStore = new EventStore(appendOnly, eventBus);
            var factory    = new AggregateFactory(eventStore);

            var rootToSave = factory.Create <ProductCatalogAggregate>(rootId);

            eventStore.AppendToStream <ProductCatalogAggregate>(rootToSave.Id, 1, rootToSave.Changes, rootToSave.DomainEvents.ToArray());

            var root = factory.Load <ProductCatalogAggregate>(rootId);

            Assert.True(0 == root.Changes.Count);
            Assert.Equal(rootId, root.Id);
        }
Ejemplo n.º 8
0
        public void ShouldAddProductToProductCatalog()
        {
            var rootId       = Guid.NewGuid();
            var queueService = new MemoryQueueService();
            var appendOnly   = new MemoryAppendOnlyStore(queueService);
            var eventStore   = new EventStore(appendOnly);
            var factory      = new AggregateFactory(eventStore);
            var rootToSave   = factory.Create <ProductCatalogAggregate>(rootId);

            eventStore.AppendToStream <ProductCatalogAggregate>(rootToSave.Id, rootToSave.Version, rootToSave.Changes);

            var root = factory.Load <ProductCatalogAggregate>(rootId);

            root.CreateProduct(new CreateProductCommand(rootId, Guid.NewGuid(), "Notebook", "Dell Inspiron 15000"));

            Assert.True(root.Changes.Count == 1);
            Assert.True(root.Changes.ElementAt(0).GetType() == typeof(ProductCreated));
        }