Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Domain: Factories should return collections of DTOs, not a single DTO #61

Open
lhaze opened this issue Aug 30, 2019 · 1 comment
Open

Comments

@lhaze
Copy link
Collaborator

lhaze commented Aug 30, 2019

@lhaze lhaze added this to the 0.1 milestone Aug 30, 2019
@lhaze lhaze added this to To do in Domain Tier via automation Aug 30, 2019
@lhaze
Copy link
Collaborator Author

lhaze commented Aug 30, 2019

Let's look into a somewhat common use-case: an entity has a list of other instances as its components. For example, let's take an Invoice entity with a list of InvoiceFields. Elements of this list can be modeled with a ValueObject model (an "inline" submodel, represented by PostgreSQL JSONB column) or with a sub-entity (represented by a separate table and attached to an Invoice by a foreign key). Either way, both InvoiceRepository and Invoice should be unaware of the choice of modeling type.

class Invoice(Entity):
    fields: t.List['InvoiceField'] = related(field='invoice', nullable=False)


class InvoiceField(Entity):  # or a ValueObject
    invoice: Invoice
    quantity: int
    product: Product

Next, there's a Factory that models the complexity of the creation of an Invoice instance. Let's use MarshmallowFactory for that, and we won't use any implicit field generation.

class InvoiceFactory(MarshmallowFactory):
    class InvoiceSchema(marshmallow.Schema):
        fields = fields.Nested("InvoiceFieldSchema", many=True, exclude=("invoice",))
        class Meta:
            fields = ("__id__",)  # ooopsie, a somewhat accidental name conflict

    class InvoiceFieldSchema(marshmallow.Schema):
        class Meta:
            fields = ("__id__", "quantity", "product", "invoice")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Domain Tier
  
To do
Development

No branches or pull requests

1 participant