Is it possible to create huge events in Event Sourcing?

We use event sources and create aggregates from the event stream. I have 2 units - A1 and A2. A1 is used as a template for creating A2. Size A1 can be quite large. The main idea of ​​Event Sourcing is to ensure that every change in the state of the application is committed to the event object. So, to save A2, we need to store a lot of information in the first event.

Is this situation common or created from a template not a good idea? Is there a better way to solve it?

+5
source share
2 answers

This will help more if you post more specific examples of your aggregates and events. In general, you can create more detailed events if that makes sense in your situation. Then instead of the 1-1 relationship between Command and Event, you will have a 1-N relationship that is fully consistent with the CQRS theory.

So let's give you an example:

CreateInvoice : Command
- InvoiceId
- Customer (10 fields)
- Address (5 more fields)
- InvoceLine[] (where each InvoiceLine also have 10 fields or so)
- Rest of 100 or so fields

InvoiceCreated : Event
- InvoiceId
- Customer (10 fields)
- Address (5 more fields)
- InvoceLine[] (where each InvoiceLine also have 10 fields or so)
- Total
- Rest of 100 or so fields

And in Command Handler:

void Handle(CreateInvoce cmd)
{
  var invoice = new Invoice(cmd.InvoiceId, cmd.Customer, cmd.Address, cmd.Lines ....)
  uow.Register(invoice);
}

where only one InvoceCreated event will be generated.

Instead, you can have more detailed events:

InvoiceCreated : Event
- InvoiceId
- Customer
- Address

InvoiceLineAdded
- InvoiceId
- Item
- Vat
- Subtotal
- Etc

Then in Command Handler:

void Handle(CreateInvoce cmd)
{
  var invoice = new Invoice(cmd.InvoiceId, cmd.Customer, cmd.Address);

  foreach (var line in cmd.Lines)
  {
      invoice.AddLine(line.Item, line.Quantity, line.Price, ...);
  }

  uow.Register(invoice);
}

Here ctor will raise the InvoiceCreated event, and the AddLine method will raise the InvoiceLineAdded event. You can then have events like InvoiceLineChanged / InvoiceLineRemoved that you can use with updates.

This will allow you to have more detailed events, while maintaining the issuance of coarser commands.

, / PoV.

P.S. , . /-. - . , -. , "" , , ;)?

, .

+3

: ?

, SRP. , .. . ​​.....

, , -. , . ( ) , , ? : : PhoneNumberChanged : PhoneNumberCorrected, PhoneNumberMigrated ( , ). . Ofc. , , , .

, . , , , ?

0

All Articles