protected bool IsRepeatedCommand(IPartyCommand command, IEventStoreAggregateId eventStoreAggregateId, IPartyState state) { bool repeated = false; if (((IPartyStateProperties)state).Version > command.AggregateVersion) { var lastEvent = EventStore.GetEvent(typeof(IPartyEvent), eventStoreAggregateId, command.AggregateVersion); if (lastEvent != null && lastEvent.CommandId == command.CommandId) { repeated = true; } } return(repeated); }
protected virtual void Update(IPartyCommand c, Action <IPartyAggregate> action) { var aggregateId = c.AggregateId; var stateType = GetStateType(c); var state = StateRepository.Get(stateType, aggregateId, false); var aggregate = GetPartyAggregate(state); var eventStoreAggregateId = ToEventStoreAggregateId(aggregateId); var repeated = IsRepeatedCommand(c, eventStoreAggregateId, state); if (repeated) { return; } aggregate.ThrowOnInvalidStateTransition(c); action(aggregate); Persist(eventStoreAggregateId, aggregate, state); }
protected virtual Type GetStateType(IPartyCommand c) { Type clazz = null; //typeof(PartyState); string discriminatorVal = null; if (c is ICreateParty) { discriminatorVal = ((ICreateParty)c).PartyTypeId; } else if (c is IMergePatchParty) { discriminatorVal = ((IMergePatchParty)c).PartyTypeId; } else if (c is IDeleteParty) { discriminatorVal = ((IDeleteParty)c).PartyTypeId; } if (discriminatorVal != null) { switch (discriminatorVal) { case PartyTypeIds.Party: clazz = typeof(PartyState); break; case PartyTypeIds.Organization: clazz = typeof(OrganizationState); break; case PartyTypeIds.Person: clazz = typeof(PersonState); break; } } if (clazz == null) { throw new ArgumentException(String.Format("CANNOT find discriminator info from command: {0}", c)); } return(clazz); }
private static bool IsCommandCreate(IPartyCommand c) { return(c.Version == PartyState.VersionZero); }