mirror of
https://github.com/HackSoftware/Django-Styleguide.git
synced 2025-02-06 22:50:53 +03:00
Add section with basic service explanation
This commit is contained in:
parent
3cb761831a
commit
aebf4511ff
28
README.md
28
README.md
|
@ -24,3 +24,31 @@ Expect often updates as we discuss & decide upon different things.
|
||||||
|
|
||||||
* If the model property spans multiple relations, it should better be a selector.
|
* If the model property spans multiple relations, it should better be a selector.
|
||||||
* If a model property, added to some list API, will cause `N + 1` problem that cannot be easily solved with `select_related`, it should better be a selector.
|
* If a model property, added to some list API, will cause `N + 1` problem that cannot be easily solved with `select_related`, it should better be a selector.
|
||||||
|
|
||||||
|
## Services
|
||||||
|
|
||||||
|
A service is a simple function that:
|
||||||
|
|
||||||
|
* Takes keyword-only arguments
|
||||||
|
* Is type-annotated (even if you are not using `mypy` at the moment)
|
||||||
|
* Does business logic - from simple model creation to complex cross-cutting concerns, to calling external services & tasks.
|
||||||
|
|
||||||
|
An example service that creates an user:
|
||||||
|
|
||||||
|
```python
|
||||||
|
def create_user(
|
||||||
|
*,
|
||||||
|
email: str,
|
||||||
|
name: str
|
||||||
|
) -> User:
|
||||||
|
user = User(email=email)
|
||||||
|
user.full_clean()
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
create_profile(user=user, name=name)
|
||||||
|
send_confirmation_email(user=user)
|
||||||
|
|
||||||
|
return user
|
||||||
|
```
|
||||||
|
|
||||||
|
As you can see, this service calls 2 other services - `create_profile` and `send_confirmation_email`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user