Skip to content

Instantly share code, notes, and snippets.

View mariobittencourt's full-sized avatar

Mario Bittencourt mariobittencourt

  • SSENSE
  • Montreal
View GitHub Profile
@mariobittencourt
mariobittencourt / Order.ts
Created July 16, 2020 00:18
Updated Domain Logic
class Order {
protected whenOrderCreated(event: OrderCreated): void {
// setting the other properies as before
// The event now contains the exchangeRate captured at the moment that the order was created
this._chargedAmount = event.amount * event.exchangeRate;
}
}
@mariobittencourt
mariobittencourt / Order.ts
Created July 16, 2020 00:16
Variable Domain logic
class Order {
protected whenOrderCreated(event: OrderCreated): void {
// setting the other properies as before
exchangeRate = exchangeRateToCurrency[event.currency];
this._chargedAmount = event.amount * exchangeRate;
}
}
@mariobittencourt
mariobittencourt / Order.ts
Last active September 8, 2020 11:04
Order with version converter
class Order {
// If it is the repository that is triggering the reconstruction it would use a version converter
protected whenOrderCreated(event: OrderCreated): void {
this._items = event.items;
this._status = OrderStatus.OPEN;
this._amount = event.amount;
this._destination = event.destination;
this._customerId = event.customerId;
@mariobittencourt
mariobittencourt / Order.ts
Created July 15, 2020 23:59
Order updated with the versioning conversion
class Order {
protected whenOrderCreated(event: OrderCreated): void {
this._items = event.items;
this._status = OrderStatus.OPEN;
this._amount = event.amount;
this._destination = event.destination;
this._customerId = event.customerId;
// New code - detect if we have the property first
if (event.hasOwnProperty('deliveryMethod') {
@mariobittencourt
mariobittencourt / Order.ts
Created July 15, 2020 23:47
Updated Order Created Apply
class Order {
protected whenOrderCreated(event: OrderCreated): void {
this._items = event.items;
this._status = OrderStatus.OPEN;
this._amount = event.amount;
this._destination = event.destination;
this._customerId = event.customerId;
// New code
this.deliveryMethod = event.deliveryMethod;
class DeclineProjector:
def __init__(self, projection: DeclineProjection):
self.projection = projection
@method_dispatch
def apply(self, event: DomainEvent):
pass
@apply.register(PaymentDeclined)
def _(self, event: PaymentDeclined):
@mariobittencourt
mariobittencourt / customer_payment_projection.py
Created June 17, 2020 02:13
Customer Payment Projection
class PaymentProjection:
def __init__(self, repository: PaymentViewRepository):
self.repository = repository
def boot(self) -> bool:
return self.repository.initialize()
def create_payment(self, payment_id: str, status: str, amount_due: float, last_updated_at: str):
payment_view = PaymentView(
payment_id=payment_id,
@mariobittencourt
mariobittencourt / customer_payment_projector.py
Created June 17, 2020 02:07
Customer Payment Projector
class PaymentProjector:
def __init__(self, projection: PaymentProjection):
self.projection = projection
@method_dispatch
def apply(self, event: DomainEvent):
raise ValueError('Unknown event!')
@apply.register(PaymentCreated)
def _(self, event: PaymentCreated):
class Projectionist:
def __init__(self, projector: PaymentProjector, stream_service: PaymentStreamService,
ledger_repository: LedgerRepository, ledger_name: str):
self.projector = projector
self.stream_service = stream_service
self.ledger_repository = ledger_repository
self.ledger = None
self.ledger_name = ledger_name
async def start(self):
@mariobittencourt
mariobittencourt / manage_customer_payment_projection.py
Last active June 17, 2020 01:56
customer payment projection
async def import_stream():
projectionist = PaymentProjectionist(
projector=projector, stream_service=stream_service,
ledger_repository=ledger_repository, ledger_name=PAYMENT_PROJECTION
)
await projectionist.start()