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
22
votes
3 answers

Ninject and DataContext disposal

I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important…
Bas
  • 1,232
  • 1
  • 12
  • 26
22
votes
5 answers

' Sequence contains no elements' exception when initializing ninject

I have a problem with Ninject which I have not seen before and don't know how to fix. I am using the MVC NuGet package for MVC 5. In my NinjectWebCommon.cs, there is the following method: public static void Start() { …
serlingpa
  • 12,024
  • 24
  • 80
  • 130
22
votes
2 answers

Parameterless constructor error with Ninject bindings in .NET Web Api 2.1

Working currently on writing an API site (.NET Web Api 2.1) For our prior API sites we had used the Ninject.MVC3 package and wired up a dependency resolver and scope manually and plugged in our logic into NinjectWebCommon as per the…
Khepri
  • 9,547
  • 5
  • 45
  • 61
22
votes
1 answer

Is there a simple way to use Dependency Injection on my connections?

I'm looking for a way to inject my connections into my repositories. I tried to inject the SqlConnection using the IDBConnection, but I got some problems when NInject tries to deactivate the connection, the event is never called. And I cannot figure…
user2011100
21
votes
1 answer

Get an instance of an object with Ninject

I installed on my project Ninject.MVC3 via Nuget. I read this article that to inject dependencies in my controllers, all you had to do was install Ninject, add my dependencies in NinjectMVC3.cs and ready. So far so good, but how to retrieve the…
ridermansb
  • 10,779
  • 24
  • 115
  • 226
21
votes
3 answers

MVC 3 - how to implement a service layer, do I need repositories?

I am currently building my first MVC 3 application, using EF Code First, SQL CE and Ninject. I have read a lot about using Repositories, Unit of Work and Service Layers. I think I have got the basics sorted out, and I have made my own…
esba
  • 335
  • 2
  • 7
21
votes
1 answer

Does Ninject support Func (auto generated factory)?

Autofac automatically generates factories for Func; I can even pass parameters. public class MyClass { public MyClass(Func a, Func b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If…
user593358
21
votes
3 answers

How can Json.NET perform dependency injection during deserialization?

When I have a class with no default constructor, i.e. using dependency injection to pass its dependencies, can Newtonsoft.Json create such an object? For example: public class SomeFoo { private readonly IFooDependency _dependency; public…
zafeiris.m
  • 4,339
  • 5
  • 28
  • 41
21
votes
3 answers

Bind multiple implementations to the same interface with ninject

Why is it not possible for me to do the following in…
bizzehdee
  • 20,289
  • 11
  • 46
  • 76
21
votes
1 answer

How to use Ninject in WPF application

I'm familiar with MVC but I'm trying to learn WPF with Ninject. Can some one give me some example or reference?
novian kristianto
  • 751
  • 3
  • 12
  • 34
21
votes
4 answers

Accessing the Ninject Kernel Globally

This question is not specifically related to Ninject. It's more of a general coding question, but I'm posting it here in case there might be a better way entirely of handling the issue in Ninject, than what I am trying to do. I would like to know…
Fred Chateau
  • 869
  • 1
  • 6
  • 16
20
votes
2 answers

Can't get Ninject.Extensions.Interception working

I've been trying for ages to figure this our. when i try to bind my class with an interceptor i'm getting the following exception on the line Kernel.Bind().ToSelf().Intercept().With(); Error loading Ninject component…
Ryan Burnham
  • 2,619
  • 3
  • 27
  • 43
20
votes
5 answers

ASP.NET MVC 3 Site Loading Is Extremely Slow

I really don't know where to begin with this question, but the site I'm working on at times has some really slow page loads. Especially after doing a build, but not always. I usually have to refresh the page 5-10 times before it actually comes up.…
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
20
votes
2 answers

Ninject - binding constructors with arguments / Entity Framework connection string

Please forgive my ignorance, but I am very new to IOC and NinJect. I have searched for high and low for easily understandable solutions but so far they have eluded me. So far I have the following and all works as expected: private class…
JcMaltaDev
  • 1,344
  • 1
  • 9
  • 17
20
votes
2 answers

Ninject and MVC3: Dependency injection to action filters

I've found loads of inconclusive articles and questions on how to do property injection on an ActionFilter in ASP.NET MVC3 using Ninject. Could someone give me a clear example please? Here's my custom auth attribute. public class…
Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141