Enable decorating a service type #75
abdulhaq-e
started this conversation in
Ideas
Replies: 1 comment
-
import svcs
import uuid
from typing import Protocol
class UserService(Protocol):
pass
class UserSQLAlchemyService:
pass
registry = svcs.Registry()
registry.register_factory(UserService, lambda: UserSQLAlchemyService)
class UserSQLAlchemyAdvancedService:
def __init__(self, service: UserService):
self.service = service
def user_sqlalchemy_service_decorator(svcs_container):
original_service = svcs_container.get(UserService)
return UserSQLAlchemyAdvancedService(original_service)
registry.register_factory(UserService, user_sqlalchemy_service_decorator) Something like the above, I'm relying on a plugin architecture where decorating existing services is important. In my implementation, the the decoration has a special api and they are all stored in an iterable where they are applied after all "main" registrations are resolved. So something like this would be possible: registry.register_factory(UserService, user_sqlalchemy_service_decorator, decorator=True) or to be more explicit: registry.decorate_factory(UserService, user_sqlalchemy_service_decorator) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a library where I wrote something very similar to
svcs
but not as polished of course :D. I'm planning to remove my hacky dependency resolver code and replace it withsvcs
. However, looking at the source code and examples, there doesn't seem to be a way of "decorating" a service, i.e. registering it once then when registering it a second time, it can have access to the first instance.Am I right with this conclusion? I can attempt to open a PR with my solution if that is the case.
Beta Was this translation helpful? Give feedback.
All reactions