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
319
votes
7 answers

How do the major C# DI/IoC frameworks compare?

At the risk of stepping into holy war territory, What are the strengths and weaknesses of these popular DI/IoC frameworks, and could one easily be considered the best? ..: Ninject Unity Castle.Windsor Autofac StructureMap Are there any other…
ocodo
  • 29,401
  • 18
  • 105
  • 117
106
votes
5 answers

Ninject vs Unity for DI

We are using ASP.net MVC. Which of these is the best DI framework Ninject or Unity and why?
Miral
  • 5,968
  • 16
  • 57
  • 85
98
votes
9 answers

Combining DI with constructor parameters?

How do I combine constructor injection with "manual" constructor parameters? ie. public class SomeObject { public SomeObject(IService service, float someValue) { } } Where IService should be resolved/injected by my DI container, and…
George R
  • 3,784
  • 3
  • 34
  • 38
90
votes
7 answers

Ninject + MVC3 = InvalidOperationException: Sequence contains no elements

I created a new MVC3 project, hit F5, saw the sample page. Then I used NuGet to get the Ninject.MVC extension. I modified my global.asax according to the Ninject documentation, How To Setup an MVC3 Application: public class MvcApplication :…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
82
votes
6 answers

How can I implement Ninject or DI on asp.net Web Forms?

There are plenty of examples for having it worked on an MVC application. How is it done on Web Forms?
nellbryant
  • 3,109
  • 4
  • 20
  • 16
72
votes
8 answers

MVC5, Web API 2 and Ninject

I have created a new MVC5 project with Web API 2, I then added the Ninject.MVC3 package from NuGet. Constructor injection is working fine for the MVC5 controllers, but i am getting an error when trying to use it with the Web API Controllers. An…
Declan
  • 1,850
  • 1
  • 18
  • 23
71
votes
10 answers

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Using EF5 with a generic Repository Pattern and ninject for dependency injenction and running into an issue when trying to update an entity to the database utilizing stored procs with my edmx. my update in DbContextRepository.cs is: public…
Juan
  • 1,144
  • 1
  • 11
  • 16
69
votes
3 answers

Using HttpContext.Current in WebApi is dangerous because of async

My question is a bit related to this: WebApi equivalent for HttpContext.Items with Dependency Injection. We want to inject a class using HttpContext.Current in WebApi area using Ninject. My concern is, this could be very dangerous, as in WebApi…
Lukas K
  • 6,037
  • 4
  • 23
  • 31
68
votes
3 answers

How to use DI container when OwinStartup

It's a Web API 2 project. When I implement DI using Ninject, I got an error message An error occurred when trying to create a controller of type 'TokenController'. Make sure that the controller has a parameterless public constructor. [assembly:…
E-Cheng Liu
  • 968
  • 1
  • 12
  • 16
64
votes
1 answer

Creating an instance using Ninject with additional parameters in the constructor

I decided to start using Ninject and face an issue. Say I have the following scenario. I have an IService interface and 2 classes implementing this interface. And also I have a class, which has a constructor getting IService and an int. How can I…
StuffHappens
  • 6,457
  • 13
  • 70
  • 95
60
votes
3 answers

What is the intention of Ninject modules?

I'm a complete newbie to ninject I've been pulling apart someone else's code and found several instances of nInject modules - classes that derive from Ninject.Modules.Module, and have a load method that contains most of their code. These classes are…
Jonathan
  • 32,202
  • 38
  • 137
  • 208
60
votes
4 answers

What is Ninject and when do you use it?

I have been helping a few friends on a project and there is a class that uses Ninject. I am fairly new to C# and I have no idea what that class is doing, which is why I need to understand Ninject. Can anyone explain what Ninject is and when does one…
bachkoi32
  • 1,426
  • 4
  • 20
  • 31
56
votes
4 answers

Dependency Injection with Ninject and Filter attribute for asp.net mvc

I'm writing a custom Authorization Filter for asp.net mvc 3. I need to inject a userservice into the class but I have no idea how to do this. public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter { private IUserService…
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
52
votes
7 answers

Ninject sample application?

I want to get started using Dependency Injection and IOC and I want to get more into using Ninject. Are there any good application with sourcecode available that is written using Ninject that I can look at as a reference?
Andy
52
votes
1 answer

Ninject: Registering an already created instance with Ninject?

Can anyone tell me if i can register an already created instance of a class with Ninject so that it will use this instance everytime it needs injecting? I suppose you can call it a singleton but I have the instance already created. All the…
Martin
  • 23,844
  • 55
  • 201
  • 327
1
2 3
99 100