ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Q: What are advantages of Deferred Lifestyle? #196

Closed mcdis closed 5 years ago

ipjohnson commented 5 years ago

Hi @mcdis

The deferred singleton lifestyle is only useful when you're having issues with circular dependencies. Essentially when using the normal singleton lifestyle the instance is created during the construction of the compiled linq expression for maximum performance. The deferred singleton doesn't construct the instance until the very end after the linq expression has been constructed and compiled. It sacrifices a very small amount of performance for the ability to be constructed later in the resolution process.

I've only seen one or two situations where the deferred lifestyle was needed.

dadhi commented 5 years ago

@mcdis Sorry for intruding. I would imagine the case, when the whole graph is failing for some reason, without deferred lifestyle you may still get singletons created. Which maybe not what you'd expect.

ipjohnson commented 5 years ago

@dadhi touches on an interesting side affect because the Singleton is created during the expression creation it can have an interesting side effect in case of failure or even trying to understand the order of creation.

ipjohnson commented 5 years ago

@mcdis can I close this out?