简体   繁体   中英

Simple Injector: Automatic constructor injection recursion

I am trying to understand how automatic constructor injection is done in SimpleInjector.NET. My working source code is from the Code Project article (v1.5.0.12199).

I have looked at the Container's GetInstance)() code and also the InstanceProducer's GetInstance() code but I don't see any recursive calls anywhere.

Can someone point out where the recursion for automatic constructor injection happens?

A lot has changes in the container from version v1.5 to the current v2.5 framework. How things are exactly done in the old version I can't recall, but this is what happens in the current v2.5 version (and things might of course change in the future, since they are implementation details).

If you want to look at the method where this happens, you need to go to the private BuildConstructorParameters method of the Registration class (note again, I'm talking about the v2.5 source code here). This method calls Type.GetParameters and iterates them and asks the configured IConstructorInjectionBehavior type for the expression for the parameter. The DefaultConstructorInjectionBehavior will call Container.GetRegistration to get the proper InstanceProducer for that parameter and that closes the loop.

So the complete stack trace will be something like this (in top down order):

  • Container.GetInstance
  • InstanceProducer.GetInstance
  • InstanceProducer.BuildInstanceCreator
  • InstanceProducer.BuildExpressionInternal
  • Registration.BuildExpression
  • Registration.BuildTransientExpression
  • Registration.BuildNewExpression
  • Registration.BuildConstructorParameters
  • Registration.BuildParameterExpressionFor
  • DefaultConstructorInjectionBehavior.BuildParameterExpression
  • InstanceProducer.BuildExpression
  • InstanceProducer.BuildExpressionInternal <- recursion starts here
  • Registration.BuildExpression

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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