HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects
MIT License
5k stars 511 forks source link

Service for bulk_create #150

Closed devmitrandir closed 4 months ago

devmitrandir commented 7 months ago

Hello

If I need to create many objects of another model B within the service of model A, should I create a b_bulk_create service and call it within a_create?

RadoRado commented 7 months ago

@devmitrandir hello :wave:

If you have a service, dedicated to some action, regarding model A, which action also requires creating a bunch of instances of model B, you just put that code inside the service & don't create another one, just to be called & add another level of indirection.

Services are all about wrapping your actions over your domain in a single place.

Cheers!

devmitrandir commented 7 months ago

Thanks!

But here's the example

def user_create(
    *,
    email: str,
    name: str
) -> User:
    user = User(email=email)
    user.full_clean()
    user.save()

    profile_create(user=user, name=name)
    confirmation_email_send(user=user)

    return user

By this logic, it turns out that I need to create objects of type B in another service.

Example:


def sample_create():
   sample = Sample()
   ...
   parameters_bulk_create(sample=sample)
   return sample
RadoRado commented 4 months ago

@devmitrandir I'd say the following:

If sample_create is the only place, where you need to run parameters_bulk_create - simply inline the code in sample_create.

No need to draw boundaries where there's no need for them.

This is something that we quite often do.

I'm also closing this issue. Feel free to reopen it or simply open a new one.

Cheers!