Ninject auto binding

This is called scan by convention in StructureMap, I’m surprised it’s not in standard Ninject package, while this feature call be found in Ninject.extension.conventions.

OK, let go get it. git clone, compile failed due to whole bunch of silverlight stuff. Weird, I gave up, time to try the powerful NuGet.

I would say NuGet is amazing, dependency lookup just like gem in ruby, libs are downloaded into packages folder.? OK, anyway, move it after if you don’t like it.

How to get the history version from NuGet?

One frustrated thing in VisualStduio is, the version number in project file (csproj) still keeps untouched, the problem turned to compile passed, but unit-test running into an error, can not find the assembly in old version.

Old version is not in GAC, I had to change those out-of-date csproj manually, the problem went away. VisualStudio Bug?

Ninject auto binding code:

            var standardKernel = new StandardKernel();

            // Manually set bind for those classes not folowing convention
            standardKernel.Bind().ToSelf();
            standardKernel.Bind().To();

            standardKernel.Scan(x =>
                {
                    x.FromAssembliesMatching("*");
                    x.BindWith();
                });

About generic:

For closing generic implementation like EmailRepository : IRepository<Email>, we can use

a.BindWith(new GenericBindingGenerator(typeof(IRepsitory<>)));

Even more advanced, checkĀ  this post about multi generic binding.

For open generic (Jeremy Miller is not recommanding this approach though.) like Repository<Email> : IRepository<Email>, the trick is:

Bind(typeof(IRepository<>).To(typeof(Repository<>)).
Advertisements