tags:

views:

165

answers:

2

I'm creating a tiny C# application, which currently consists of a core assembly and a winforms assembly. I realize I probably don't really need Ninject in a small thing like this, but I would like to try it out.

Anyways, to work with Ninject I have understood that you would write a set of modules, which maps class is returned and so on. After that you would create an instance of IKernel and load your modules into that.

But, where do I keep those modules? And where do I keep the kernel? Where do stuff go?

+2  A: 

You may create static wrapper class for kernel. That way you could do something like ServiceLocator.Resolve()

For registering services there are two ways: inline and module registration. Both of them should be loaded at bootstrapping. Module is better for organizing.

Maybe it would be easier to start with StructureMap because there is static class and it has auto mapping features.

Those screencasts should get you starting:

Marek Tihkan
+2  A: 

+1'd Marek's answer - definitely look through those resources.

Some points...

You're definitely right to try this, even in a small app. Its also important to think hard about superficially simple questions like the one you posed. For DI, you really do have to actually do some work with it to really appreciate it - I for one was in the "Oh, I've only got a small app" (denial) camp for a long time until I actually used it.

There's a school of though that one in general should be steering away from Service Locator and just having injection [without any dependencies on a container].

If you dont use Service Locators, nobody needs to know where the Container (Kernel) is, which is the best thing.

Modules are mainly for the purposes of compartmentalising batches of things to register in a particular overall Container (Kernel).

Surely there's a canonical 'Global Container' Singleton implementation out there for Ninject? EDIT: Just found one:- http://www.codethinked.com/post.aspx?id=8e162def-7b67-4bc1-9fa2-87bbeb356104

See also http://stackoverflow.com/questions/1651174/ninject-how-do-i-inject-into-a-class-library/1653708#1653708

Ruben Bartelink