Questions tagged [ninject]

NInject is a dependency injection framework for .NET applications.

Ninject is a lightweight dependency injection framework for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software’s architecture, your code will become easier to write, reuse, test, and modify.

Ninject works with different types of .NET projects including Forms, WPF/Silverlight, WP7 and MVC.

As an example, the MVC implementation allows interfaces (dependencies) to be added to a controller's constructor parameters, and also into Filters. Ninject uses a library WebActivator for .NET to inject specified implementations into the constructors at the right time.

Installation

Installing Ninject can most easily be done using its NuGet package:

Install-Package ninject
3869 questions
25
votes
1 answer

Ninject doesn't call Dispose on objects when out of scope

I was surprised to find that at least one of my objects created by Ninject is not disposed of at the end of the request, when it has been defined to be InRequestScope Here's the object I'm trying to dispose: Interface: public interface IDataContext…
Cynthia
  • 2,100
  • 5
  • 34
  • 48
25
votes
3 answers

Which is a good approach to test Ninject bindings?

We use ninject in all our projects, and as you will know, sometimes it becomes hard to test if the kernel would be able to resolve every type at execution time, because sometimes control gets lost when the magnitude of bindings and autobindings…
Pato
  • 679
  • 8
  • 24
25
votes
3 answers

What happens to using statement when I move to dependency injection

I am currently using the following code: public class MyProvider { public MyProvider() { } public void Fetch() { using (PopClient popClient = new PopClient()) { .... } } } Because I want…
Thomas
  • 5,888
  • 7
  • 44
  • 83
25
votes
1 answer

How to handle DBContext when using Ninject

I am trying to use Ninject and OpenAccess for the first time. Please help me with the following. Here is what my project looks like... public class ContentController : Controller { private ContentService contentSvc; public…
eadam
  • 23,151
  • 18
  • 48
  • 71
24
votes
1 answer

Is binding ToConstant and calling InSingletonScope redundant?

Well, this question is pretty simply stated by the title. For a local variable factory: var factory = Fluently.Configure() ... Are these two lines…
joshperry
  • 41,167
  • 16
  • 88
  • 103
24
votes
7 answers

Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0' or one of its dependencies

I am adding Ninject in MVC project using the following commands in Package Manager Console: Install-Package Ninject -version 3.0.1.10 Install-Package Ninject.Web.Common -version 3.0.0.7 Install-Package Ninject.MVC3 -Version 3.0.0.6 When I run the…
Dani Petrick
  • 359
  • 2
  • 4
  • 13
24
votes
4 answers

IoC (Ninject) and Factories

If I have the following code: public class RobotNavigationService : IRobotNavigationService { public RobotNavigationService(IRobotFactory robotFactory) { //... } } public class RobotFactory : IRobotFactory { public IRobot Create(string…
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
23
votes
1 answer

Autofac equivalent of Ninject's WhenInjectedInto()

So we're working on converting some projects at work from Ninject to Autofac, and we've stumbled on something really neat within Ninject that we can't figure out how to do in Autofac. In our application, we have an interface called ISession which…
23
votes
1 answer

Ninject syntax for "Bind" with multiple arguments

How I can use multiple parameters in Ninject syntax like following? Bind() .To() .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString ); What if more than…
rem
  • 16,745
  • 37
  • 112
  • 180
23
votes
6 answers

How to initialize an object using async-await pattern

I'm trying to follow RAII pattern in my service classes, meaning that when an object is constructed, it is fully initialized. However, I'm facing difficulties with asynchronous APIs. The structure of class in question looks like following class…
Haspemulator
  • 11,050
  • 9
  • 49
  • 76
23
votes
2 answers

Ninject WithConstructorArgument : No matching bindings are available, and the type is not self-bindable

My understanding of WithConstructorArgument is probably erroneous, because the following is not working: I have a service, lets call it MyService, whose constructor is taking multiple objects, and a string parameter called testEmail. For this string…
23
votes
7 answers

Specific down-sides to many-'small'-assemblies?

I am planning out some work to introduce Dependency Injection into what is currently a large monolithic library in an attempt to make the library easier to unit-test, easier to understand, and possibly more flexible as a bonus. I have decided to use…
jerryjvl
  • 19,723
  • 7
  • 40
  • 55
23
votes
1 answer

How to bind Generic-type interfaces in Ninject

I'm fairly new to Ninject, and found myself stumbling when I came to implement a generic repository pattern. I want to bind a dependency IRepository to a class ConcreteRepository where ConcreteRepository implements…
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
22
votes
9 answers

Ninject.MVC5 not generating NinjectWebCommon.Cs

I'm developing a MVC5 project on Visual Studio 2017 Version 15.4. I'm getting unexpected result here what I never faced before. I've installed Ninject.MVC5 package from nuget. It's installing nicely and not giving any error or warning. But problem…
Jaber Kibria
  • 932
  • 3
  • 9
  • 29
22
votes
3 answers

When to use Singleton vs Transient vs Request using Ninject and MongoDB

I'm not quite sure when I should use SingletonScope() vs TransientScope() vs RequestScope() when I do my binding in my global.cs file. I have for example my call to MongoSession (using NoRM and the mvcStarter project http://mvcstarter.codeplex.com/)…
VinnyG
  • 6,883
  • 7
  • 58
  • 76