I have stumbled across this question when I have noticed that a property injection (through [Inject]
and kernel.Inject(instance)
within a controller took about 3 seconds, thus delaying very much requests processing for a specific controller. Most of the other controllers were affected in a lesser degree.
Short answer
Yes, Ninject clearly brings an overhead, but I found to dependent on three factors: library version, dependency tree size and how dependency is solved.
Aim for a bigger version (3.3.4+), ensure that a dependency does not trigger lots of dependencies to be solved and that you use constructor injection rather than property injection.
Long answer
1. Injection pattern - I encountered performance issues when using property injection. A quick win was to replace it prop => kernel.Get<IFoo>()
, but this pattern is not recommended (3.3.4 marks direct kernel usage as obsolete). However, it reduced the time required to solve the reference without performing major refactoring.
2. Dependency tree - my application used some generic repositories injected within a scoped data access which in turn was injected in several services. For some services this created a large dependency tree which required a lot of time to be solved. Reducing this tree greatly improved the performance.
3. Library version - I have upgraded the library from 3.2.0 to 3.3.4 and noticed a significant performance improvement. However, I payed the price of multiple changes inside the application (migrate from NinjectWebCommon
to MvcApplication
and upgrade of various libraries such as Castle.Core, Ninject.MockingKernel, Ninject.MockingKernel.NSubstitute, Ninject.Web.Common.WebHost)