ninject

Conditional dependency injection binding only when property not null

那年仲夏 提交于 2019-11-29 11:56:43
It is a desktop application which is obliged to impersonate the current user when accessing the underlying data source. How can I tell Ninject not to bind the dependency until the property of a parent object is not null? Application forces user authentication upon startup Once authenticated, a reference to the current user credentials is kept within the IMembershipService Accessing the underlying datasource obliges to have a user authenticated so that the commection string states the credentials to impersonate I am actually using NHibernate, and I need to dynamically create the connection

What's the difference between .ToConstructor and .ToMethod in Ninject 3?

心不动则不痛 提交于 2019-11-29 10:41:51
问题 In Ninject3 there's a new .ToConstructor feature. As described, it helps to strongly-type constructor arguments like: Bind<IMyService>().ToConstructor( ctorArg => new MyService(ctorArg.Inject<IFoo>(), ctorArg.Inject<IBar>())); What's actually the difference between using .ToConstructor and .ToMethod in an almost the same way: Bind<IMyService>().ToMethod( x => new MyService(x.Kernel.Get<IFoo>(), x.Kernel.Get<IBar>())); Is it just a syntax sugar to avoid using Kernel.Get<>() or is there

Can NInject load modules/assemblies on demand?

让人想犯罪 __ 提交于 2019-11-29 10:35:17
问题 Are there facilities in NInject that will allow me to load services from other modules (assemblies) on demand like it's done in Unity? 回答1: I'm pretty sure this is what you're looking for: var kernel = new StandardKernel(); kernel.Load( Assembly.Load("yourpath_to_assembly.dll"); If you look at KernelBase with reflector in Ninject.dll you will see that this call will recursively load all modules in the loaded assemblies (Load method takes an IEnumerable) public void Load(IEnumerable<Assembly>

How to instantiate a MEF exported object using Ninject?

我与影子孤独终老i 提交于 2019-11-29 09:39:06
问题 My application is using MEF to export some classes from an external assembly. These classes are setup for constructor injection. The issue I am facing is that MEF is attempting to instantiate the classes when I try to access them. Is there a way to have Ninject take care of the instantiation of the class? IEnumerable<Lazy<IMyInterface>> controllers = mefContainer.GetExports<IMyInterface>(); // The following line throws an error because MEF is // trying to instantiate a class that requires 5

How to use Ninject Conventions extension without referencing Assembly (or Types within it)

为君一笑 提交于 2019-11-29 09:11:47
Sorry in advance for the long question, it's long because I've been digging at this all day. The general problem: I have an ASP.Net MVC2 application with the following projects: MyApp.Web, MyApp.Services, MyApp.Data. We code to interfaces and utilize Ninject 2 for DI/IoC. However, I'm getting awfully tired of typing (and forgetting to type): Bind<ISomeService>.To<SomeService>; So, knowing about Ninject.Extensions.Convensions, I have attempted to use it to automatically scan and register modules and simple dependencies of the type IXxxx => Xxxx. What I tried that works (but isn't quite enough):

Is binding ToConstant and calling InSingletonScope redundant?

早过忘川 提交于 2019-11-29 09:07:20
Well, this question is pretty simply stated by the title. For a local variable factory : var factory = Fluently.Configure() ... Are these two lines equivalent: Bind<ISessionFactory>().ToConstant(factory).InSingletonScope(); and: Bind<ISessionFactory>().ToConstant(factory); Andrew Savinykh In the latest version of ninject, when you create a ToConstant binding it will automatically set the Scope to Singleton. Thus, the InSingletonScope() part in your example is redundant. From ninject code base: /// <summary> /// Indicates that the service should be bound to the specified constant value. /// <

Share a Kernel between WebAPI and MVC

六眼飞鱼酱① 提交于 2019-11-29 08:12:53
I've got a simple MVC4 site which uses ASP.NET webAPI and also MVC pages. I'd like to use Ninject DI for both controller types but I'm wondering how this can be done? I've got Ninject DI working for WebAPI, but now not sure how to share the same Kernel elegantly. Should I be using some sort of Kernel singleton which both Dependency Resolvers can refer to? Anyone had any experience with this? Cheers. You should use the same IKernel instance for a single application-level composition root, may be WebApi or MVC controllers. If you are using Ninject.MVC3 package: When the kernel is initialized in

Autofac: any way to resolve the innermost scope?

不想你离开。 提交于 2019-11-29 07:56:47
I'm currently trying out Autofac in a new ASP.NET MVC project after having used Ninject, Castle Windsor and other IoC containers in the last years. So while I know about IoC containers in general, I'm fairly new to Autofac and I'm still looking for some best practices. Currently I'm trying to find out if there is a way to resolve the innermost nested scope. I have the following situation: a component that is registered as SingleInstance() has a method that creates a nested lifetime scope, providing a configuration action to configure some components as InstancePerLifetimeScope, and within this

Dependency Injection with Ninject, MVC 3 and using the Service Locator Pattern

我们两清 提交于 2019-11-29 07:12:31
问题 Something that has been bugging me since I read an answer on another stackoverflow question (the precise one eludes me now) where a user stated something like " If you're calling the Service Locator, you're doing it wrong. " It was someone with a high reputation (in the hundred thousands, I think) so I tend to think this person might know what they're talking about. I've been using DI for my projects since I first started learning about it and how well it relates to Unit Testing and what not.

Upgrading Ninject/Ninject WCF Extensions to the latest version 3.0.0.5

浪子不回头ぞ 提交于 2019-11-29 06:45:20
I am currently using Ninject (2.2.1.4) and Ninject.Extensions.Wcf (2.2.0.4) with my WCF service. I would like to upgrade to Ninject (3.0.0.15) and Ninject.Extensions.Wcf (3.0.0.5) and it doesn't look like I can use my current approach anymore. Can anyone point me to some samples or posts on how to get the latest version of Ninject working with a WCF project. My current approach: I wrote a module: public class NinjectDependencyResolver : NinjectModule { public override void Load() { // Declare bindings } } I added the Factory Attribute to my .svc file Factory="Ninject.Extensions.Wcf