Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

python - The best way to implement a service layer in DRF

I am going to implement a multi-threaded application using django rest framework and MVC (model-view-controller). Every new call to my application - is a new thread. I want to put business logic on a separate layer. I have two ideas how to do this

Idea number one:

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

And the service call will look like this:

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

Idea number two:

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

And the service call will look like this:

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

Which implementation is best? In the first case we have static methods. In the second case we create a new object every time. What is the most correct way to implement this using a DRF and MVC?

question from:https://stackoverflow.com/questions/65647255/the-best-way-to-implement-a-service-layer-in-drf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...