ninject

How to conditionally bind a instance depending on the injected type using unity?

走远了吗. 提交于 2019-12-01 05:12:19
I'm used to Ninject, and for a specific project I'm asked to learn Unity. There is one thing i can't find information on how to do. In Ninject I can state: Bind<IWarrior>().To<Samurai>().Named("Samurai"); Bind<IWarrior>().To<Ninja>().Named("Ninja"); Bind<IWeapon>().To<Katana>().WhenInjectedInto(typeof(Samurai)); Bind<IWeapon>().To<Shuriken>().WhenInjectedInto(typeof(Ninja)); And then when one asks for the warrior named samurai, the samurai comes with kanana and the ninja comes with shurikens. As it should. I don't want to reference the container in the warriors to get the appropriate weapon,

Bind a Global Action Filter to all Controllers in an Area using MVC 3 Dependency Injection with Ninject 2.2

纵饮孤独 提交于 2019-12-01 04:58:07
问题 I was able to to use ASP.NET MVC 3 and Ninject 2.2 to inject a logger object into a custom ActionFilterAttribute thanks to the help I received in this post. Now I would like to bind my custom ActionFilterAttribute only to all controllers that are in a specific area. I was able to get started with the following binding but it only handles one controller in a certain area. I would like my code to bind to all controllers in a specific area. Any ideas? /// <summary> /// Load your modules or

Ninject : Resolving an object by type _and_ registration name/identifier

懵懂的女人 提交于 2019-12-01 04:29:14
I am looking for a way to do something like this with Ninject : // Sample from the Unity application block IMyService result = myContainer.Resolve<IMyService>("Data"); ( from http://msdn.microsoft.com/en-us/library/cc440957.aspx ) Is it possible? Ninject 2.0 has this capability: Bind<IMyService>().To<MyServiceA>().Named("Data"); Bind<IMyService>().To<MyServiceB>().Named("SomethingElse"); kernel.Get<IMyService>("Data"); // will return MyServiceA AFAIK there is no way to do that directly in Ninject, but you can use Contextual Binding instead. 来源: https://stackoverflow.com/questions/533260

Ninject.MVC3 Bootstrapper's Kernel property is marked as Obsolete. How can I get access to the kernel?

给你一囗甜甜゛ 提交于 2019-12-01 03:51:00
I updated Ninject.MVC3 package from 2.2.1.0 to 2.2.2.0. Before I had access to the Kernel object through BootStrapper.Kernel property but in the new version Kernel property is marked as obsolete. I get a warning saying 'Public ReadOnly Property Kernel As Ninject.IKernel' is obsolete: 'Do not use Ninject as Service Locator'. Is there a different way to access the kernel in the new version? Remo Gloor The reason why this has been marked Obsolete and will be changed to internal in future is that people tend to use Ninject as a service locator if it is possible to do so. But Service Locator is an

Using Ninject with Membership.Provider

我怕爱的太早我们不能终老 提交于 2019-12-01 03:24:06
I'm new to Ninject and I'm having problems using it with a custom membership provider. My membership provider has a repository interface passed in. It looks like: public class CustomMembershipProvider : MembershipProvider { public CustomMembershipProvider( IRepository repository ) { } } I'm using the code thats part of the Account Model in the MVC app as a starting point. However when it calls Membership.Provider I get an error saying No parameterless constructor defined for this object . I've setup the bindings in ninject to bind a IRepository to a Repository class which work as I've testing

Ninject InRequestScope fallback to InThreadScope

只愿长相守 提交于 2019-12-01 03:10:22
In my MVC3 project I've setup my kernel to Ninject the Entityframework context on a InRequestScope basis, this works perfect, but I have a background runner that does some workflow management. It fires up a new thread each 5 minutes and I Ninject my dependencies into this thread, If I change the scope to InThreadScipe the Dispose method is fired, but If i change it back to InRequestScope the Dispose method wont fire. Is there a way of fallbacking to InThreadScope if InRequestScope isnt available? Update: Just got an upvote for this question and why not update it with some additional info. I

Ninject Dependency Injection for SignalR

本小妞迷上赌 提交于 2019-12-01 02:58:34
问题 In my NinjectWebCommon.cs file, under CreateKernel method I am applying the injection like this private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); // Web API Injection GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); // SignalR Injection GlobalHost

How to conditionally bind a instance depending on the injected type using unity?

删除回忆录丶 提交于 2019-12-01 02:25:58
问题 I'm used to Ninject, and for a specific project I'm asked to learn Unity. There is one thing i can't find information on how to do. In Ninject I can state: Bind<IWarrior>().To<Samurai>().Named("Samurai"); Bind<IWarrior>().To<Ninja>().Named("Ninja"); Bind<IWeapon>().To<Katana>().WhenInjectedInto(typeof(Samurai)); Bind<IWeapon>().To<Shuriken>().WhenInjectedInto(typeof(Ninja)); And then when one asks for the warrior named samurai, the samurai comes with kanana and the ninja comes with shurikens.

How to stop Ninject from overriding custom DataAnnotationsModelValidatorProvider?

∥☆過路亽.° 提交于 2019-12-01 01:56:18
问题 I have a custom DataAnnotationsModelValidatorProvider for doing model validation in a more dynamic way then just adding attributes. I tried to add my provide to the global.asax.cs like so: ModelValidatorProviders.Providers.Clear(); ModelValidatorProviders.Providers.Add(new AttributeValidatorProvider()); But once I load my form, I get an error saying "Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required".

MVC3 Action Filter Using Database (EF 4.1 DBContext, Ninject)

巧了我就是萌 提交于 2019-12-01 00:41:04
I'm trying to setup an 'Authorization' Filter on an Action, creating my own ActionFilterAttribute where I do a database lookup to determine if a user has access to a certain resource. On my class inheriting from ActionFilterAttribute, I have created an Injected(Ninject) property to hold the service that I am using for the database access. I have a parameterless constructor so that I can use this as an attribute on my actions. In the 'OnActionExecuting' Method, I am able to gain access to the Injected property (it's not null), but the base DBCotext that it is using is closed. This working fine,