AxonFramework/AxonFramework — Issue #11: Repository doesn't throw exception when storing wrong type of aggregate
Author: abuijze
Created: 2011-12-03 14:41:21+00:00
State: open
Labels: Enhancement

*From an email conversation:*
The symptoms were the following :

1. I instantiate a Foo object (Foo is an AR type). I add it to a
repository, and then load it back later --> ok, it works
2. I instantiate a Bar object (Bar is another AR type). I add it to a
repository, and then load it back later --> fails, I get a "Cannot
cast class Foo to Bar" !

When looking to Hibernate logs, I saw that the DomainEventEntry
corresponding to the creation of Bar actually had the aggregateType
"Foo" when being inserted in the db.

Ok, so one big headache later, I came to realize that I hadn't
declared the event sourcing repository for Bar in my Spring app
context.
So, instead of telling right to my face that I'm stupid, it looks like
Axon just tried to use another repository to store the event... which
worked. But, deserializing is another story :)

*Expected behavior*
Repositories should throw an exception when the type of aggregate is not assignable to the type they are configured to store.

*Implementation details*
The AbstractRepository should get a Class in its constructor that corresponds with the generic parameter of the Repository. This class can be used to verify the type of incoming aggregates.

Requires API change.