How do I load an aggregate object from db?

59 views
Skip to first unread message

Meric

unread,
May 6, 2020, 9:45:17 PM5/6/20
to Axon Framework Users
Hi guys.

I've had the question for a long time. For most samples on Internet. They always creat one aggregate object first and then operate the aggregate objects. My question is, how can I load one from db other than create one every time. I'll take e-sopping as an example. I treat one product as an aggregate object. I can't load all of them into my program memory. So how can I do?

What I do is, I write another constructor whit the parameter UpdateProductCommand as well as the constructor with parameter CreateProductCommand. In this constructor, I load it from db.Is this OK?


class Product{

    public Product(){}

    @CommandHandler
   public Product(CreateProductCommand command){
       apply(new CreateProductEvent(command.id));
   }

    @CommandHandler
   public Product(UpdateProductCommand command){
       load(command.id)
       ...
       apply(new UpdateProductEvent(command.id));
   }
}


ashwani tiwari

unread,
May 7, 2020, 5:29:27 AM5/7/20
to axonfr...@googlegroups.com
You don't have to call load in command handler on aggregate. Its automatically load by the Id you provided in memory. You need to provide only Id with annotations @TargetIdentifer in command.

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/7e1a7966-2525-47d1-8dba-d64fbb069379%40googlegroups.com.

Meric

unread,
May 7, 2020, 6:55:09 AM5/7/20
to Axon Framework Users
Oh, I think so too. But what if the id do not exist in the memory. How can I get the aggregate object?


在 2020年5月7日星期四 UTC+8下午5:29:27,ashwani tiwari写道:
You don't have to call load in command handler on aggregate. Its automatically load by the Id you provided in memory. You need to provide only Id with annotations @TargetIdentifer in command.

On Thu, 7 May 2020, 7:15 am Meric, <ahe...@gmail.com> wrote:
Hi guys.

I've had the question for a long time. For most samples on Internet. They always creat one aggregate object first and then operate the aggregate objects. My question is, how can I load one from db other than create one every time. I'll take e-sopping as an example. I treat one product as an aggregate object. I can't load all of them into my program memory. So how can I do?

What I do is, I write another constructor whit the parameter UpdateProductCommand as well as the constructor with parameter CreateProductCommand. In this constructor, I load it from db.Is this OK?


class Product{

    public Product(){}

    @CommandHandler
   public Product(CreateProductCommand command){
       apply(new CreateProductEvent(command.id));
   }

    @CommandHandler
   public Product(UpdateProductCommand command){
       load(command.id)
       ...
       apply(new UpdateProductEvent(command.id));
   }
}


--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonfr...@googlegroups.com.

ashwani tiwari

unread,
May 7, 2020, 7:11:28 AM5/7/20
to axonfr...@googlegroups.com
Why you want to load all the product aggregate in memory ?.

Since Product is your aggregate, and if it have no Id. Then it's not an aggregate.  Every aggregate must have a Id.


To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/780bf941-9e7d-4389-8d0d-08fdadd3cb22%40googlegroups.com.

Meric

unread,
May 8, 2020, 12:40:48 AM5/8/20
to Axon Framework Users
I do not want to load all of them into memory. I want to know how to load one from db. I've fond a way to do so. That is what GenericJpaRepository do. https://docs.axoniq.io/reference-guide/implementing-domain-logic/command-handling/state-stored-aggregates

But ~-~ I do not know how to extend the repository. Such as findProductByProductNameEquals. May you give me a light?

在 2020年5月7日星期四 UTC+8下午7:11:28,ashwani tiwari写道:

Allard Buijze

unread,
May 8, 2020, 5:18:45 AM5/8/20
to Axon Framework Users
Hi Meric,

the problem is that your Update command handler is a constructor. By specifying it on a constructor, you're basically saying that you want a new instance to be constructed. If you want to load an existing instance, then simply make that handler a regular instance method.

The GenericRepository is a repository for a command model. You never load an aggregate using anything else than the AggregateIdentifier. If you need to get the identifier based on some other information, you'll use a query model repository instead. This is the basic principle of CQRS.

Hope this helps.
Cheers,

Allard Buijze
CTO


To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/6deafc4e-20bf-4caa-acd5-4c4131c8fd45%40googlegroups.com.

Meric

unread,
May 8, 2020, 9:22:24 PM5/8/20
to Axon Framework Users
Hi Allard Buijze,

     Thanks a lot for your help. I learned a lot. I'm still a little confused. 

      Does CQRS mean that I must store all actions in the form of events or commands after the system is established in the data source?
  
      What iff I have data first and then use CQRS or EventSourcing, how do I take out the data and construct the aggregate root?

      I am getting more and more confused.

Thanks again.

在 2020年5月8日星期五 UTC+8下午5:18:45,Allard Buijze写道:

在 2020年5月8日星期五 UTC+8下午5:18:45,Allard Buijze写道:

Steven van Beelen

unread,
May 19, 2020, 4:10:27 AM5/19/20
to Axon Framework Users
Hi Meric,

CQRS doesn't mandate you to use events to recreate Command and Query Models, it is just a very good fit to employ Event Sourcing for the Command Model and an Event-Driven Architecture to create your Query Models.
If you already have an existing set of data and you want to migrate this to a CQRS pattern where the models are build from events, than you will have to write a migration tool to populate your event store by dispatching the right set of commands towards your command model.

Hope this clarifies things somewhat for your Meric!
Added, might help to check out AxonIQ's learning section here.
It explains all the terminology you might encounter when using Axon.

Cheers,
Steven

To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/77c95d27-7396-4f54-9f54-81ceac85b249%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages