Last active
September 8, 2020 11:04
-
-
Save mariobittencourt/dedd4bd28860c13e5e1dc0fa076467b2 to your computer and use it in GitHub Desktop.
Order with version converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
// New code - No need to check as the event that has been received | |
this._deliveryMethod = event.deliveryMethod; | |
} | |
} | |
class OrderCreatedVersionConverter { | |
protected convertFromV1(eventData) { | |
eventData.deliveryMethod = 'Standard'; | |
return eventData; | |
} | |
convert(eventData): OrderCreated { | |
if (!eventData.hasOwnProperty('version')) { | |
return this.convertFromV1(eventData); | |
} | |
// As you add more versions you can convert from older versions until you have the latest one... | |
return eventData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment