quinta-feira, 10 de novembro de 2016

DDD - Rule of Thumbs #1 - Aggregate/Bounded Context Transaction Atomicity

Vou procurar listar aqui no blog algumas das Rule of Thumbs do DDD aos poucos. Hoje vou começar falando sobre Atomicidade dos Aggregates e Bounded Contexts.

Vaughn Vernon diz em seu livro "Implementing DDD":
"A properly designed Aggregate is one that can be modified in any way required by the business with its invariants completely consistent within a single transaction. And a properly designed Bounded Context modifies only one Aggregate instance per transaction in all cases. (...)
Limiting modification to one Aggregate instance per transaction may sound overly strict. However, it is a rule of thumb and should be the goal in most cases. It addresses the very reason to use Aggregates."
  O que aprendemos aqui de prático? Que...

[1] Se uma agregação precisa sofrer duas operações para manter consistência, então ela não está devidamente construída.

Por exemplo:

venda.AdicionarItem(novoItem);
venda.ValorTotal += novoItem.SubTotal;

No código acima, se ao adicionar o item o valor total não for computado internamente, nossa agregação entrará num estado inconsistente.

[2] Embora não seja o contexto, podemos concluir pela mesma lógica que um Application Service deve realizar uma única operação por Bounded Context, seja diretamente em uma agregação ou através de uma única chamada de serviço.

Nenhum comentário: