Esempio n. 1
0
        public IEnumerable <AggregateCommit> Apply(AggregateCommit current)
        {
            if (ShouldApply(current))
            {
                var    urnRaw = Urn.Parse(Encoding.UTF8.GetString(current.AggregateRootId));
                var    urn    = AggregateUrn.Parse(urnRaw.Value);
                string currentAggregateName = urn.AggregateRootName;

                if (currentAggregateName == targetAggregateFoo)
                {
                    var fooBarId        = new FooBarId("1234", "elders");
                    var newFooBarEvents = new List <IEvent>();
                    foreach (IEvent @event in current.Events)
                    {
                        if (@event.GetType() == typeof(TestCreateEventFoo))
                        {
                            newFooBarEvents.Add(new TestCreateEventFooBar(fooBarId));
                        }
                        else if (@event.GetType() == typeof(TestUpdateEventFoo))
                        {
                            var theEvent = @event as TestUpdateEventFoo;
                            newFooBarEvents.Add(new TestUpdateEventFooBar(fooBarId, theEvent.UpdatedFieldValue));
                        }
                    }

                    HandleMaxRevision(fooBarId);
                    var aggregateCommitFooBar = new AggregateCommit(fooBarId.RawId, aggregateMaxRevision[fooBarId], newFooBarEvents);
                    yield return(aggregateCommitFooBar);
                }
                else
                {
                    var fooBarId        = new FooBarId("1234", "elders");
                    var newFooBarEvents = new List <IEvent>();
                    foreach (IEvent @event in current.Events)
                    {
                        if (@event.GetType() == typeof(TestCreateEventBar))
                        {
                            newFooBarEvents.Add(new TestCreateEventFooBar(fooBarId));
                        }
                        else if (@event.GetType() == typeof(TestUpdateEventBar))
                        {
                            var theEvent = @event as TestUpdateEventBar;
                            newFooBarEvents.Add(new TestUpdateEventFooBar(fooBarId, theEvent.UpdatedFieldValue));
                        }
                    }
                    HandleMaxRevision(fooBarId);
                    var aggregateCommitFooBar = new AggregateCommit(fooBarId.RawId, aggregateMaxRevision[fooBarId], newFooBarEvents);
                    yield return(aggregateCommitFooBar);
                }
            }
            else
            {
                yield return(current);
            }
        }
        public TestAggregateRootFooBar(FooBarId id)
        {
            var @event = new TestCreateEventFooBar(id);

            Apply(@event);
        }