简体   繁体   English

这些构造函数参数来自控件实例的这种反转?

[英]Where do these constructor parameters come from in this inversion of control instantiation?

I'm working on an incomplete application in WPF/C#. 我正在WPF / C#中处理不完整的应用程序。 Most of it is very well coded, though as I didn't know about Inversion of Control beforehand it puzzled me for a while. 大部分代码都经过了很好的编码,尽管由于我之前并不了解控制反转,所以它使我困惑了一段时间。 Now I think I've figured it out some, but some things still stump me. 现在我想我已经解决了一些问题,但是有些问题仍然困扰我。

Case in point: we have this great big class called CoreApplication that manages everything general. 例子:我们有一个很棒的大类叫做CoreApplication ,它管理所有常规CoreApplication Among other things, it has a series of RegisterType instructions, among which 除其他外,它具有一系列的RegisterType指令,其中

SharedObjects.InversionOfControl.RegisterType<IGoodClass, GoodClass>();

( SharedObjects being a static class that defines, among other things, a static IUnityContainer object named InversionOfControl , initialized with new UnityContainer() at declaration.) SharedObjects是一个静态类,除其他外,它定义了一个名为InversionOfControl的静态IUnityContainer对象,该对象在声明时使用新的UnityContainer()初始化。)

Elsewhere in CoreApplication , there is an ICommand that includes the code CoreApplication其他地方,有一个ICommand包含代码

var goodClassForNavigation = (SharedObjects.InversionOfControl.Resolve<IGoodClass>()); 

Which, I understand, calls the constructor of GoodClass , since Register() linked IGoodClass and GoodClass and Resolve() was called with IGoodClass . 据我了解,它调用了GoodClass的构造函数,因为Register()链接了IGoodClassGoodClassResolve()是用IGoodClass

Fine. 精细。 Now, going to GoodClass to understand what it does, I see that its constructor looks like this: 现在,转到GoodClass了解其功能,我看到其构造函数如下所示:

public GoodClass(CoreApplication coreApplication)
{
    this.coreApplication = coreApplication;
    // Other stuff happens
}

I've made sure that the parameter was duly instantiated; 我确保参数已正确实例化; various properties and fields of it have the values I'd expect. 它的各种属性和字段具有我期望的值。

What I cannot understand is, where does this CoreApplication parameter come from? 我不明白的是,这个CoreApplication参数来自哪里?

I tried to look up the constructor in GoodClass 's base classes, but they have no explicit one. 我试图在GoodClass的基类中查找构造函数,但它们没有显式的构造函数。

Thanks to those who still are with me so far. 谢谢那些至今仍与我在一起的人。 What is happening? 怎么了? Where does the constructor take its parameter from? 构造函数从哪里获取参数? Is it from the class the constructor is called in? 是从构造函数调用的类中获得的吗? From something in SharedObjects.InversionOfControl ? SharedObjects.InversionOfControl东西开始?

Simply put your DI framework does the heavy lifting: 简单地说,您的DI框架可以完成繁重的工作:

  1. you call Resolve(), a method of your DI framework 您调用Resolve(),这是您的DI框架的一种方法
  2. the DI framework now resolves IGoodClass to GoodClass DI框架现在将IGoodClass解析为GoodClass
  3. the DI framework analyses the constructor and sees that a parameter is needed DI框架分析了构造函数,并发现需要一个参数
  4. the DI famework now tries to resolve a parameter instance based on the parameter type, in your case CoreApplication DI famework现在尝试根据参数类型解析参数实例,在您的情况下为CoreApplication
  5. the DI framework creates and instance of CoreApplication DI框架创建CoreApplication实例
  6. the DI framework now can create an instance of GoodClass by passing the resolved instance of CoreApplication to the constructor DI框架现在可以通过将解析的CoreApplication实例CoreApplication给构造函数来创建GoodClass的实例

The resolution process can be configured, for example with registration by convention . 可以配置解析过程,例如通过约定注册

I suggest you read the article Dependency Injection with Unity . 建议您阅读文章Unity依赖注入

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

相关问题 KeyVaultClient.AuthenticationCallback Delegate 的参数从何而来? - Where do the parameters for KeyVaultClient.AuthenticationCallback Delegate come from? 如何在CompositeCommand上进行控件反转? - How to do inversion of control on CompositeCommand? Html.RenderAction调用的操作的参数从何而来? - Where do the parameters come from for action that gets invoked by Html.RenderAction? 这些其他解决方案配置从何而来? - Where do these additional solution configurations come from? 这些1k线程来自哪里 - Where do these 1k threads come from 控制对象的实例化而不限制构造函数访问 - Control Instantiation of objects without restricting constructor access 填充列表视图控件将参数从另一个表单传递给构造函数 - Populating listview control passing parameters to constructor from another form IoC框架的“构建器”和“容器”一词从何而来? - Where do the words Builder and Container come from for an IoC Framework? VB6 / COM Interop:这些事件来自哪里? - VB6/COM Interop: where do these events come from? C# 相交扩展 - “相等”元素从何而来? - C# Intersect Extension - where do the "equal" elements come from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM