阅读最新的真实Python教程
iocframework的Python项目详细描述
IOC框架文档
这个项目是python面向对象项目的依赖注入框架
安装
pip install iocframework
基本用途
- 注册项目的类依赖关系映射。在
- 从注册的依赖项映射解析项目的依赖项。在
- 解析项目的类的嵌套依赖项,利用类型化实例化没有参数构造函数的依赖项。在
示例用例
from IOCFramework import get_app
class BarService:
def __init__(self):
pass
class FooService:
bar_service: BarService
def __init__(self, bar_service: BarService):
self.bar_service = bar_service
class FooController:
foo_service: FooService
def __init__(self, foo_service: FooService):
self.foo_service = foo_service
app = get_app()
foo_controller = app.make(FooController) # makes an instance of fooController
在上面的例子中,依赖树从FooController
向下到BarService
,在现实生活中,它可能更深入,这个包通过沿着依赖关系图巧妙地解析和实例化FooController。它将BarService
识别为FooService
的依赖项,该依赖项本身就是所需的FooController
的依赖项。在
另一个用例是当你想解析一个类,但是有一些默认值。在
示例用例
^{pr2}$在上面的示例中,我们创建了一个FooService
的实例,我们通过手动创建BarService
的实例并通过参数名将其传递给容器,从而手动将其依赖项BarService
的实例注入到该实例中。这对于向类实例传递常量或将另一个类创建的对象传递给新类非常有用。在
另一个用例是将类或接口绑定到容器中的实现。在
示例用例
from IOCFramework import get_app
class BarService:
def __init__(self):
pass
class AbstractFooService:
def say_foo(self):
raise MethodNotImplemented
class FooService(AbstractFooService):
bar_service: BarService
def __init__(self, bar_service: BarService):
self.bar_service = bar_service
def say_foo(self):
return "foo"
app = get_app()
bindings = {
AbstractFooService: FooService
}
app.add_bindings(bindings)
foo_service = app.make(AbstractFooService)
foo_val = foo_service.say_foo() # returns foo
在上面的例子中,我们注册了AbstractFooService
以返回{
上面的用例可以扩展到singleton,假设我们希望我们的整个应用程序使用一个非常昂贵的类的单个实例,我们可以使用app的singleton方法;下面是一个例子
示例用例
from IOCFramework import get_app
class BarService:
def __init__(self):
pass
class FooService:
bar_service: BarService
def __init__(self, bar_service: BarService):
self.bar_service = bar_service
class FizzService:'
bar_service: BarService
def __init__(self, bar_service: BarService):
self.bar_service = bar_service
app = get_app()
singletons = {
BarService: BarService
}
app.add_singletons(singletons)
foo_service = app.make(FooService)
fizz_service = app.make(FizzService)
fizz_service.bar_service == foo_service.bar_service # True because barService is a singleton
还有另一个用例为上面的用例增加了功能,想象一下你有一个基类,有多个子类,每个子类服务于一个服务类。要解决这一问题,您可以使用服务的特定类实现键入依赖项,将自己锁定到实现中,或者您也可以使用它们的基类键入它们,然后根据需要解决它们。在
示例用例
from IOCFramework import get_app
class BaseBarService:
def __init__(self):
pass
class FooBarService(BaseBarService):
def __init__(self):
pass
class FizzBarService(BaseBarService):
def __init__(self):
pass
class FooService:
bar_service: BaseBarService
def __init__(self, bar_service: BaseBarService):
self.bar_service = bar_service
class FizzService:'
bar_service: BaseBarService
def __init__(self, bar_service: BaseBarService):
self.bar_service = bar_service
app = get_app()
app.when(FizzService, needs=BaseBarService, give=FizzBarService) # give can also be a callable which returns an instance of the service in question
app.when(FooService, needs=BaseBarService, give=FooBarService)
fizz_service = app.make(FizzService)
foo_service = app.make(FooService)
isinstance(fizz_service.bar_service, FizzBarService) # True
isinstance(foo_service.bar_service, FooBarService) # True
这个项目基于Laravel的IOC容器。在
- 项目
标签: