-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: factory methods only receive activating type instead of registered type #53
Comments
Hi @StummeJ |
Sure, we ran into issues when trying to construct a database connection based on an aliased type ReadOnlyDb = Annotated[Engine, intern("database_read_only")]
ReadWriteDb = Annotated[Engine, intern("database_read_write")]
def database_client_factory(
scope: ActivationScope, activating_type: Type, registered_type: Type
) -> Engine:
...
if registered_type == ReadOnlyDb:
host = config.reader.host
port = config.reader.port
if "postgresql" in config.dialect:
connection_options.update(
{
"isolation_level": "REPEATABLE READ",
"postgresql_readonly": True,
"postgresql_deferrable": True,
}
)
elif registered_type == ReadWriteDb:
host = config.writer.host
port = config.writer.port
...
# Database
self._container.add_singleton_by_factory(database_client_factory, ReadOnlyDb)
self._container.add_singleton_by_factory(database_client_factory, ReadWriteDb) If you request a type that relies on |
Why not something like the following, if you want to reason in terms of ReadOnlyDb = Annotated[Engine, intern("database_read_only")]
ReadWriteDb = Annotated[Engine, intern("database_read_write")]
def read_only_db_engine_factory(
scope: ActivationScope, activating_type: Type
) -> Engine: ... # return instance of ReadOnlyDb
def read_write_db_engine_factory(
scope: ActivationScope, activating_type: Type
) -> Engine: ... # return instance of ReadWriteDb
# Database
if config.read_only_db:
self._container.add_singleton_by_factory(read_only_db_engine_factory)
else:
self._container.add_singleton_by_factory(read_write_db_engine_factory) I do something similar in the projects where I want to support different kinds of persistence layer (like here) if settings.db_connection_string:
# Register SQL DB data access services
else:
# Register alternative data access services The second argument of the register factory functions was never meant to be used in the way you are suggesting, it was always meant to be used when the factory doesn't have an annotated return type. |
For the database connection there is work prior to and after the conditional that is relevant to both. Having two methods does solve this issue, but it does create duplication in the codebase. Any thoughts with regards to that? |
Given the following, factory methods fail to produce the expected outcome, and instead raise the
NotImplementedError
exception.The text was updated successfully, but these errors were encountered: