Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * 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
   *
   *     http://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 org.axonframework.eventsourcing;
 
 
Aggregate factory that uses a convention to create instances of aggregates. The type must declare an accessible constructor accepting a org.axonframework.domain.AggregateIdentifier as single parameter. This constructor may not perform any initialization on the aggregate, other than setting the identifier.

If the constructor is not accessible (not public), and the JVM's security setting allow it, the GenericEventSourcingRepository will try to make it accessible.

Parameters:
<T> The type of aggregate this factory creates
Author(s):
Allard Buijze
Since:
0.7
 
 public class GenericAggregateFactory<T extends EventSourcedAggregateRootimplements AggregateFactory<T> {
 
     private final String aggregateType;
     private final Constructor<T> constructor;

    
Initialize the AggregateFactory for creating instances of the given aggregateType.

Parameters:
aggregateType The type of aggregate this factory creates instances of.
Throws:
IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security settings prevent the GenericEventSourcingRepository from calling the constructor.
 
     public GenericAggregateFactory(Class<T> aggregateType) {
         Assert.isTrue(EventSourcedAggregateRoot.class.isAssignableFrom(aggregateType),
                       "The given aggregateType must be a subtype of EventSourcedAggregateRoot");
         Assert.isFalse(Modifier.isAbstract(aggregateType.getModifiers()), "Given aggregateType may not be abstract");
         this. = aggregateType.getSimpleName();
         try {
             this. = aggregateType.getDeclaredConstructor(AggregateIdentifier.class);
             if (!.isAccessible()) {
                 .setAccessible(true);
             }
         } catch (NoSuchMethodException e) {
             throw new IncompatibleAggregateException(String.format(
                     "The aggregate [%s] does not have a suitable constructor. "
                             + "See Javadoc of GenericEventSourcingRepository for more information.",
                     aggregateType.getSimpleName()), e);
         }
     }

    

This implementation is AggregateSnapshot aware. If the first event is an AggregateSnapshot, the aggregate is retrieved from the snapshot, instead of creating a new -blank- instance.

Throws:
IncompatibleAggregateException if the aggregate constructor throws an exception, or if the JVM security settings prevent the GenericEventSourcingRepository from calling the constructor.
 
     @SuppressWarnings({"unchecked"})
     @Override
     public T createAggregate(AggregateIdentifier aggregateIdentifierDomainEvent firstEvent) {
         T aggregate;
         if (AggregateSnapshot.class.isInstance(firstEvent)) {
             aggregate = (T) ((AggregateSnapshotfirstEvent).getAggregate();
         } else {
             try {
                 aggregate = .newInstance(aggregateIdentifier);
             } catch (Exception e) {
                 throw new IncompatibleAggregateException(String.format(
                         "The constructor [%s] threw an exception".toString()), e);
             }
         }
         return aggregate;
     }
 
     @Override
     public String getTypeIdentifier() {
         return ;
    }
New to GrepCode? Check out our FAQ X