I can't seem to find the right answers in several related questions here.
I'm learning Ninject from an article (created a console app in C#), I created a class I named DILoader and inherited NinjectModule:
public class DILoader : NinjectModule
{
public override void Load()
{
Bind<ICustomer>().To<RetailCustomer>();
Bind<ICustomer>().To<WholesaleCustomer>();
}
}
So in the start up:
static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly()); }
My question is, the Load() method above is triggered. How does this happen behind the scene when NinjectModule has an abstract Load() method? Where is this triggered?
Thanks
Here: https://github.com/ninject/Ninject/blob/master/src/Ninject/Modules/NinjectModule.cs
Inside the NinjectModule,
/// <summary>
/// Called when the module is loaded into a kernel.
/// </summary>
/// <param name="kernelConfiguration">The kernel configuration that is loading the module.</param>
/// <param name="settings">The ninject settings.</param>
/// <exception cref="ArgumentNullException"><paramref name="kernelConfiguration"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="settings"/> is <see langword="null"/>.</exception>
public void OnLoad(IKernelConfiguration kernelConfiguration, INinjectSettings settings)
{
Ensure.ArgumentNotNull(kernelConfiguration, nameof(kernelConfiguration));
Ensure.ArgumentNotNull(settings, nameof(settings));
this.KernelConfiguration = kernelConfiguration;
this.Components = this.KernelConfiguration.Components;
this.Settings = settings;
this.Load();
}
And then in the KernelConfiguration implementation you can see it calling module.OnLoad(...) here,
/// <summary>
/// Loads the module(s) into the kernel.
/// </summary>
/// <param name="modules">The modules to load.</param>
/// <exception cref="ArgumentNullException"><paramref name="modules"/> is <see langword="null"/>.</exception>
public void Load(IEnumerable<INinjectModule> modules)
{
Ensure.ArgumentNotNull(modules, nameof(modules));
modules = modules.ToList();
foreach (INinjectModule module in modules)
{
if (string.IsNullOrEmpty(module.Name))
{
throw new NotSupportedException(ExceptionFormatter.ModulesWithNullOrEmptyNamesAreNotSupported());
}
if (this.modules.TryGetValue(module.Name, out INinjectModule existingModule))
{
throw new NotSupportedException(ExceptionFormatter.ModuleWithSameNameIsAlreadyLoaded(module, existingModule));
}
module.OnLoad(this, this.Settings);
this.modules.Add(module.Name, module);
}
foreach (INinjectModule module in modules)
{
module.OnVerifyRequiredModules();
}
}
Have a look at the source and you can trace it around a bit more thoroughly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.