How do I load an aggregate object from db?

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 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.

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写道:

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.

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写道:

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,

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 if****f 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写道:

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

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写道:

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.

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.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/7e1a7966-2525-47d1-8dba-d64fbb069379%40googlegroups.com.


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.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/780bf941-9e7d-4389-8d0d-08fdadd3cb22%40googlegroups.com.


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.
To view this discussion on the web visit https://groups.google.com/d/msgid/axonframework/6deafc4e-20bf-4caa-acd5-4c4131c8fd45%40googlegroups.com.

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

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