简体   繁体   English

如何在运行时注入对象(初始化后)?

[英]How can objects be injected in run time (after the initialization)?

I'm using the Dependency Injection technique in my two current ongoing projects. 我在我目前正在进行的两个项目中使用了依赖注入技术。

I use Unity as my IoC framework. 我使用Unity作为我的IoC框架。

I'm using a class called ComponentBuilder which is in charge of creating the application objects and their events subscription as part of the initialization of the application. 我正在使用一个名为ComponentBuilder的类,它负责创建应用程序对象及其事件订阅,作为应用程序初始化的一部分。

Once the application is running all the objects are ready and data is passed from one object to the other. 应用程序运行后,所有对象都准备就绪,数据从一个对象传递到另一个对象。

The problem i have is: 我遇到的问题是:

Some of the objects are created dynamically after the initialization, these objects need other objects that already exists. 一些对象是在初始化后动态创建的,这些对象需要已存在的其他对象。

Since the ComponentBuilder already finished its part, i need to find a way of passing the needed objects to these "dynamic" objects. 由于ComponentBuilder已经完成了它的部分,我需要找到一种方法将所需的对象传递给这些“动态”对象。 These dynamic objects are actually a hierarchy of objects created in run time, the deepest object in the hierarchy needs a component (object) from the top of the hierarchy. 这些动态对象实际上是在运行时创建的对象层次结构,层次结构中最深的对象需要从层次结构顶部开始的组件(对象)。

I thought of two possible solutions (i don't like them both): 我想到了两种可能的解决方案(我不喜欢它们):

  1. Pass the container of all objects from the ComponentBuilder to the dynamic objects so that they can use and subscribe to the desired objects. 将ComponentBuilder中所有对象的容器传递给动态对象,以便它们可以使用和订阅所需的对象。 the main con of this option is the expose of all objects which can hide dependencies between objects. 此选项的主要内容是暴露所有可隐藏对象之间依赖关系的对象。 I saw that some people pass the object container to all objects as if it were a logger or display, i think this is wrong and hides the dependencies we want to expose to other objects. 我看到有些人将对象容器传递给所有对象,好像它是一个记录器或显示器,我认为这是错误的,并隐藏了我们想要暴露给其他对象的依赖项。

  2. Pass the needed objects to first object that is created in init time, so that later it would pass the objects down the object hierarchy to the dynamic object. 将所需对象传递给在init时创建的第一个对象,以便稍后将对象层次结构中的对象传递给动态对象。 the main con of this option is that some of the objects down the hierarchy don't need these objects. 此选项的主要内容是层次结构中的某些对象不需要这些对象。

can you think of a better solution ? 你能想到更好的解决方案吗?

Seems you need a standard factory/abstract factory pattern, where you pass in compile time dependencies in the constructor and runtime dependencies in the creation methods. 似乎您需要一个标准的工厂/抽象工厂模式,在这里您可以在构造函数中传递编译时依赖项,并在创建方法中传递运行时依赖项。

class Factory
{
  ISomeService service;

  public Factory(ISomeService service)
  {
    this.service = service;
  }


  public SomeObject CreateItem(object runtimeDependency)
  {
    return new SomeObject(service, runtimeDependency);
  }
}

I didn't really understood your question. 我真的不明白你的问题。 But it seems you are looking for Service Locator pattern. 但似乎您正在寻找服务定位器模式。 Most IoC frameworks have some kind of static object, that allows you to create dependencies on-the-fly instead of injecting them into constructors or properties. 大多数IoC框架都有某种静态对象,它允许您即时创建依赖项,而不是将它们注入构造函数或属性中。

But this has requirement, that your code knows about your IoC. 但这要求您的代码了解您的IoC。 I hate this requirement, so its not the possible solution. 我讨厌这个要求,所以它不是可能的解决方案。 In this case, creating Factory , that allows you to create your dependencies for you is better solution. 在这种情况下,创建Factory ,允许您为您创建依赖项是更好的解决方案。 In this case, concrete way those dependencies are resolved can be hidden by concrete factory, that is internaly using your specific IoC. 在这种情况下,解决这些依赖关系的具体方式可以由具体工厂隐藏,即使用您的特定IoC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM