AggregateFactory design

Hi Allard

I explore axon and try to separate aggregate of event-handling logic from the main aggregate logic
Logica impl can be changed in future
I like annotations and use AbstractAnnotatedAggregateRoot
Snippet:

public class MyAggregate extends AbstractAnnotatedAggregateRoot {

@Transient
private MyAggregateLogica logica; //interface

public MyAggregate(MyAggregateLogica logica) {
this.logica = logica;
}

Structure:
MyAggregate
MyAggregateLogica
MyAggregateLogicaImpl //can changed in future

I try create it from factory
Snippet:

public class MyAggregateFactory extends GenericAggregateFactory {

private Class<?> aggregateLogica;

public MyAggregateFactory(Class<?> clazz) {
super(MyAggregate.class);
this.aggregateLogica = clazz;
}

@Override
protected MyAggregate doCreateAggregate(Object aggregateIdentifier, DomainEventMessage firstEvent) {
MyAggergateLogica myAggregateLogica = null;
try {
myAggregateLogica = (MyAggregateLogica) aggregateLogica.newInstance();
} catch …

MyAggregate aggregate = new MyAggregate(myAggregateLogica); //inject logica in aggregate
return aggregate;
}
}
I’m worried that thats new instances of logica no removed GC and memory out or it will work ok?

Hi Evgenij,

I don’t see any issues with this approach. The Logica class can be garbage collected as soon as the Aggregate is not used anymore.

Cheers,

Allard