在Unity中,什么时候通过反射来确定依赖关系?
除了注册的依赖项之外,通过反射确定的依赖项(例如注入的依赖项)是在依赖项的生命周期内还是在 DI 容器的生命周期内完成的?
编辑:心目中的 DI 容器是 Unity。
编辑:详细说明:我很好奇反射过程发生了多少次,并且还想清楚地了解天气或使用反射的 Unity 构建阶段是否会找到构造函数和属性。 Unity 构建的第四阶段,根据 MSDN (来源 ),“预创建”是“第四阶段,对构造函数、属性等进行反射”。这个阶段运行多少次?一次是在容器构建过程中,还是每次都需要解决构造函数和属性依赖关系?这种情况发生在 Register Resolve Release 模式的哪一部分?
Aside from registered dependencies, are dependencies that are determined through reflection, such as injected dependencies, done in regards to the lifetime of the dependency or during the lifetime of the DI Container?
Edit: DI Container in mind is Unity.
Edit: Elaboration: I am curious how many times the reflection process occurs, and also wanted clarity regarding weather or not the stage in the Unity build that uses reflection will find constructors and properties. The fourth stage in the Unity build, according to the MSDN ( Source ), is "Precreation" being the "Fourth stage. Reflection over constructors, properties, etc. is performed here." How many times is this stage run? Once during the construction of the container, or every time constructor and property dependencies need to be resolved? Which part of the Register Resolve Release pattern does this occur in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Unity 使用名为 StrategyChain 的构建管道来创建对象。每次从容器中解析对象时都会运行此管道。
该管道中的每个步骤都是作为一项策略实施的。这些策略查找称为 BuilderPolicies 的值。这些策略封装了策略积累的信息(例如使用反射来确定要使用哪个构造函数、要注入哪些属性等)。第一次运行特定类型的管道时,策略会将策略放入名为
PolicyList
的存储中。下次要求管道构建该类型时,这些值将被重用,因此所涉及的工作只会花费您一次。更新
我猜你是指 ASP.NET?实际上有两个
PolicyList
。一种仅在策略链中存在一个周期(瞬态),另一种与容器的生命周期相关。只要您的UnityContainer
实例没有被释放或被垃圾回收,它就会存在。您可能感兴趣的一件事是:您无法序列化容器。因此,您无法告诉在网络场上运行的应用程序将其存储在缓存中并在服务器之间共享。Unity uses a build pipeline called
StrategyChain
to create objects. This pipeline is run every time you resolve an object from the container.Each step in that pipeline is implemented as a strategy. These strategies lookup values called
BuilderPolicies
. These policies encapsulate information a strategy has accumulated (like using reflection to determine which constructor to use, which properties to inject etc.). The first time you run the pipeline for a specific type the strategies put policies into a store calledPolicyList
. The next time the pipeline is asked to build that type those values will be reused so the effort involved should only cost you once.UPDATE
I guess you mean ASP.NET? Actually there are two
PolicyLists
. One which only lives for one cycle through theStrategyChain
(transient) and one which is coupled to the lifetime of the container. It lives as long as your instance of theUnityContainer
is not disposed or garbage collected. One thing that might be interesting to you: You can't serialize your container. Thus you can't tell an application running on a web farm to store it in a cache and share it between servers.