How to get started

Apr 22, 2013 at 7:46 PM
Hello,

I consider myself advanced in the ways of DI, getting acquainted with this as a way of facilitating bootstrapper features.

I'd like to do as much by convention as possible, and have some control over what to wire up through modules.

I am using Ninject as my DI container.

My question is simply this: let's say I hook up my NinjectModules with various concerns throughout the project.

I'd like to instruct the bootstrapper to suck up all the modules or other concerns in assemblies starting with "SuchAndThus", possibly also that implement a given interface, along these lines.

I'd like to do this automagically, like I tell Bootstrapper.Start() and the rest wires up automatically.

I see the snippets of code, or possibly I am making too much out of the issue, that it really is just that simple.

Any pointers? Thank you...

Regards,

Michael Powell
Coordinator
Apr 22, 2013 at 7:56 PM
Hi Michael,

If you are using NinjectModules, all you have to say is
Bootstrapper.With.Ninject().Start();
Bootstrapper will "suck up" like you say, all the Ninject Modules located in any assembly that is loaded.
You can instruct Bootstrapper to look in specific assemblies using the Including feature like this
Bootstrapper.Including.
    Assembly(Assembly.GetAssembly(typeof(MyMapCreator))).
    AndAssembly(Assembly.GetAssembly(typeof(MyTasks))).
    Start();
There are other ways to do this but you can find out about them in the Ninject Extension Documentation Page.

I hope that helps.

Luis
Apr 22, 2013 at 10:05 PM
Edited Apr 22, 2013 at 10:27 PM
Maybe it's just me, I am trying this: Bootstrapper.With.Ninject().Start();

I've also verified the assemblies I have loaded in AppDomain.CurrentDomain include the ones that I have NinjectModule-derived classes, but their load methods are not happening. Or perhaps I am missing something. Is this done lazily, not at load time, but when instance(s) are resolved?

Edit: I end up with something like this, but I don't see my NinjectModule Load happening.
Bootstrapper.IncludingOnly.AssemblyRange(
  AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.StartsWith(@"SuchAndThus.")))
    .With.Ninject().Start();
Regards,

Michael
Apr 22, 2013 at 10:31 PM
Edited Apr 22, 2013 at 11:03 PM
I figured it out. It's a reference chaining thing I think. I need to verify that my assemblies, modules therein, and so on, are all referencing each other properly, then the modules load up properly.

Edit: Of course, it seems the only other caveat is: non-abstract direct-inheritance from NinjectModule. In other words, this doesn't seem to work: public abstract class FirstModule : NinjectModule { } public class SecondModule : FirstModule { }.

But for what DI is, this might be an acceptable trade off. Or get creative in how I wire up my assemblies, etc.
Apr 22, 2013 at 11:31 PM
Another stupid question: I've pre-loaded the referenced assemblies through some AssemblyName AppDomain magic, and now have a list of Assemblies that looks reasonable, closer to what I'd expect I have defined with modules strewn about the AppDomain. So... I am loading a single NinjectModule. The other two that I've defined do not get loaded. Any ideas why? The bootstrapper finds one and quits? Or it can be instructed to continue and find them all?
Apr 23, 2013 at 12:08 AM
luisbocaletti wrote:
Hi Michael,

If you are using NinjectModules, all you have to say is
Bootstrapper.With.Ninject().Start();
Bootstrapper will "suck up" like you say, all the Ninject Modules located in any assembly that is loaded.
You can instruct Bootstrapper to look in specific assemblies using the Including feature like this
Bootstrapper.Including.
    Assembly(Assembly.GetAssembly(typeof(MyMapCreator))).
    AndAssembly(Assembly.GetAssembly(typeof(MyTasks))).
    Start();
There are other ways to do this but you can find out about them in the Ninject Extension Documentation Page.

I hope that helps.

Luis
What am I missing?
Apr 23, 2013 at 12:20 AM
As long as I force-feed Bootstrapper with the assemblies I want, something like typeof(MyModule).Assembly, it can find the modules and load them up. Would be nice to do it more fluidly, more transparently, by convention, that's the whole point, without much in the way of AppDomain AssemblyName, Assembly.Load voodo. Will need to spend a little more time with the AppDomain issue, why I must pre-load referenced assemblies and such and see if I can't get that going. But for now it addresses the immediate concern: identifying assemblies and loading with the bootstrapper. Thanks much...
Coordinator
Apr 23, 2013 at 2:36 AM
Edited Apr 23, 2013 at 2:37 AM
I'm glad you figured it out for now.