Providers
Aioinject implements multiple different providers, they work similarly to service lifetimes in other libraries, such as DI in .NET Core
Scoped
Objects provided with Scoped
provider would be cached within context.
import aioinject
container = aioinject.Container()
container.register(aioinject.Scoped(list))
with container.sync_context() as ctx:
object_1 = ctx.resolve(list)
object_2 = ctx.resolve(list)
assert object_1 is object_2
Transient
Transient
provider would provide different instances even if used within same context
import aioinject
container = aioinject.Container()
container.register(aioinject.Transient(list))
with container.sync_context() as ctx:
object_1 = ctx.resolve(list)
object_2 = ctx.resolve(list)
assert object_1 is not object_2
Singleton
Singleton
works as you expect - there would be only one instance of a singleton
object, bound to a specific container
import aioinject
container = aioinject.Container()
container.register(aioinject.Singleton(list))
with container.sync_context() as ctx:
object_1 = ctx.resolve(list)
with container.sync_context() as ctx:
object_2 = ctx.resolve(list)
assert object_1 is object_2
Object
Object
provider just returns an object provided to it: