How to easily tell if Ninject can resolve a class
Asked Answered
B

2

6

I'm introducing Ninject into a large mess of a existing project. I want to write a diagnostic test to make sure that all of the classes Ninject will end up creating can actually BE resolved by Ninject...without actually creating any of them.

The reason I want to avoid the actual construction is that many of these classes have a tendency to start up database operations in their constructors (sigh yes I know). Otherwise I would just run them all through Get<T> with a try/catch

Bulkhead answered 12/11, 2010 at 22:50 Comment(0)
B
5

There's a CanResolve extension on IResolutionRoot (i.e., you can use it against Kernel if you have the right usings in place). There's a CreateRequest that you use to create the request. Have a look in the sources and tests if you need an example or any deeper information.

Berty answered 15/11, 2010 at 9:41 Comment(0)
I
1

I know this is an old post but it was the first one I found when searching for how to find if a class can be resolved by Ninject without actually calling get() and risking an exception.

Ninject version 3.0.2 have a method CanResolve which returns a boolean:

kernel.CanResolve<T>()

I got 3.0.2 from nuget but its currently market unstable (Ninject.3.0.2-unstable-9037) so I'm not sure if I use this in production just yet.

India answered 23/10, 2013 at 12:18 Comment(1)
Unfortunally, CanResolve does not look into the resolution tree. So if it is missing a binding on a second order dependency, it will not tell you.Holcomb

© 2022 - 2024 — McMap. All rights reserved.