在 DRF 中实现服务层的最佳方式

问题描述

我将使用 django rest 框架和 MVC(模型-视图-控制器)实现一个 multi-threaded 应用程序。对我的应用程序的每个新调用 - 都是一个新线程。我想将业务逻辑放在一个单独的层上。我有两个想法如何做到这一点

想法一:

class UserService:
    @staticmethod
    def create_user(user):
        # some business logic
    
    @staticmethod
    def update_user(user):
        # some business logic
    
    # another business methods

服务调用将如下所示:

user_created = UserService.create_user(User("John",19))

想法二:

class UserService:
    def __init__(self,**kwargs):
        # maybe some logic 
    
    def create_user(self,user):
        # some business logic

    def update_user(user):
        # some business logic

# another business methods

服务调用将如下所示:

user_service = UserService()
user_created = user_service.create_user(User("John",19))

哪种实现最好?在第一种情况下,我们有静态方法。在第二种情况下,我们每次都创建一个新对象。使用 DRF 和 MVC 实现这一点的最正确方法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)