简体   繁体   English

在Fantom afIoc中的IocService和RegistryBuilder之间选择的标准是什么

[英]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: 冗长的答案促使我更新了文档...正在进行中,但这是当前的修订版:

Building the Registry 建立注册表

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后缀是标准的做法。

The IocService IocService

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.

相关问题 如何对使用AlienFactory的afIoc和afMorphia框架的Fantom DAO类进行单元测试? - How to unit-test a Fantom DAO class that uses AlienFactory's afIoc and afMorphia frameworks? 如何选择将实现注入到自动装配的构造函数中 - How to choose what implementation get's injected in to an autowired constructor “后期约束力”与“控制权倒置”之间有什么关系? - What is the relationship between “late binding” and “inversion of control”? @Inject和@Autowired有什么区别 - What is the difference between @Inject and @Autowired 在控制反转中,回调和依赖注入之间有什么区别? - In Inversion of Control, what is the difference between a callback and dependency injection? 在 Autofac 的上下文中:服务和组件之间有什么区别? - In the context of Autofac: What is the difference between a Service and a Component? DependencyInjection和没有DependencyInjection示例之间有什么区别? - What is the difference between the DependencyInjection and without DependencyInjection example? ContainerControlledLifetimeManager和HierarchicalLifetimeManager之间的主要区别是什么? - What is the main difference between ContainerControlledLifetimeManager and HierarchicalLifetimeManager? 谁能告诉我 spring 中的控制反转 (IOC) 和依赖注入 (DI) 有什么区别? - can anyone please tell me what is the difference between Inversion of Control (IOC) and Dependency Injection (DI) in spring? C ++中的控制反转和依赖注入之间有什么区别? - What is the difference between Inversion of Control and Dependency injection in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM