You have to put the NinjectModule inside the domain assembly (and not the web assembly), and you can tell ninject about checking your domain assembly and finding the module. a module is just a class that inherits from the NinjectModule base class and implements Load (). Thus, a web project only needs a link to a domain project. The scan should look something like this:
any of these two: (there are actually a few more options, but these are the main ones that I use)
var assemblysToScan = "Assembly.*"; var assemblysToScan = new[] { "Assembly.Domain", "Assembly.Web" };
and then just
kernel.Scan(x => { x.From(assemblysToScan) x.AutoLoadModules(); });
If you want to allow an object, just put it as the ctor argument, and if ninject controls you objects, it automatically enters the type (based on the configured bindings).
EDIT:
add links to ninjectcommonservicelocator (don't remember the exact name) and microsoft.practices.servicelocation
in your boot block add the following:
using Microsoft.Practices.ServiceLocation; using CommonServiceLocator.NinjectAdapter; var locator = new NinjectServiceLocator(kernel); ServiceLocator.SetLocatorProvider(() => locator);
then in your class:
using Microsoft.Practices.ServiceLocation; class CookieManager : ICookieManager { SecureObjectSerialiser secureObjectSerialiser; CookieManager() { this.secureObjectSerialiser = ServiceLocator.Current.GetInstance<SecureObjectSerialiser>(); } }
The reason for the microsoft.practices locator is that your code should not know that ninject works under covers. instead, you use a generic servicelocator, which can be reused if you change containers.
personally, I would avoid this and just enter everything. but if you cannot, you cannot.
Aaronhs
source share