sunnydi依赖注入框架

tycoon的Python项目详细描述


https://img.shields.io/pypi/v/sunnydi.svghttps://img.shields.io/pypi/status/sunnydi.svghttps://travis-ci.org/thomasstreet/sunnydi.svg?branch=masterhttps://coveralls.io/repos/github/thomasstreet/sunnydi/badge.svg?branch=masterDocumentation Status

sunnydi是 在python中管理和注入依赖项。

它的灵感来自于.net的AutofacGuice用于Java。

如何使用

在我们的例子中,我们将为helloservice创建一个ioc模块。

classHelloService(object):defhello(self):return'hello'

创建扩展sunnydi.ioc.module的新配置模块。 模块定义如何创建、销毁对象并将其提供给ioc对象图中的其他对象实例。 在最简单的配置中,我们只需将字符串名称绑定到helloservice类类型:

classHelloModule(Module):defconfigure(self):self.bind('hello_service').to(HelloService)

然后,我们可以创建注入器并像这样解决hello服务:

>>>hello_module=HelloModule()>>>injector=hello_module.create_injector()>>>hello_service=injector.get('hello_service')>>>hello_service.hello()'hello'

解析实例通过构造函数参数提供给使用类。 例如,给定以下类:

classMyClass(object):def__init__(self,hello_service):self._hello_service=hello_servicedefdo_hello(self):returnself._hello_service.hello()

由于 服务的绑定名与myclass构造函数中定义的参数匹配:

>>>my_class_instance=injector.get(MyClass)>>>my_class_instance.do_hello()'hello'

对于高级用法,请签出docs

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
与ReentrantLock相比,java ReentrantReadWriteLock的性能非常差   java如何使用Maven Android Studio正确导入?   安卓将ADB添加到我的Java PC应用程序   反射Java getDeclaredConstructor失败,来自JUnit的NoSuchMethodException   JSP上siteedit标记库的java替代   JavaSpring环境概要文件和JPA   java中是否有一个类似于StringBuilder的类,唯一的区别是它具有固定的长度?   JavaMathContext。小数点32 vs MathContext。小数点64,使用哪一个,为什么?   java使用spring在Ibm Websphere MQ中实现重试逻辑   java调用SpriteBatch。开始()和结束()   java有一种从文本中读取文本的方法。文件,并将其设置为pom中的maven属性。xml专家?   java让sitemesh使用struts2   Java Swing:在现有窗口上定位对话框   使用带有MemSql的JPA本机查询的java Select json列