Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

What do you do when the create requires a lot of fields? #106

Open
simkimsia opened this issue Apr 30, 2020 · 5 comments
Open

What do you do when the create requires a lot of fields? #106

simkimsia opened this issue Apr 30, 2020 · 5 comments

Comments

@simkimsia
Copy link

at https://github.com/dry-python/bookshelf/blob/master/bookshelf/repositories/profile.py#L29, i notice you pass in the user which is required to create the profile

What happens when there are like a dozen required fields to create a new entity? Will you then have a dozen parameters?

@proofit404
Copy link
Contributor

I prefer to pass necessary data as typed dictionary and define a factory method to fill default params.

class ProfileData(TypedDict):
    name: str
    age: int

@dataclass
class Profile:
    @classmethod
    def create(cls, data: ProfileData):
        return cls(balance=0, **data)

@simkimsia
Copy link
Author

ok this is the confusing part for me.

i know that mappers can work with dataclass, attrs, or pydantic models.

So how do i know when to use which?

I did find an article that compares dataclass against attrs so I know that dataclass is a subset of attrs.

What do you recommend?

@simkimsia
Copy link
Author

And your create is at the entity level. So what do you do at the repository level?

@proofit404
Copy link
Contributor

Repository level if for working with a database, a REST endpoint, etc.

When you already have the data you can create entities yourself. I don't see any problems with this approach.

attrs, dataclasses, and pydanctic are equally supported by mappers. You can choose anything you like. But I suggest not mix them across the project.

@simkimsia
Copy link
Author

Repository level if for working with a database, a REST endpoint, etc.

I had to read that a few times.

But i think i understand what u mean now. You mean to say that even in the repository level, you will use a ProfileData typedDict to pass in for create

Yes?

attrs, dataclasses, and pydanctic are equally supported by mappers. You can choose anything you like. But I suggest not mix them across the project.

What do you recommend as default for a Django app? I am on 3.7 python django 2.2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants