ninject

Multiple database contexts when using repository pattern

谁说胖子不能爱 提交于 2019-12-05 08:31:46
问题 I am a bit lost right now... I've never seen this much divergent information regarding solution to the problem. But let us start from the beginning. I am using ASP.NET MVC with Repositories injected to Controllers, thanks to the Ninject. I have 2 simple Entities: Admin with a list of created blog entries and Entries with one virtual Admin field. Admin: public class Admin { [Key, ScaffoldColumn(false)] public int Id { get; set; } [Required(ErrorMessage = "Zły login.")] [StringLength(20),

How do I get a new object using Ninject as in this example

百般思念 提交于 2019-12-05 07:32:19
问题 I am in need of adding an item of type IVehicle which is injected at runtime from constructor to a for loop. IVehicle vehicle; for (int i=0;i<=someValue;i++) { list.insert(i,vehicle); //some processing to assign values } now because Ivehicle is already injected by this time, my list has same value despite the value on view different and coming through the controller. How can I new up this object everytime EDIT The best way to new up this object everytime I found was to request new from the

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

拥有回忆 提交于 2019-12-05 06:46:44
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. 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 then ninject didn't know what to pass, for example there might have been a SamuraiSubClass type. But by

Convention based binding in ASP.NET 5 / MVC 6

让人想犯罪 __ 提交于 2019-12-05 06:21:05
It is possible to register dependencies manually: services.AddTransient<IEmailService, EmailService>(); services.AddTransient<ISmsService, SmsService>(); When there are too much dependencies, it becomes difficult to register all dependencies manually. What is the best way to implement a convention based binding in MVC 6 (beta 7)? P.S. In previous projects I used Ninject with ninject.extensions.conventions . But I can't find a Ninject adapter for MVC 6. No, there is no support for batch registration in the ASP.NET 5 built-in DI library. As a matter of fact, there are many features that are

How to handle async calls with Ninject InRequestScope?

冷暖自知 提交于 2019-12-05 05:55:51
We are using Ninject in an ASP.NET Web Api application, and we bind our DbContext with InRequestScope . This works well with most of our requests, because they do all their work synchronously, so the context can be safely disposed after the request is completed. However, we have on request in which we do an asynchronous web service call, that has a continuation method passed as a callback, and that callback method needs to use the database context. However our request shouldn't wait for the asynchronous service call to finish, but return immediately (this is an explicit requirement). Here is a

Ninject Multicasting

天大地大妈咪最大 提交于 2019-12-05 05:24:04
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? Also, the Bind<> calls can be in different projects which may or may not be loaded at run-time, so

How do I inject into some generic asp.net http handler using Ninject?

╄→гoц情女王★ 提交于 2019-12-05 05:10:13
I'm a newbie using Ninject and I can't figure out how to inject into my generic http handler. I have a MVC3 project and I'm injecting my services into controllers with no problem at all. This is what I got in my Ninject App_start class for registering services: private static void RegisterServices(IKernel kernel) { kernel.Bind<NLSubscriber.Core.Service.Repository.INLUserRepository>().To<NLSubscriber.Core.Service.Repository.EFDAL.EFNLUserRepository>().InRequestScope(); kernel.Bind<Neticon.Mvc.Helpers.IConfigHelper>().To<Neticon.Mvc.Helpers.AzureEnabledConfigHelper>().InSingletonScope(); kernel

Ninject Binding, Interface to Interface

做~自己de王妃 提交于 2019-12-05 04:58:29
问题 I want to do something along the lines of this: kernel.Bind<IBootTaskA>().To<BootTaskA>().InSingletonScope(); kernel.Bind<IBootTaskB>().To<BootTaskB>().InSingletonScope(); kernel.Bind<IBootTask>().To<IBootTaskA>(); kernel.Bind<IBootTask>().To<IBootTaskB>(); So i can do this: public class Boot { public Boot(IBootTask[] bootTasks) { foreach(var task in bootTasks){task.Execute();} } } but i cant seem to bind an interface to an interface, anyone know a way around this? 回答1: Heres how you do it.

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

做~自己de王妃 提交于 2019-12-05 04:25:07
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 this type itself, so I'm not sure how to inject my repository into it. I thought about using service

How do I inject Identity classes with Ninject?

爷,独闯天下 提交于 2019-12-05 04:20:55
I'm trying to use UserManager in a class, but I'm getting this error: Error activating IUserStore{ApplicationUser} No matching bindings are available, and the type is not self-bindable. I'm using the default Startup.cs, which sets a single instance per request: app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); I'm able to get the ApplicationDbContext instance, which I believe is getting injected by Owin (Is that true?): public class GenericRepository<T> : IGenericRepository<T> where T : class { private