[英]What is the criteria to choose between IocService and RegistryBuilder in Fantom afIoc
The documentation of Alien Factory's IoC framework for Fantom says: Alien Factory的Fantom IoC框架文档说明:
You can use IocService to start IoC as a Fantom service: 您可以使用IocService将IoC作为Fantom服务启动:
IocService([MyModule#]).start
...
reg := ((IocService) Service.find(IocService#)).registry
service := reg.dependencyByType(MyService#)
...
Service.find(IocService#).uninstall
Or use RegistryBuilder to manage the Registry instance manually; 或使用RegistryBuilder手动管理注册表实例;
reg := RegistryBuilder().addModule(MyModule#).build.startup
...
service := reg.dependencyByType(MyService#)
...
reg.shutdown
But what is the criteria to decide the appropriate way of initialising the registry on a specific scenario? 但是,根据什么标准来决定在特定情况下初始化注册表的适当方式呢?
The short answer - use RegistryBuilder
. 简短的答案-使用
RegistryBuilder
。
The long answer prompted me to update the documentation... it's ongoing, but here's the current revision: 冗长的答案促使我更新了文档...正在进行中,但这是当前的修订版:
Use RegistryBuilder
to manually manage an IoC Registry
instance. 使用
RegistryBuilder
手动管理IoC Registry
实例。 You would typically do this when running tests. 您通常在运行测试时执行此操作。
registry := RegistryBuilder().addModule(MyModule#).build().startup()
...
service := registry.dependencyByType(MyService#)
...
registry.shutdown()
Ensure modules are added from other IoC libraries the code uses. 确保从代码使用的其他IoC库中添加模块。 Example, if using the IocEnv library then add
IocEnvModule
: 例如,如果使用IocEnv库,则添加
IocEnvModule
:
registry := RegistryBuilder().addModule(MyModule#).addModule(IocEnvModule#).build().startup()
It's standard that IoC library modules are named after the library, but with a Module
suffix. IoC库模块以库命名,但是带有
Module
后缀是标准的做法。
If your code runs in an IoC container, such as BedSheet , then the container manages the Registry
instance for you. 如果您的代码在IoC容器(例如BedSheet)中运行 ,则该容器将为您管理
Registry
实例。
If running unit tests then typically you would create your own Registry
instance and hold it as a variable / field. 如果运行单元测试,那么通常您将创建自己的
Registry
实例并将其保存为变量/字段。
An alternative is to create a Fantom Service to hold the registry. 一种替代方法是创建一个Fantom服务来保存注册表。 This is useful in situations where static access to the
Registry
, such as fwt
applications where you have very little control over how your classes are created. 这在静态访问
Registry
情况下很有用,例如fwt
应用程序,您几乎无法控制如何创建类。
IocService
is a helper class that extends 'Service' and contains convenience methods for creating and accessing the registry. IocService
是一个帮助程序类,它扩展了“服务”并包含用于创建和访问注册表的便捷方法。
For example, to create and start a Fantom IoC Service: 例如,要创建并启动Fantom IoC服务:
IocService([MyModule#]).start()
Then from anywhere in your code, it may be accessed with: 然后,可以在代码中的任何位置使用以下命令对其进行访问:
iocService := (IocService) Service.find(IocService#)
...
myService := iocService.serviceById(MyService#.qname)
Uninstall IocService
like any other: 像其他任何
IocService
一样卸载IocService
:
Service.find(IocService#).uninstall()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.