/** * 使用指定的 聚合工厂 和 聚合 事件仓储 初始化 * * @param aggregateFactory The factory for new aggregate instances * @param AggregateEventStore The event store that holds the event streams for this repository */ public EventSourcingRepository( final AggregateFactory<T> aggregateFactory, AggregateEventStore AggregateEventStore) { super(aggregateFactory.getAggregateType()); Assert.notNull(AggregateEventStore, "AggregateEventStore may not be null"); this.aggregateFactory = aggregateFactory; this.AggregateEventStore = AggregateEventStore; }
/** * 根据聚合标识符和 指定 版本加载 聚合 * * @param aggregateIdentifier the identifier of the aggregate to load * @param expectedVersion The expected version of the loaded aggregate * @return the fully initialized aggregate */ @Override protected T doLoad(ID aggregateIdentifier, final Long expectedVersion) { AggregateEventStream events = null; AggregateEventStream originalStream = null; try { try { events = AggregateEventStore.readEvents(getTypeIdentifier(), aggregateIdentifier); } catch (EventStreamNotFoundException e) { throw new AggregateNotFoundException(aggregateIdentifier, "The aggregate was not found", e); } originalStream = events; for (EventStreamDecorator decorator : eventStreamDecorators) { events = decorator.decorateForRead(getTypeIdentifier(), aggregateIdentifier, events); } final T aggregate = aggregateFactory.createAggregate(aggregateIdentifier, events.peek()); List<AggregateEvent> unseenEvents = new ArrayList<AggregateEvent>(); aggregate.initializeState(new CapturingEventStream(events, unseenEvents, expectedVersion)); if (aggregate.isDeleted()) { throw new AggregateDeletedException(aggregateIdentifier); } CurrentUnitOfWork.get() .registerListener(new ConflictResolvingListener(aggregate, unseenEvents)); return aggregate; } finally { IOUtils.closeQuietlyIfCloseable(events); // if a decorator doesn't implement closeable, we still want to be sure we close the original // stream IOUtils.closeQuietlyIfCloseable(originalStream); } }
public String getTypeIdentifier() { if (aggregateFactory == null) { throw new IllegalStateException( "Either an aggregate factory must be configured (recommended), " + "or the getTypeIdentifier() method must be overridden."); } return aggregateFactory.getTypeIdentifier(); }
/** * 使用指定的 锁 机制 初始化 * * @param aggregateFactory The factory for new aggregate instances * @param AggregateEventStore The event store that holds the event streams for this repository * @param lockManager the locking strategy to apply to this repository */ public EventSourcingRepository( AggregateFactory<T> aggregateFactory, AggregateEventStore AggregateEventStore, LockManager lockManager) { super(aggregateFactory.getAggregateType(), lockManager); Assert.notNull(AggregateEventStore, "AggregateEventStore may not be null"); this.AggregateEventStore = AggregateEventStore; this.aggregateFactory = aggregateFactory; }