5

I am new in Ninject and I need some help to move on.

I have a solution that consists of web.form (presentation) and various other class libraries projects.

In web.form application inside NinjectWebCommon cs file I have the following

kernel.Bind<HttpContext>()
      .ToMethod(ctx => HttpContext.Current).InThreadScope();

kernel.Bind<HttpContextBase>()
      .ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();

kernel.Bind<MPIBE.DESTINATION.CORE.SiteContext>()
      .ToMethod(ctx => new MPIBE.DESTINATION.CORE.SiteContext(
                           new HttpContextWrapper(HttpContext.Current)
       ));

I am trying to get an instance of a class (following the constructor)

public SessionUtilities(SiteContext siteContext)
{
    _siteContext = siteContext;
}

and I noticed that i can get the instance only form web.forms application and I can't get from other projects (class libraries). Does this make any sense?

I am trying to get the instance via property injection

[Inject]
public SessionUtilities _sessionUtilities { get; set; }
Yuck
  • 49,664
  • 13
  • 105
  • 135
profanis
  • 2,741
  • 3
  • 39
  • 49

1 Answers1

12

I suspect the class that contains your _sessionUtilities property is being created with new instead of via Ninject.

Ninject will only inject your _sessionUtilities property if the containing instance is also created by Ninject, either because it is created with kernel.Get() or because it is itself being injected.

shamp00
  • 11,106
  • 4
  • 38
  • 81
  • The instance is created via property injection as shown above and not by using the "new". – profanis Oct 02 '13 at 07:17
  • I'm talking about the instance of _the containing class_. The class which contains the `_sessionUtilities` property. How is that created? – shamp00 Oct 02 '13 at 07:45
  • This class instantiated via new... so, this class needs to instantiated with ninject as well? – profanis Oct 02 '13 at 10:50
  • 3
    Yes! Dependency injection is [turtles all the way down](https://en.wikipedia.org/wiki/Turtles_all_the_way_down). – shamp00 Oct 02 '13 at 13:53
  • @shamp00 That's useful information. Is there a way to get an instance of a Kernel in a non Ninject created instance? I'm trying to inject into a colleagues class in a referenced project which has no access to the current Kernel. – Ian May 30 '14 at 16:27
  • Yes @Ian, it's possible via a static `ServiceLocator`, such as the one described in [this article](http://kohari.org/2008/06/18/playing-nice-with-service-locators/). However, [a service locator is considered a code smell](http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/), since your code is then tightly coupled to the service locator. It would be better to pass a `SessionUtilitiesProvider` class instead. – shamp00 Jun 02 '14 at 09:22
  • @shamp00: I've actually asked a question about this http://stackoverflow.com/questions/23989946/dependency-injection-into-referenced-project if you'd be up for providing an answer with a brief example of the SessionUtlitiesProvider? – Ian Jun 02 '14 at 09:25