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

Shared functionality across models #161

Open
eddielu opened this issue Dec 1, 2024 · 1 comment
Open

Shared functionality across models #161

eddielu opened this issue Dec 1, 2024 · 1 comment

Comments

@eddielu
Copy link

eddielu commented Dec 1, 2024

I am following django style guide which proposes use services like person_create and classroom_create. This is all working great, except now I am having trouble with functions that can handle multiple type's of instances.

For example, previously, I could pass in a type like Person / Classroom and then the fn could blind use t = Type(**kwargs), then t.save().

But how do I do this following django style guide, where there are person_create and classroom_create. This limited example has only two types, but what if there are N types that the shared fn can take in.

@RadoRado
Copy link
Member

RadoRado commented Dec 2, 2024

@eddielu Hello and thank you for opening this issue 👋

I am not a 100% sure I understand what you are asking / implying, but I'll answer based on what I think it is:

The general idea of the service layer is to wrap all the related business / app / domain logic in once place.

This can lead you to the following extreme: Every model creation is happening in a separate service function. This is not very readable nor maintainable.

So if you need to create a person & create related classrooms for that person upon creation - you place that code there & don't extract it to a separate classroom_create.

Generally speaking, if you have to do something only in 1 place, there's no need to extract it, so it's reasuable.

Again, I'm not sure I completely understand your question, so if you can provide an example - I may be able to give you a better answer.

Cheers!

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

No branches or pull requests

2 participants