Example #1
0
        public void ExecuteDomainCreate(Guid aggregateId, Action <T> action)
        {
            var aggregate = AggregateRootFactory.Build <T>(aggregateId);

            action(aggregate);
            _repository.Save(aggregate);
        }
Example #2
0
        protected override void Given()
        {
            var aggregate = AggregateRootFactory.Create <AccountAggregateRoot>();

            aggregate.InitialiseAccount(AggregateId, "account-id");
            aggregate.AddAmount(2.00M);
            Subject.Save(aggregate);
        }
Example #3
0
        protected Investor RecreateInvestor()
        {
            var aggregateFactory = new AggregateRootFactory();

            var events = GetEventDatas().OrderBy(e => e.Version).Select(x => x.DeserializeEvent());

            return((Investor)aggregateFactory.CreateAsync <Investor>(events));
        }
Example #4
0
        public T GetById(Guid aggregateId)
        {
            var t      = AggregateRootFactory.Build <T>(aggregateId);
            var events = _storage.GetEventsForAggregate <T>(aggregateId);

            t.LoadFromEvent(events);
            return(t);
        }
Example #5
0
        public virtual async Task <TAggregateRoot> GetAsync(Guid id)
        {
            var aggregate = Aggregates.FirstOrDefault(x => x.Id == id);

            if (aggregate == null)
            {
                var eventStore = DbContextFactory.GetEventStore();
                if (eventStore == null)
                {
                    return(null);
                }
                else
                {
                    var @event = await eventStore.GetLastEventAsync(id);

                    if (@event.EventType != DeletedEvent.Type.FullName)
                    {
                        aggregate = AggregateRootFactory.CreateAggregate <TAggregateRoot>();
                        var events = await eventStore.GetEventsAsync(id, 0);

                        aggregate.LoadFromHistory(events.Select(e => e.ToAggregateEvent()).ToArray());
                        await Aggregates.AddAsync(aggregate);

                        return(aggregate);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            else
            {
                var eventStore = DbContextFactory.GetEventStore();
                if (eventStore == null)
                {
                    return(aggregate);
                }
                else
                {
                    var events = await eventStore.GetEventsAsync(aggregate.Id, aggregate.Version);

                    if (events.Any() && events.First().Version != aggregate.Version + 1)
                    {
                        // TODO: data is dirty
                        throw new MSFrameworkException(
                                  $"Entity {typeof(TAggregateRoot)} Id {id} is not match in event store and can't auto rebuild");
                    }
                    else
                    {
                        aggregate.LoadFromHistory(events.Select(e => e.ToAggregateEvent()).ToArray());
                        return(aggregate);
                    }
                }
            }
        }
Example #6
0
        protected override void Given()
        {
            ReloadedAccountAggregateRoot = AggregateRootFactory.Create <NonDisposingAccountAggregateRoot>();
            ReloadedAccountAggregateRoot.InitialiseAccount(AggregateId, "account-id");
            ReloadedAccountAggregateRoot.AddAmount(2.00M);

            // intermediate save should cause a concurrency error when we save below
            Subject.Save(ReloadedAccountAggregateRoot);

            ReloadedAccountAggregateRoot.AddAmount(5.00M);
        }
Example #7
0
        private async Task <TAggregate> LoadAggregate <TAggregate>(Guid id)
            where TAggregate : AggregateRoot
        {
            var events = await _eventStorage.Get(id, -1, typeof(TAggregate).GetCollectionName());

            if (!events.Any())
            {
                throw new AggregateNotFoundException(id);
            }
            var aggregate = AggregateRootFactory <TAggregate> .CreateAggregate(id);

            aggregate.LoadFromHistory(events);
            return(aggregate);
        }
Example #8
0
        public void CanReconstitute()
        {
            // arrange
            var memento = (object)null;
            var events  = new[] { new SomethingHappened() };
            var factory = new AggregateRootFactory();

            // act
            var aggregateRoot = factory.Create <PersistedAggregate>(memento, 0, events, "state");

            aggregateRoot.MakeSomethingHappen();

            // assert
            aggregateRoot.ThingsThatHappened.Should().HaveCount(2);
        }
Example #9
0
        public virtual TAggregateRoot Get(TAggregateRootId id)
        {
            var eventStore = DbContextFactory.GetEventStore();
            var aggregate  = Aggregates.FirstOrDefault(x => x.Id.Equals(id));

            if (eventStore == null)
            {
                return(aggregate);
            }
            else
            {
                var idAsString = id.ToString();
                if (aggregate == null)
                {
                    var @event = eventStore.GetLastEvent(idAsString);
                    if (@event.EventType != typeof(DeletedEvent <TAggregateRoot, TAggregateRootId>).FullName)
                    {
                        aggregate = AggregateRootFactory.CreateAggregate <TAggregateRoot>();
                        var events = eventStore.GetEvents(idAsString, 0);
                        aggregate.LoadFromHistory(events.Select(e => e.ToAggregateEvent()).ToArray());
                        Aggregates.Add(aggregate);
                        return(aggregate);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    var events = eventStore.GetEvents(idAsString, aggregate.Version);
                    if (events.Any() && events.First().Version != aggregate.Version + 1)
                    {
                        // TODO: data is dirty
                        throw new MSFrameworkException(
                                  $"Entity {typeof(TAggregateRoot)} Id {id} is not match in event store and can't auto rebuild");
                    }
                    else
                    {
                        aggregate.LoadFromHistory(events.Select(e => e.ToAggregateEvent()).ToArray());
                        return(aggregate);
                    }
                }
            }
        }
Example #10
0
 /// <summary>
 /// Factory method to create a post
 /// </summary>
 /// <param name="factory">The factory to be used for AggregateRootCreation</param>
 /// <param name="title"></param>
 /// <param name="textContent"></param>
 /// <returns></returns>
 public static Post CreatePost(
     AggregateRootFactory factory,
     String title,
     String textContent,
     String excerpt,
     String blogName)
 {
     //Slug is created replacing any non number or letter char with a dash
     //accents are removed
     String slug = title.Slugify();
     if (String.IsNullOrEmpty(excerpt))
     {
         //Need to calculate the excerpt of the post, in this version just take some text from the
         //body of the post.
         HtmlDocument doc = new HtmlDocument();
         doc.LoadHtml(textContent);
         excerpt = doc.DocumentNode.InnerText;
         if (excerpt.Length > 200) excerpt = excerpt.Substring(0, 200) + "...";
     }
     var evt = new PostCreated(title, textContent, slug, blogName, excerpt);
     return factory.Create<Post>(evt);
 }
Example #11
0
        public async Task <T> Get <T>(Guid aggregateId)
            where T : AggregateRoot
        {
            var aggregate = AggregateRootFactory <T> .CreateAggregate(aggregateId);

            var snapshotVersion = await TryRestoreAggregateFromSnapshot(aggregateId, aggregate);


            IEnumerable <Event> events;

            if (snapshotVersion == -1)
            {
                events = await _eventStorage.Get(aggregateId, -1, typeof(T).GetCollectionName());

                if (!events.Any())
                {
                    throw new AggregateNotFoundException(aggregateId);
                }
            }
            else
            {
                events = await _eventStorage.Get(aggregateId, snapshotVersion, typeof(T).GetCollectionName());
            }

            if (events.Any())
            {
                aggregate.LoadFromHistory(events);
            }


            if (aggregate.Removed)
            {
                throw new AggregateRemovedException(aggregate.Id);
            }

            return(aggregate);
        }
Example #12
0
 public ReplyCommandHandler(AggregateRootFactory factory)
 {
     _factory = factory;
 }
Example #13
0
 public AccountCommandHandler(AggregateRootFactory factory, ILockService lockService, RegisterAccountIndexService registerAccountIndexService)
 {
     _factory     = factory;
     _lockService = lockService;
     _registerAccountIndexService = registerAccountIndexService;
 }
Example #14
0
 public SectionCommandHandler(AggregateRootFactory factory)
 {
     _factory = factory;
 }
Example #15
0
 public PostCommandHandler(AggregateRootFactory factory)
 {
     _factory = factory;
 }
 public static CategoryManager Create(AggregateRootFactory factory, String id)
 {
     return factory.Create<CategoryManager>();
 }