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

One thought on “Ninject auto binding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s