ninject

Ninject: What does it mean to bind something to itself?

断了今生、忘了曾经 提交于 2019-12-07 02:55:00
问题 Ninject has the functionality of self binding like Bind<Samurai>().ToSelf(); I read about this but I don't get the importance or how this can be useful. Any comments are appreciated. 回答1: If Ninject finds a object that needs to be created and it has a constructor that has a Samurai parameter it does not know how to instantiate it. But when you use Bind<Samurai>().ToSelf(); then Ninject knows that a Samurai needs to be created to pass to the Samurai parameter. If that binding was not there

How to create a Ninject custom scope that returns the same object until that object is disposed?

霸气de小男生 提交于 2019-12-07 02:49:08
问题 In Ninject, declaring a binding in singleton scope means that the same object will be returned every time. There can only be one object, ever. What I would like is to return one object at a time. In other words: The first call to Get() instantiates a new object and returns it. Subsequent calls to Get() return the same instance. The object is disposed. The first call to Get() after the object was disposed instantiates a new/second object and returns that. Subsequent calls to Get() return the

NamedScopes Ninject bindings and async (threading)

时光总嘲笑我的痴心妄想 提交于 2019-12-07 02:26:07
问题 My project is structured by services and repositories (all repositories share the db context). In one of my service layers, I have an asynchronous method that writes to the database using a repository. The web request will finish and dispose of the context before this method can get to use it. I tried to understand NamedScopes as stated in this answer. I still can't seem to understand how to implement it. I'll show how my project is structured and hope someone can help me at the code level.

MVC3, Ninject, MvcSiteMapProvider - How to inject dependency to overridden method

孤街醉人 提交于 2019-12-07 00:08:13
问题 I have an MVC3 application that is using Ninject and MvcSiteMapProvider. I have created this class which MvcSiteMapProvider uses to dynamically add nodes to my sitemap: public class PageNodeProvider : DynamicNodeProviderBase { public override IEnumerable<DynamicNode> GetDynamicNodeCollection() { // need to get repository instance var repository = // how do I get this??? foreach (var item in repository.GetItems()) { yield return MakeDynamicNode(item); } } } The MvcSiteMapProvider instantiates

Are controller factories neccessary when using Ninject in ASP.NET mvc 4

二次信任 提交于 2019-12-07 00:00:44
问题 I am at a loss of what to do with the multitude of documentation available through google in .net as regards using Ninject with asp.net mvc 4 First of all, i want to know if Controller factories are neccessary in asp.net. Also, is constructor injection really the only way we can do dependency injection with MVC 4 because property injection and method injection does not seem to work when i use them with my controllers 回答1: I am not an expert on Ninject but as far as i know, i am only using it

Ninject Multicasting

删除回忆录丶 提交于 2019-12-06 23:11:17
问题 I want to bind multiple implementations of a service and have all of them called at once: var kernel = new StandardKernel(); kernel.Bind<IBreakfast>.To<Spam>(); kernel.Bind<IBreakfast>.To<Eggs>(); kernel.Bind<IBreakfast>.To<MoreSpam>(); kernel.Get<IBreakfast>().Eat(); // call Eat method on all three bound implementations Ninject doesn't like that, and will throw an exception about having multiple bindings. Is there a way I can get around that error, and have all the implementations called?

How do you organise your NInject modules?

只愿长相守 提交于 2019-12-06 19:01:23
问题 NInject's module architecture seems useful but I'm worried that it is going to get in a bit of a mess. How do you organise your modules? Which assembly do you keep them in and how do you decide what wirings go in which module? 回答1: Each subsystem gets a module. Of course the definition of what warrants categorisation as a 'subsystem' depends... In some cases, responsibility for some bindings gets pushed up to a higher level as a lower-level subsystem/component is not in a position to make a

How to resolve error :the type does not appear to implement microsoft.practices.servicelocation.iservicelocator?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 17:52:43
问题 I am new to MVC, i am following " PRO ASP.NET MVC 4 by Adam Freeman ". I am currently working on its 6th chapter. In which i am learning how to use Ninject in MVC 4 for Dependency Injection. I have created the application as described in the book. Now i am not getting why the following Error Comes: the type does not appear to implement microsoft.practices.servicelocation.iservicelocator Here is my Controller code: public class HomeController : Controller { private Product[] products = { new

How can Json.NET perform dependency injection during deserialization?

£可爱£侵袭症+ 提交于 2019-12-06 17:13:58
问题 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 SomeFoo(IFooDependency dependency){ if(dependency == null) throw new ArgumentNullException("dependency"); _dependency = dependency; } public string Data { get; set; } public int MoreData { get; set; } public void DoFoo(){ Data = _dependency.GetFooData();

Ninject InRequestScope missing

自古美人都是妖i 提交于 2019-12-06 16:31:57
问题 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<DataContext>().To<MoneywatchDataContext>() but could not find InRequestScope method ended up doing this, Bind<DataContext>().To<MoneywatchDataContext>().InScope(ctx => HttpContext.Current) Just wanted to find out if: If this will behave exactly like the InRequestScope Method. That it will gurantee