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
50
votes
2 answers

What does WebActivator do?

This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs was created and placed in App_Start folder. Does this mean it automatically…
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
48
votes
3 answers

NInject: Where do you keep your reference to the Kernel?

I'm using NInject on a new web application and there are two things that are unclear to me: Don't I need to keep a reference to the Kernel around (Session/App variable) to insure that GC doesn't collect all my instances? For example, if I specify…
JC Grubbs
  • 39,191
  • 28
  • 66
  • 75
48
votes
2 answers

Ninject + Bind generic repository

I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null? I have tried various things like: Bind(typeof(IRepository)).To(typeof(Repository));…
ebb
  • 9,297
  • 18
  • 72
  • 123
46
votes
3 answers

Translate ninject ISecureDataFormat binding to Autofac

I am migrating a large codebase from Ninject to Autofac and am struggling on one of the bindings that I believe is causing an activation error based on some of my debugging. Ninject: Bind>() …
O'Mutt
  • 1,557
  • 1
  • 13
  • 27
42
votes
4 answers

NInject with Generic interface

I have defined one interface and one class: public interface IRepository { } public class RoleRepository:IRepository { } Inject here: public RoleService { [Inject] public RoleService(IRepository rep) …
Sam Zhou
  • 715
  • 2
  • 7
  • 9
40
votes
1 answer

Is it possible to bind different interfaces to the same instance of a class implementing all of them?

I have the following (simplified) situation: I have two interfaces interface IAmAnInterface { void DoSomething(); } and interface IAmAnInterfaceToo { void DoSomethingElse(); } and a class implementing both: class IAmAnImplementation:…
Silas
  • 1,140
  • 11
  • 12
39
votes
2 answers

Dependency injecting UserStore in OWIN startup using Ninject OWIN middleware

I am having problems creating a custom UserStore using dependency injection when creating an ApplicationUserManager using the OWIN request pipeline. Background I am trying to migrate the user functionality in our web application from using the…
RiddleMeThis
  • 851
  • 1
  • 8
  • 18
39
votes
1 answer

Where should I do Injection with Ninject 2+ (and how do I arrange my Modules?)

I have a solution with two relevant (to this question) projects, and a few others; Class library with functionality used by several other projects. ASP.NET MVC application. My question is basically where I should do IoC with Ninject 2,…
Rune Jacobsen
  • 9,907
  • 11
  • 58
  • 75
39
votes
17 answers

ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object

UPDATE - Please look at my answer for a link and explanation of the solution to this problem Before we start, I know this is a very common question and I've been using Ninject for many moons without issues, but now it's come up and I can't figure…
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
37
votes
6 answers

Configuring Ninject with Asp.Net MVC & Web Api

i have setup my project with Ninject IoC. My project has regular Asp.Net MVC controllers and Web Api controllers. Now, Ninject works with Web Api but Ninject doesn't work with regular Asp.MVC controllers. My regular MVC controller…
35
votes
5 answers

Error Handling in asp.net mvc 3

Is there a built in or a proper way to handle errors in asp.net mvc 3? This is what I want to do: If the application crashes, or throws an error, it goes to a specific error page. I can throw my own error from the controller action. (and it goes to…
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
35
votes
15 answers

Ninject MVC3 - bootstrapper throwing "Already Initialized" exception

I've created an empty Asp.Net MVC3 project, and used nuget install-package Ninject.MVC3 Without doing anything else (no services registered and not even a controller created) I run the application. The Project breaks on line 22 in NinjectMVC3.cs…
Yngve B-Nilsen
  • 9,606
  • 2
  • 36
  • 50
35
votes
2 answers

With.Parameters.ConstructorArgument with ninject 2.0

How to use this functionality in ninject 2.0? MyType obj = kernel.Get(With.Parameters.ConstructorArgument("foo","bar")); The "With" isn't there :(
andrecarlucci
  • 5,927
  • 7
  • 52
  • 58
34
votes
1 answer

Ninject dependency injection with Decorator pattern

Say, I have such classes hierarchy: public interface IRepository { } public class SomeSimpleRepository : IRepository {} Now I want to "decorate" SomeSimpleRepository with additional functions public class MoreAdvancedRespository : IRepository { …
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
34
votes
3 answers

Ninject InRequestScope missing

Have a couple of questions regarding the latest version (2.2.1.4) of ninject. Was trying to Bind a Linq2sql DataContext to a concrete implementation InRequestScope (in a class library project) Bind().To() but…
Raghu
  • 2,678
  • 2
  • 31
  • 38