private void Execute(CreateCustomer command) { var customer = _aggregateEventStore.GetAggregate <Customer>(command.Id); customer.Create(command.Id, command.Name); _aggregateEventStore.Save(customer); }
/// <summary> /// Save the specified <paramref name="context"/> changes for the given aggregate. /// </summary> /// <param name="aggregate">The current aggregate version for which the context applies.</param> /// <param name="context">The command context containing the aggregate changes to be applied.</param> public SaveResult Save(Aggregate aggregate, CommandContext context) { Verify.NotNull(aggregate, nameof(aggregate)); Verify.NotNull(context, nameof(context)); var copy = aggregate.Copy(); var aggregateType = aggregate.GetType(); var key = String.Concat(aggregateType.GetFullNameWithAssembly(), "-", aggregate.Id); using (var aggregateLock = new AggregateLock(aggregateType, aggregate.Id)) { aggregateLock.Aquire(); try { var result = aggregateStore.Save(copy, context); memoryCache.Set(key, copy, CreateCacheItemPolicy()); return(result); } catch (ConcurrencyException) { memoryCache.Remove(key); throw; } } }
/// <summary> /// Initializes a newly created aggregate instance. /// </summary> /// <typeparam name="TAggregate">The type of aggregate to initialize.</typeparam> /// <param name="aggregateStore">The aggregate repository to which the aggregate is to be saved.</param> /// <param name="aggregate">The newly created aggregate.</param> /// <param name="initializer">The aggregate initializer.</param> private static TAggregate Create <TAggregate>(this IStoreAggregates aggregateStore, TAggregate aggregate, Action <TAggregate> initializer) where TAggregate : Aggregate { var context = CommandContext.GetCurrent(); using (var createContext = new CommandContext(aggregate.Id, context.Headers, new CommandEnvelope(aggregate.Id, context.Command))) { initializer.Invoke(aggregate); return((TAggregate)aggregateStore.Save(aggregate, createContext).Aggregate); } }
/// <summary> /// Save the specified <paramref name="context"/> changes for the given aggregate. /// </summary> /// <param name="aggregate">The current aggregate version for which the context applies.</param> /// <param name="context">The command context containing the aggregate changes to be applied.</param> public SaveResult Save(Aggregate aggregate, CommandContext context) { InvokePreSaveHooks(aggregate, context); try { var result = aggregateStore.Save(aggregate, context); InvokePostSaveHooks(result.Aggregate, result.Commit, null); return(result); } catch (Exception ex) { InvokePostSaveHooks(aggregate, null, ex); throw; } }
/// <summary> /// Invokes the underlying <see cref="Aggregate"/> command handler method for <see cref="Command"/>. /// </summary> /// <param name="context">The current command context.</param> public void Handle(CommandContext context) { var command = context.Command; var aggregate = aggregateStore.Get(AggregateType, context.AggregateId); Log.Trace("Executing {0} command on aggregate {1}", command, aggregate); aggregate.VerifyCanHandleCommand(command); executor(aggregate, command); if (context.HasRaisedEvents) { aggregateStore.Save(aggregate, context); } else { Log.Warn("Executing {0} command on aggregate {1} raised no events", command, aggregate); } }