AxonFramework / AxonFramework

Framework for Evolutionary Message-Driven Microservices on the JVM
https://axoniq.io/
Apache License 2.0
3.25k stars 784 forks source link

Repository doesn't throw exception when storing wrong type of aggregate #11

Closed abuijze closed 7 years ago

abuijze commented 12 years ago

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.

alexbool commented 12 years ago

Why can't we obtain the generic Class<?> parameter in the constructor using refelction?. Thus no API changes are required.

abuijze commented 12 years ago

In Axon 2, the API's are changing anyway. So it's not really a problem.

It's possible to find out what aggregate should be stored using reflection on the generi parameters, but that's not full-proof.