Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.modern;
import dk.cloudcreate.essentials.components.eventsourced.aggregates.*;
import dk.cloudcreate.essentials.components.eventsourced.aggregates.snapshot.AggregateSnapshot;
import dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.*;
import dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.classic.Event;
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.EventStore;
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateEventStream;
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrder;
import dk.cloudcreate.essentials.shared.reflection.Reflector;
import dk.cloudcreate.essentials.shared.reflection.invocation.*;
import dk.cloudcreate.essentials.shared.types.GenericType;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import static dk.cloudcreate.essentials.shared.FailFast.requireNonNull;
import static dk.cloudcreate.essentials.shared.MessageFormatter.msg;
/**
* A modern opinionated interpretation of the classic {@link dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.classic.AggregateRoot} design, where the {@link Event}'s are mutable.
* The modern interpretation doesn't specify any requirement on the design of the Events, they can be Java 17+ records or simple POJO's.
*
*
* Note: The {@link AggregateRoot} works best in combination with the {@link StatefulAggregateRepository} that's configured to use the {@link StatefulAggregateInstanceFactory#reflectionBasedAggregateRootFactory()},
* because the {@link AggregateRoot} needs to be provided its aggregated id through the {@link AggregateRoot#AggregateRoot(Object)} constructor!
*
*
* The modern {@link AggregateRoot} supports keeping the state projection and {@link EventHandler} annotated methods within the {@link AggregateRoot} instance or within an {@link AggregateState} instance.
* If you wish to keep the state projection and {@link EventHandler} annotated methods within an {@link AggregateState} instance, then you only need to implement the {@link WithState} interface:
*
{@code
* public class Order extends AggregateRoot implements WithState {
* public Order(OrderId orderId,
* CustomerId orderingCustomerId,
* int orderNumber) {
* super(orderId);
* requireNonNull(orderingCustomerId, "You must provide an orderingCustomerId");
*
* apply(new OrderEvent.OrderAdded(orderId,
* orderingCustomerId,
* orderNumber));
* }
*
* public void addProduct(ProductId productId, int quantity) {
* requireNonNull(productId, "You must provide a productId");
* if (state(OrderState.class).accepted) {
* throw new IllegalStateException("Order is already accepted");
* }
* apply(new OrderEvent.ProductAddedToOrder(aggregateId(),
* productId,
* quantity));
* }
*
* public void accept() {
* if (state(OrderState.class).accepted) {
* return;
* }
* apply(eventOrder -> new OrderEvent.OrderAccepted(aggregateId(),
* eventOrder));
* }
* }
* }
*
* @param the type of id
* @param the type of event
* @param the aggregate type
*/
public abstract class AggregateRoot> implements StatefulAggregate {
private transient PatternMatchingMethodInvoker