辅助注射什么时候有用? [英] When is Assisted-inject useful?

查看:79
本文介绍了辅助注射什么时候有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在我的应用程序中使用Guice.但是我发现自己主要使用辅助注入,因为有一系列注入对象都取决于程序的输入. 因此,几乎所有东西都可以辅助注射.

I'm currently using Guice in my App. However i find myself mostly using assisted inject because there is a chain of injected objects that all depend on what was the input of the program. Hence almost everything is assisted inject.

例如,需要A的需求B和需要C的需求,需要Z的需求,需要从命令行输入的信息.最后,我觉得一切都会得到协助.因此,鉴于我坚持使用它,我想确保自己使用正确.

For instance A need B who need c who need Z which needs input from the command line. In the end i feel like everything will be assisted injected. So given that i'm stuck with it i want to be sure that i m using it right.

我个人觉得写自己的工厂会很好.此外,除了相同的优点外,我还可以将我所讨论的对象的创建进一步限制在这些工厂中.

I personally feel like writing my own factories would be as good. Moreover, in addition to the same advantage i could further limit the creation of my objects in question to these factories.

因此,我的问题是,使用辅助注射真的有多大用处,难道仅仅是让事物同时被辅助和不被辅助的想法吗?如果像我这样,您只有辅助参数怎么办?

Hence my question here is, how useful is it really to use assisted inject, is it only the idea of having things assisted and non assisted as the same time? What if like in my case you have only have assisted parameters?

在辅助注射的帮助下,他们必须有所收获.我只是看不到.

Their must be some gain at organizing it with assisted injection. I just don't see it.

如果有人能在这里启发我,我将不胜感激,

If anyone could enlighten me here, i would greatly i appreciate,

非常感谢

PS:

我的依赖性是这样的:

我有一个 InfrastructureService ,它需要一个 KnowledgeBaseService ,而它又需要一个 ConFigDataObject .我的configDataObject包含来自程序输入的信息. configDataObject在对这些输入进行一些验证和处理之后存储这些信息.例如,可以向其提供一个表示文件路径的字符串,它将验证它是存在的文件,并具有将该File返回给其使用方的getter方法.其他内容可能是实际的URL对象的URLNname,依此类推.

I have an InfrastructureService, which require a KnowledgeBaseService which in turn require an ConFigDataObject. My configDataObject contains information coming from the inputs of the program. The configDataObject store these information after doing some validation and processing on those inputs. For instance a string representing file path can be provided to it, and it will validate that it is file that exist, and have a getter method that return that File to its consumer. Other things might be URLNname to real URL Object, and so on.

此处的重点是以下图形: InfrastructureService -> KnowledgeBaseService -> ConFigDataObject -> InputData

The important point here is this following graph: InfrastructureService -> KnowledgeBaseService -> ConFigDataObject -> InputData

因此, InfrastructureService 只能与以正确的inputFile,URL,workingfolder等开头的 knowledgeBaseService 一起使用. configDataObject,可以从程序的输入中接收它们并存储它们的已处理版本.

Therefore the InfrastructureService can only work with a knowledgeBaseService that is started with the right inputFile, URL, workingfolder and etc.... which is provided to it with a configDataObject, that receive them from the input of the program and store a processed version of them.

因此,到目前为止,我要做的是拥有一个AssistantFactory以创建 knowledgeBaseService .它以ConfigDataObject作为参数. configDataObject 是使用 factoryMethod(Scala配套对象)创建的.最后,还为 InfrastructureService 创建了一个辅助工厂,该工厂将 KnowledgeBaseService 作为其 creation方法的参数.

Therefore as of now what i do is to have a assistedFactory to create the knowledgeBaseService. It takes as parameter the ConfigDataObject. The configDataObject is created with a factoryMethod (Scala Companion Object). Finally, the an assistedFactory is also create for the InfrastructureService which takes as parameter for its creation method a KnowledgeBaseService.

您可以猜测,几乎所有内容都是预先创建的,并且是以某种方式手动创建的.我觉得很奇怪.

As you can guess everything is pretty much created upfront, and somehow manually. I find it odd.

推荐答案

在这里,我最终以一种比使用由于应用程序的参数使用不必要的辅助注入链更为优雅的方式解决了该问题.

Here is what i did to finally solve the issue in a more elegant way than using an unnecessary chain of assisted injected due to the parameters of the application.

也就是说,如果就我而言,您的数据来自命令行,那么我认为正确的方法是将输入数据结构的类型绑定到从命令行获得的实例输入结构:

that is If as in my case your data comes from the command Line then the right appraoch i believe is to bind the type of the input data structure to instance input structure obtained from the command line:

object MyApp extends App {

  val config = ConfigData(args(0))

  val injector = Guice.createInjector(module(config))
  val service = injector.getInstance(classOf[InfrastructureService])

  println("The service name is:" + service.kbService.config.configName)

}


case class module(config: ConfigData) extends AbstractModule {
  def configure(): Unit = {
    bind(classOf[ConfigData]).toInstance(config)
  }
}

case class ConfigData(val configName: String)

class KbService @Inject() (val config: ConfigData)

class InfrastructureService @Inject() (val kbService: KbService)

我相信这里的关键是要提醒自己,可以使用任何必要的输入数据来对模块进行参数化

I believe the Key here is to remind yourself that the module can be parameterize with any input data deem necessary

这篇关于辅助注射什么时候有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆