Overriding the default constructor parameters using Ninject

I use Ninject for my IoC Container in most of my projects, at this time.

When setting up my infrastructure layer, it is pretty common that I’ll need to send along some value that isn’t the default.

To send a non-default class, such as the case where I want to send a DbContext other than the default, I use this method:


//MyEntitiesClass is generated by the EDMX in my domain layer.
kernel.Bind<IMyRepository>().To<MyRepository>()
                .WithConstructorArgument("dbContext", kernel.Get());

//assume myConnString has been loaded from web.config just before this is called.
kernel.Bind<IMyRepository>().To<MyRepository>()
                .WithConstructorArgument("connString", myConnString);

That is pretty simple.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s