Today, generic dependencies are handled by registering concrete types such as directly with the container.
from sqlalchemy.orm import Session
from wireup import injectable
# Define base type
class Repository[T]:
model: type[T]
def __init__(self, session: Session) -> None:
self.session = session
def get(self, id: int) -> T | None:
return self.session.get(self.model, id)
# Define concrete types for injection
@injectable
class UserRepository(Repository[User]):
model = User
@injectable
class BlogPostRepository(Repository[BlogPost]):
model = BlogPost
Investigate whether allowing the following registration is worth it.
One way this could work is by having a special parameter in the ctor.
class Repository[T]:
def __init__(self, session: Session, model: GenericType[type[T]]) -> None:
self.session = session
self.model = model
def get(self, id: int) -> T | None:
return self.session.get(self.model, id)
That would make it possible to inject types like Repository[User] or Repository[BlogPost] without defining a subclass for each one.
Today, generic dependencies are handled by registering concrete types such as directly with the container.
Investigate whether allowing the following registration is worth it.
One way this could work is by having a special parameter in the ctor.
That would make it possible to inject types like
Repository[User]orRepository[BlogPost]without defining a subclass for each one.