Monday, August 1, 2016

Using Ninject with Generic Types

I have been using Ninject for years to bind Interfaces to Classes, or even Classes to instances of Classes, but I had never earlier bound a generic Interface to a generic Class!  In my case, I was creating a Generic Repository class and needed to bind it using Ninject.

After doing some searching through StackOverflow discussions, I came across this solution which worked well:



As you can tell from the above code sample, the only main difference in the code base is to use the typeof casting operator to get the generic type to resolve correctly!

No comments:

Post a Comment