Domain models

The following definitions apply to classes for business rules. Notice that they are meant to be pure business objects, not bound to anything in particular. They aren't models of an ORM, or objects of an external framework, and so on. The application should work with these objects (or objects with the same criteria).

In each case, the dosctring documents the purpose of each class, according to the business rule:

from typing import Union

class DispatchedOrder:
"""An order that was just created and notified to start its delivery."""

status = "dispatched"

def __init__(self, when):
self._when = when

def message(self) -> dict:
return {
"status": self.status,
"msg": "Order was dispatched on {0}".format(
self._when.isoformat()
),
}


class OrderInTransit:
"""An order that is currently being sent to the customer."""

status = "in transit"

def __init__(self, current_location):
self._current_location = current_location

def message(self) -> dict:
return {
"status": self.status,
"msg": "The order is in progress (current location: {})".format(
self._current_location
),
}


class OrderDelivered:
"""An order that was already delivered to the customer."""

status = "delivered"

def __init__(self, delivered_at):
self._delivered_at = delivered_at

def message(self) -> dict:
return {
"status": self.status,
"msg": "Order delivered on {0}".format(
self._delivered_at.isoformat()
),
}


class DeliveryOrder:
def __init__(
self,
delivery_id: str,
status: Union[DispatchedOrder, OrderInTransit, OrderDelivered],
) -> None:
self._delivery_id = delivery_id
self._status = status

def message(self) -> dict:
return {"id": self._delivery_id, **self._status.message()}

From this code, we can already get an idea of what the application will look like—we want to have a DeliveryOrder object, which will have its own status (as an internal collaborator), and once we have that, we will call its message() method to return this information to the user.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.97.235