public IEnumerable <AggregateCommit> Apply(AggregateCommit current) { if (ShouldApply(current)) { var urnRaw = Urn.Parse(Encoding.UTF8.GetString(current.AggregateRootId)); var urn = StringTenantUrn.Parse(urnRaw.Value); var fooId = new FooId(urn.Id, urn.Tenant); LoadFromEventStore(fooId); aggregateMaxRevision[fooId]++; var newFooEvents = new List <IEvent>(); foreach (IEvent @event in current.Events) { if (@event.GetType() == typeof(TestCreateEventBar)) { newFooEvents.Add(new TestCreateEventFoo(fooId)); } else if (@event.GetType() == typeof(TestUpdateEventBar)) { var theEvent = @event as TestUpdateEventBar; newFooEvents.Add(new TestUpdateEventFoo(fooId, theEvent.UpdatedFieldValue)); } } var aggregateCommitFooBar = new AggregateCommit(fooId.RawId, aggregateMaxRevision[fooId], newFooEvents); yield return(aggregateCommitFooBar); } else { yield return(current); } }
public IEnumerable <AggregateCommit> Apply(AggregateCommit current) { if (ShouldApply(current)) { var urnRaw = Urn.Parse(Encoding.UTF8.GetString(current.AggregateRootId)); var urn = StringTenantUrn.Parse(urnRaw.Value); string currentAggregateName = urn.ArName; 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 bool ShouldApply(AggregateCommit current) { var urnRaw = Urn.Parse(Encoding.UTF8.GetString(current.AggregateRootId)); var urn = StringTenantUrn.Parse(urnRaw.Value); string currentAggregateName = urn.ArName; if (currentAggregateName == targetAggregateName) { return(true); } return(false); }