ninject

Ninject to bind on different controllers

做~自己de王妃 提交于 2019-11-26 21:28:34
问题 I am trying to Bind two concrete classes to one interface. What command should I use in Ninject to do that? What I am trying to do is to Bind two concrete classes to one interface base on the controller Name. Is that possible? I suppose that in ninject you use the .When to give the conditional but there is no tutorial out there where they show you how to use the .When for ninject. 回答1: Here are few examples. Check out Ninject source project and its Tests subproject for various samples of

How to use Ninject with ASP.NET Web API?

烈酒焚心 提交于 2019-11-26 20:39:12
问题 In MVC I simply make the class NinjectControllerFactory that implements DefaultControllerFactory interface then do some bindings in it. at last in Global I run it: ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); But what about using Ninject in ASP.NET Web API? there are some information on the web but are out dated and for pre-released versions. Is there a straightforward way for this problem? 回答1: The reason a lot of the articles are old is because the

Ninject doesn't call Dispose on objects when out of scope

为君一笑 提交于 2019-11-26 20:09:17
问题 I was surprised to find that at least one of my objects created by Ninject is not disposed of at the end of the request, when it has been defined to be InRequestScope Here's the object I'm trying to dispose: Interface: public interface IDataContext : IDisposable { MessengerEntities context { get; set; } } MessengerEntities is Entity Framework's implementation of ObjectContext -- my context object. Then I create a concrete class like so: public class DataContext : IDataContext { private

How the binding are done with decorators using Ninject?

对着背影说爱祢 提交于 2019-11-26 18:24:34
问题 Based on this question : Should thoses kind of service go injected in a base class ? (versus static classes). How the binding would be done with decorators using Ninject ? or any DIContainer ? public class CachedLoggedRepository : IRepository { public IRepository repository { get; set; } void Add(); } public class CachedRepository : IRepository { public IRepository repository { get; set; } void Add(); } public class Repository : IRepository { void Add(); } 回答1: You have to use conditional

Does Ninject support Func (auto generated factory)?

我的梦境 提交于 2019-11-26 17:35:07
Autofac automatically generates factories for Func<T> ; I can even pass parameters. public class MyClass { public MyClass(Func<A> a, Func<int, B> b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If not, what workaround can I apply? Thanks. Update : Just found this post, seems the answer is no: How do I handle classes with static methods with Ninject? Remo Gloor NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https://github.com/ninject/ninject.extensions.factory/wiki EDIT: NB there is a Bind<T>().ToFactory()

Ninject Binding Attribute to Filter with Constructor Arguments

 ̄綄美尐妖づ 提交于 2019-11-26 16:50:38
问题 I read as many answers as I could for this, but they seem to fall short of one detail. The trouble is when binding an action filter (with a service injected by controller) to a corresponding attribute, I've been unable to figure out how to pass parameter/property values from the attribute to its bound filter. Below is the code, and below that my intended fake-code: Filter & Attribute public class AuthorizationFilter : IAuthorizationFilter { private readonly IAuthorizationService

MVC3 + Ninject - How to?

坚强是说给别人听的谎言 提交于 2019-11-26 16:43:08
问题 I've just started playing with IoC containers and therefore chosed Ninject. After several hours of sweat and tears I still cant figure out how to setup my MVC3 application with Ninject. So far I have: Global.asax.cs public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}

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

徘徊边缘 提交于 2019-11-26 16:41:40
问题 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 recommendation. This was causing heartburn int our new project with the parameterless constructor error. In the past, this was a dead give away that we were not wiring up the dependency resolver in Web Api properly. Only this time, we were. It was there. var

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

不想你离开。 提交于 2019-11-26 16:21:22
问题 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: IAmAnInterface, IAmAnInterfaceToo { public IAmAnImplementation() { } public void DoSomething() { } public void DoSomethingElse() { } } Now I bind the same class to both interfaces using Ninject. Since I want the same instance of IAmAnImplementation beeing used

Creating an instance using Ninject with additional parameters in the constructor

ぐ巨炮叔叔 提交于 2019-11-26 15:08:14
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 create an instance of this class with Ninject (I dont want to hardwire this int, I want to pass it every time I get an instance)? Here's some code illustrating the situation: interface IService { void Func(); } class StandardService : IService { public void Func() { Console.WriteLine("Standard"); } } class AlternativeService : IService { public void