views:

138

answers:

2

I am using .Net 3.5 and a console application that eventually will become a windows service.

Most of the examples I find use something like

Bind<IWeapon>().To<Sword>();

I have included all the DLL as references in my project and I the compiler is still complaining. Any clues of where I am going wrong? Sorry this might be a stupid question.

UPDATE: Just notice a whole lot of other libraries that seem to be needed but aren't referenced. There are libraries like Castle Core. Should these be included?

A: 

Try going into your project properties (right click project and click properties) and re-targeting your console application to .Net Framework 3.5, it might be saying something like ".Net Framework 3.5 Client Profile" currently. I've had this same problem in the past where assemblies wouldn't reference properly.

Hope that is the problem and that this helps.

Khalid Abuhakmeh
Hi. I had a quick look but that wasn't it. Thanks anyway.
uriDium
+3  A: 

The first thing you need to be sure you're doing is executing your binding code within a Ninject module inside of the Load method, which you override.

For example:

public class ApplicationModule : NinjectModule {

  public override void Load() {

    Bind<IWeapon>().To<Sword>(); 

    // additional bindings continue ...
  }
}

The NinjectModule class inherits classes and interfaces that define the Ninject fluent binding syntax thus making the Bind<T>() method available within the scope of the class. These modules are then passed to the Ninject kernel when the kernel is instantiated:

var kernel = new StandardKernel(new ApplicationModule()); 

The Load() method of each module passed to the kernel will then be called and the binding definitions contained within the methods executed.

The example above specifies NinjectModule as the base class; however, this is specific to version 2.0 of Ninject. If you are using Ninject 1.x, your base class will be StandardModule. In either case, the load method is overriden and binding statements (which are similar in both versions) are issued there.

To further answer your question:

  • For Ninject 1.x, the required DLL is ninject.core.dll and the namespace that StandardModule is defined in is Ninject.Core.

  • For Ninhect 2.0, the required DLL is ninject.dll and the the namespace that NinjectModule is defined in is Ninject.Modules.

In both cases, you may need to reference more namespaces depending on how complex your binding statements get -- i.e. if you use contextual bindings or scope your bindings, etc. In the case of Ninject 1.x, you also may need to reference Ninject.Conditions.dll for these more complex cases.

You do not need to reference Castle.Core unless you are using Ninject's interception feature -- which is a 1.x core feature but an extension in version 2.0.

Hope this helps.

Peter Meyer
Thanks. I can only bump you up once and select this as the right answer I wish I could do more. I struggled the whole afternoon. There is documentation about how everything hangs together but I struggled to find something for my scenario. Other tutorials on getting started all seemed to be missing when I went to the search result.
uriDium
Hey, no problem! I happen to use Ninject quite a bit and struggled for a long time when I was comparing all the IoC containers out there. Glad I could help.
Peter Meyer