Go Back

Is Ninject 2 Stealthy

Based on this documentation for Ninject , they claim to be stealthy!

 

Stating:

Ninject will not invade your code. You can easily isolate the dependency on Ninject to a single assembly in your project.”

 

Well, at first I was skeptical and just could not figure it out. So I put on my scientist hat for a little while in an attempt to prove them right!

 

Here’s what my test bed looked like… a pretty standard project configuration with multiple DLLs

 

  

 

 

  1. A console application. My driver program.
  2. A contracts DLL. My abstracts and interfaces that are known to the driver program.
  3. A service locator DLL. My services and Factories that will provide my driver program and class libraries everything they need.
  4. My first class library dll. This is where I would usually put some type of layered architecture or group of assemblies for a new release.
  5. My second class library dll. This is the same as above but separate from the previous one. 

 

   Now here are the references:  

 

1, 4, and 5 reference 2 and 3. (and every dll you ever make after this would reference 2 and 3)

3 references 4 and 5 (and any new dlls you ever make) 

 

Here’s the layout of where all the type definitions are so you can visualize it:

 

 

 

Here’s the driver program code. The test bed for this little experiment: 

using System;

using IsolatingTheNinja.Contracts;

using IsolatingTheNinja.Services;

 

namespace IsolatingTheNinja

{

    class Program 

    {

        static void Main(string[] args)

        {

            ILibrary1Type type1Instance = ServiceLocator.CreateLibrary1Type();

            ILibrary2Type type2Instance = ServiceLocator.CreateLibrary2Type();

 

            Console.WriteLine(type1Instance);

            Console.WriteLine(type2Instance);

            Console.WriteLine("Press any key to exit");

            Console.ReadKey();

        }

    }

} 

  

 

Here’s my Service Locator implementation:

using IsolatingTheNinja.Contracts;

using Ninject;

using Ninject.Modules;

 

namespace IsolatingTheNinja.Services

{

    public class ServiceLocator 

    {

        private static readonly IKernel _kernel = new StandardKernel(new INinjectModule[] { new Library1Module(), new

Library2Module() });

 

        public static ILibrary1Type CreateLibrary1Type()

        {

            return _kernel.Get<ILibrary1Type>();

        }

        public static ILibrary2Type CreateLibrary2Type()

        {

            return _kernel.Get<ILibrary2Type>();

        }

    }

}

  

 

Here’s an example of my module:

using IsolatingTheNinja.Contracts;

using IsolatingTheNinja.Library1;

using Ninject.Modules;

 

namespace IsolatingTheNinja.Services

{

    public class Library1Module : NinjectModule 

    {

        public override void Load()

        {

            Bind<ILibrary1Type>().To<Library1Type>();

        }

    }

}

 

The end result: SUCCESS. Ninject IS Stealthy! 

 Here’s the Source code

 

 

 

 

 

 

 

 

 

 

 

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Post a comment!
  1. Formatting options