-
Notifications
You must be signed in to change notification settings - Fork 18
Library design and methods
rodi
works by inspecting __init__ methods once at runtime, to generate functions that return instances of desired types. Validation steps, for example to detect circular dependencies or missing services, are done when building these functions, so additional validation is not needed when activating services.
For this reason, services are first registered inside an instance of Container class, which implements a method build_provider() that returns an instance of Services. The service provider is then used to obtain desired services by type or name. Inspection and validation steps are done only when creating an instance of service provider.
Registers an exact instance (singleton), optionally specifying a declared type. If the type is not specified, the type of the instance is used.
Registers a singleton by base and concrete type, its dependencies are resolved dynamically when the provider is built.
Registers a service by base and concrete type. Every time the service is required, a new instance is created. Dependencies are resolved dynamically when the provider is built.
Registers a service by base and concrete type. Within the context of a single call to obtain a service (provider.get
), only a single instance is created. Dependencies are resolved dynamically when the provider is built.
Registers a singleton by concrete type, its dependencies are resolved dynamically when the provider is built.
Registers a service by concrete type. Every time the service is required, a new instance is created. Dependencies are resolved dynamically when the provider is built.
Registers a service by concrete type. Within the context of a single call to obtain a service (provider.get
), only a single instance is created. Dependencies are resolved dynamically when the provider is built.
Registers a singleton by factory. The first time the singleton is required, the given factory function is used. Factories receive the instance of service provider in input. If the factory is decorated with type hints, it is not necessary to specify the service type.
def foo_factory(provider) -> Foo:
return Foo()
services.add_singleton_by_factory(foo_factory) # fine, because the type is obtained from function hint
def ufo_factory(provider):
return Ufo()
services.add_singleton_by_factory(ufo_factory, Ufo) # fine, because the type is specified
services.add_singleton_by_factory(ufo_factory) # raises exception because the type must be specified
Registers a transient service by factory. The given factory function is called whenever the service is instantiated.
Registers a scoped service by factory. The given factory function is called whenever the service is instantiated.
Registers service by base, concrete types and life style. This method is used internally by other methods involving type resolution.
Registers service by factory and optionally return type. This method is used internally by other methods involving factories.
Returns true if a given type or type name is configured in the service configuration, false otherwise.
services.add_transient(IFoo, MemoryFoo)
assert IFoo in services # OK, because IFoo is configured
Returns a service of given type, by type name or class.
Adds a singleton to the map of services. This method exists to make an instance of Services
compatible with a regular dictionary.
Returns a service of given type, by type name or class (dictionary notation).
Adds a singleton to the map of services, with dictionary notation. This method exists to make an instance of Services
compatible with a regular dictionary.
Returns true if a given type or type name is handled by the service provider, false otherwise.
services.add_transient(IFoo, MemoryFoo)
provider = services.build_provider()
assert IFoo in provider # OK, because IFoo is configured