bookshelf icon indicating copy to clipboard operation
bookshelf copied to clipboard

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

Open simkimsia opened this issue 4 years ago • 5 comments

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?

simkimsia avatar Apr 30 '20 10:04 simkimsia

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)

proofit404 avatar Apr 30 '20 10:04 proofit404

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 avatar Apr 30 '20 11:04 simkimsia

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

simkimsia avatar Apr 30 '20 11:04 simkimsia

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.

proofit404 avatar Apr 30 '20 12:04 proofit404

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

simkimsia avatar Apr 30 '20 14:04 simkimsia