@Override protected void prepareForCommit(LockAwareAggregate<T, A> aggregate) { Assert.state(aggregate.isLockHeld(), () -> "An aggregate is being used for which a lock is no longer held"); super.prepareForCommit(aggregate); }
/** * @throws AggregateNotFoundException if aggregate with given id cannot be found * @throws RuntimeException any exception thrown by implementing classes */ @Override public A load(String aggregateIdentifier, Long expectedVersion) { UnitOfWork<?> uow = CurrentUnitOfWork.get(); Map<String, A> aggregates = managedAggregates(uow); A aggregate = aggregates.computeIfAbsent(aggregateIdentifier, s -> doLoad(aggregateIdentifier, expectedVersion)); uow.onRollback(u -> aggregates.remove(aggregateIdentifier)); validateOnLoad(aggregate, expectedVersion); prepareForCommit(aggregate); return aggregate; }
@Override public A newInstance(Callable<T> factoryMethod) throws Exception { UnitOfWork<?> uow = CurrentUnitOfWork.get(); AtomicReference<A> aggregateReference = new AtomicReference<>(); // a constructor may apply events, and the persistence of an aggregate must take precedence over publishing its events. uow.onPrepareCommit(x -> prepareForCommit(aggregateReference.get())); A aggregate = doCreateNew(factoryMethod); aggregateReference.set(aggregate); Assert.isTrue(aggregateModel.entityClass().isAssignableFrom(aggregate.rootType()), () -> "Unsuitable aggregate for this repository: wrong type"); Map<String, A> aggregates = managedAggregates(uow); Assert.isTrue(aggregates.putIfAbsent(aggregate.identifierAsString(), aggregate) == null, () -> "The Unit of Work already has an Aggregate with the same identifier"); uow.onRollback(u -> aggregates.remove(aggregate.identifierAsString())); return aggregate; }