Add naming convention secitons for services/selectors

This commit is contained in:
Radoslav Georgiev 2019-11-30 12:39:16 +02:00
parent 15633973b5
commit 8d3fb415de

View File

@ -290,6 +290,34 @@ def create_user(
As you can see, this service calls 2 other services - `create_profile` and `send_confirmation_email`
### Naming convention
Naming conventions depend on your taste. It pays off to have a consistent naming convention through out a project.
If we take the example above, our service is named `create_user`. The pattern is - `<action>_<entity>`.
What we usually prefer in our projects, again, depending on taste, is `<entity>_<action>` or with the example above: `user_create`. This seems odd at first, but it has few nice features:
* Namespacing. It's easy to spot all services starting with `user_` and it's a good idea to put them in a `users.py` module.
* Greppability. Or in other words, if you want to see all actions for a specific entity, just grep for `user_`.
A full example would look like this:
```python
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
```
## Selectors
@ -314,6 +342,11 @@ def get_users(*, fetched_by: User) -> Iterable[User]:
As you can see, `get_visible_users_for` is another selector.
### Naming convention
Read the section in services. Same rules apply here.
## APIs & Serializers
When using services & selectors, all of your APIs should look simple & identical.