Advanced Automatic Ninject Bindings Advanced Automatic Ninject Bindings

You have a common interface that is used by multiple concrete classes and you donít want to specify each Ninject binding manually.


The solution to this problem is very similar to my previous post on Automatic Ninject Bindings because it also leverages the NuGet package Ninject.extensions.conventions.

In this example, letís assume you have a single interface called IService<T>.† This interface contains a generic reference to a class or object (e.g. a database model).† Then you have multiple classes that implement the interface.† E.g. UserService : IService<User>.

With the following line of code, all of your classes that implement the IService interface will automatically bind to the corresponding class:


 ††††††††††† var kernel = new StandardKernel();
 ††††††††††† kernel.Bind(x =>
 ††††††††††† {
 †††† †††††††††††x.FromThisAssembly()
 ††††††††††††††††††† .SelectAllClasses()
 ††††††††††††††††††† .InheritedFrom(typeof(IService<>))
 ††††††††††† ††††††††.BindSingleInterface();
 ††††††††††† });
 

Implementing Ninject.extensions.conventions

The above solution works extremely well when you have many services, repositories, or perhaps behaviors that apply the Command Pattern where there is a single interface that many classes can leverage.

To further the above solution, here is some example code the provides the complete picture.

Basic IService.cs example:


 using System;
 namespace UnitTestProject3
 {
   public interface IService<T>
   {
     T Get(int id);
   }
 }
 

Basic UserService.cs example:


 using System;
 using System.Collections.Generic;
 namespace UnitTestProject3
 {
    public class UserService : IService<object>
    {
       private readonly List<object> _users;
       public UserService(List<object> users)
       {
          _users = users;
       }
       public object Get(int id)
       {
          return _users[id];
       }
   }
}

And here is a single unit test asserting our binding:


 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Ninject;
 using Ninject.Extensions.Conventions;
 namespace UnitTestProject3
 {
    [TestClass]
    public class UnitTest1
    {
       [TestMethod]
       public void TestMethod1()
       {
          var kernel = new StandardKernel();
          kernel.Bind(x =>
           {
             x.FromThisAssembly()
            .SelectAllClasses()
            .InheritedFrom(typeof(IService<>))
            .BindSingleInterface();
           });
           var userService = kernel.Get<IService<object>>();
           Assert.IsInstanceOfType(userService, typeof(UserService));
       }
    }
 }
 

Published on Jan 13, 2014

Tags: Uncategorized | ASP.NET MVC and Web API Tutorial | c# | ninject

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.