Reword the initial services section

This commit is contained in:
Radoslav Georgiev 2021-09-19 19:46:45 +03:00
parent fbd7ca14cb
commit 61f6eea6db
No known key found for this signature in database
GPG Key ID: 0B7753A4DFCE646D

View File

@ -363,18 +363,29 @@ A few things to note here:
## Services ## Services
A service is a simple function that: Services are where business logic lives.
* Lives in `your_app/services.py` module The service layer speaks the specific domain language of the software, can access the database & other resources & can interact with other parts of your system.
* Takes keyword-only arguments
* Is type-annotated (even if you are not using [`mypy`](https://github.com/python/mypy) at the moment) A service can be:
* Works mostly with models & other services and selectors
* Does business logic - from simple model creation to complex cross-cutting concerns, to calling external services & tasks. - A simple function.
- A class.
- An entire module.
- Whatever makes sense in your case.
In most cases, a service can be simple function that:
- Lives in `your_app/services.py` module.
- Takes keyword-only arguments, unless it requires no or one argument.
- Is type-annotated (even if you are not using [`mypy`](https://github.com/python/mypy) at the moment).
- Interacts with the database, other resources & other parts of your system.
- Does business logic - from simple model creation to complex cross-cutting concerns, to calling external services & tasks.
An example service that creates a user: An example service that creates a user:
```python ```python
def create_user( def user_create(
*, *,
email: str, email: str,
name: str name: str
@ -383,13 +394,15 @@ def create_user(
user.full_clean() user.full_clean()
user.save() user.save()
create_profile(user=user, name=name) profile_create(user=user, name=name)
send_confirmation_email(user=user) confirmation_email_send(user=user)
return user return user
``` ```
As you can see, this service calls 2 other services - `create_profile` and `send_confirmation_email` As you can see, this service calls 2 other services - `profile_create` and `confirmation_email_send`.
In this example, everything related to the user creation is in one place and can be traced.
### Naming convention ### Naming convention