[英]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()
链接了IGoodClass
和GoodClass
而Resolve()
是用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框架可以完成繁重的工作:
IGoodClass
to GoodClass
DI框架现在将IGoodClass
解析为GoodClass
CoreApplication
DI famework现在尝试根据参数类型解析参数实例,在您的情况下为CoreApplication
CoreApplication
DI框架创建CoreApplication
实例 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.