团结不需要任何登记就能使事情变得轻松吗? [英] Does unity just make clasess with out needing anything registered?
问题描述
我正在观看Mike Tautly关于Prism的精彩介绍.一切都说得通,但是在第六个视频中我有些困惑.
I am watching Mike Tautly's awesome intro to Prism. It was all making sense, but I got a bit confused in the sixth video.
他让他的视图类的构造函数采用ViewModel的参数.然后他说团结将为我们填充(即构建它).
He has his view class's constructor take a parameter of the ViewModel. He then says that unity will fill this in for us (ie construct it).
过去,他不得不注册这种东西(即,将MyyClass注册到MyClass).但是他没有为ViewModel做到这一点.
In the past he has had to register this kind of thing (ie IMyClass registered to MyClass). But he did not do that for the ViewModel.
我能看到的唯一区别是对ViewModel的依赖不是接口,而是直接类.团结会看到这一点,只是尝试构建ViewModel还是错过了一步? (如果ViewModel有参数,它也会尝试做那些吗?)
The only difference I can see is that the dependency to the ViewModel was not an interface, but rather a direct class. Does unity see that and just try to construct the ViewModel or did I miss a step? (And if the ViewModel had parameters would it try to make those too?)
推荐答案
可以实例化一个类型(即不是基类也不是接口)时,Unity可以找出如何创建该类型而无需注册类型映射
When a type can be instantiated (i.e. not a base class nor an interface) unity can figure out how that type is created without the need of registering a type mapping.
当要注入的类型无法单独实例化时,映射很有用.
Mappings are useful when the type to be injected is not instantiatable on its own.
我希望这会有所帮助.
谢谢, 达米安
这篇关于团结不需要任何登记就能使事情变得轻松吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!