Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

A question for core.Service #26

Closed
FindBoat opened this issue Jun 22, 2014 · 7 comments
Closed

A question for core.Service #26

FindBoat opened this issue Jun 22, 2014 · 7 comments

Comments

@FindBoat
Copy link

I got a quick question for core.Service. So what's the point to wrap sqlalchemy's methods into Service rather than call them directly in each application?

Also I didn't find a way to use some sqlalchemy's methods like (query.filter, concatenating multiple filter_by etc.) without calling directly model.query blabla. It seems that Service doesn't provide all methods of sqlalchemy.

@mattupstate
Copy link
Owner

@FindBoat Explained here

@FindBoat
Copy link
Author

Thanks for the explanation. I think my second question is not answered. Actually I can't use some sqlalchemy's query method by calling Service methods. For instance, I want to query obj whose attr1=val1 and attr2=val2 or more complicated.
If I'm using obj.model.query, it seems to break the abstract of Service.

@mattupstate
Copy link
Owner

Nothing is stopping you from using the models directly, or creating a service method that abstracts the query you're trying to make. A hypothetical example:

class UserService(Service):

    def custom_method(self, email):
        return self.__model__.query.filter(self.__model__.email==email).all()

@FindBoat
Copy link
Author

Well, I assume that Service abstracts external stuffs like sqlalchemy, and UserService should only call methods provided by Service. Thus in the future, suppose we want to migrate from sqlalchemy to something else, we only need to change the implementation of Service.

However, if we are calling sqlalchemy methods directly in UserService, it breaks the assumption above. Otherwise, what's the benefit to define a Service rather than using external stuffs directly.

@mattupstate
Copy link
Owner

Thats an assumption you have made for yourself. I've never suggested that custom service methods must use the methods provided by Service. It is by no means a complete abstraction on top of SQLAlchemy either. As described in the blog post, the service layer is to expose a logical API that encapsulates your platforms core logic. That service can then be used in multiple contexts such as a web app, or even a CLI if you wanted to build one on top of it.

@FindBoat
Copy link
Author

I see. That make sense. Thank you very much Matt! Also thank you for sharing your insight on Flask.

@mattupstate
Copy link
Owner

No sweat! Appreciate the discussion.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants