Service-Repository Pattern Implementation In Django For Your APIs

Service-Repository Pattern Implementation In Django For Your APIs

Imagine that you have a Django app with a views.py file, And you put all the logics of your APIs from database stuff to email sending and …

The problem with such a code is that at some point of time this file gets too large and there are a lot of unrelated logics in it.

The problems with this type of code includes:

  • All Data Access and Business logics and all other logics are mixed together, And you don't know where to find some specific logic and updating your code will be hard.
  • Your code is not testable or at least hard to test, Because a lot of logics are in a simple method or class or whatever …
  • Your code is not reusable. Imagine you want to update a user's detail in a specific situation. You should probably duplicate your code when you want to update the user's detail in another situation when that specific condition happens.
  • You can't scale your code. When some logics gets heavy you should scale your code and because your code is a mess you can't do it, Specially in microservices.
  • You can't change your code easily.


To address these problems there is a design pattern that separates the logic and it's called "Service Repository Pattern".


Let's see an example of it in an API in Django.

Step 1: Create a services directory in your Django app with __init__.py file.

No alt text provided for this image

Step 2: Create a file named "user_service.py":

No alt text provided for this image

Step 3: Create a directory called "repositories" in your Django app and then create two files with the names "__init__.py" and "user_repository.py" in it.

No alt text provided for this image

Step 4: Create your Data Access login in your repository file.

No alt text provided for this image

You can see that we put database logic to get a user from database or creating a user in this file. This is a simple example, Imagine when you have a longer database logic.

Step 5: Create your service layer in "user_service.py" file.

No alt text provided for this image

Step 6: Create you views logic:

No alt text provided for this image


And that's it, You can use this pattern to have a more readable and testable code.

Hope you've enjoyed it.

Thanks for reading.


Updated : 12-12-2023

Note: This is a simple implementation of the pattern, and it does not have a robust dependency injection as I didn't completely implemented the constructor injection but wanted to show you a grasp of it.


Alonso Montenegro ??

Backend Developer at Intelligenia

1 个月

Why not use managers?

回复
Ravshan Sodikov

Software Engineer | AI & ML enthusiast

10 个月

Why did you not build UserRepository as an abstract class using the ABC class?

Hoàng V? Tr?n

Student at Ho Chi Minh City University of Technology

11 个月

How can you test it if you can't mock the dependency, like repository and service??

wrya mhamad

Senior Backend Developer @ RIGT OFFICIAL

1 年

What about dependency injection and DI Container?

Very useful ????

要查看或添加评论,请登录

社区洞察

其他会员也浏览了