ninject

How to do dependency injection inside ASP.NET MVC's RegisterGlobalFilters method

蹲街弑〆低调 提交于 2019-12-21 17:43:20
问题 I am still a bit new to using IOC containers and I'm struggling a bit. I am using ASP.NET MVC 5.2 with Ninject.MVC3. I have an exception filter that basically hands off to a log service: public class ExceptionLoggerFilter : IExceptionFilter { private readonly ILogService _logService; public ExceptionLoggerFilter(ILogService logService) { _logService = logService; } public void OnException(ExceptionContext filterContext) { _logService.LogError(filterContext.Exception); } } I would like to use

How come there's no IKernel implementation in Ninject.Portable

孤街浪徒 提交于 2019-12-21 17:29:47
问题 I use and fancy Ninject alot. I wonder why there is no "BasicKernel" in Ninject.Portable? Is implementing IKernel require any call that PCLs don't contain? I'm talking about simple scenario (about: Bind<If1>().To<Class1>() and Get<If1>() )? 回答1: The PCL version of Ninject is split into two libraries, Ninject.dll and Ninject.Common.dll as some of the code is platform-specific. Ninject cannot exist without its platform code. In order to use Ninject, you need to add the Portable.Ninject Nuget to

Making Ninject Interceptors work with async methods

佐手、 提交于 2019-12-21 17:09:50
问题 I am starting to work with ninject interceptors to wrap some of my async code with various behaviors and am having some trouble getting everything working. Here is an interceptor I am working with: public class MyInterceptor : IInterceptor { public async void Intercept(IInvocation invocation) { try { invocation.Proceed(); //check that method indeed returns Task await (Task) invocation.ReturnValue; RecordSuccess(); } catch (Exception) { RecordError(); invocation.ReturnValue = _defaultValue;

Ninject: Can modules be loaded that are declared as internal

血红的双手。 提交于 2019-12-21 12:42:04
问题 Is it at all possible to configure Ninject to load modules that have been declared as internal ? I have tried configuring InternalVisibleTo for the Ninject assembly, but this does not help. I can of course make the modules public , but really they should be internal . 回答1: Internally KernalBase.Load(IEnumerable<Assembly assemblies) uses the GetExportedTypes() which only returns public types. However, you could write your own "NinjectModule scanner". public static class NinjectModuleScanner {

How to use a Provider in Ninject

有些话、适合烂在心里 提交于 2019-12-21 12:11:07
问题 I have the following code public class Something { [Inject] public Configuration config {get;set;} //singleton [Inject] public Provider<WindowHandler> windowsProvider { get; set; } //NOT singleton public void Search(string text) { WindowHandler handler = windowsProvider.Create(xxxxxx); //use the new handler that was created } } but it seems the Provider takes an IContext where I put xxxxxx. Shouldn't the IContext from when I bootstrapped and created Something.cs from the kernel be used. Where

Convention based binding of constructor string arguments with Ninject

谁都会走 提交于 2019-12-21 09:25:04
问题 I'm using Ninject as IoC container in my project. I have following class: public class SomeRepository:ISomeRepository { public SomeRepository(string someDatabaseConnectionString) { // some code here.. } } In my application settings file I have connection string named "someDatabase". By default the one should add following configuration in order to inject this connection string into the constructor: kernel.Bind<ISomeRepository>() .To<SomeRepository>() .WithConstructorArgument(

Ninject and DataContext disposal

为君一笑 提交于 2019-12-21 06:55:13
问题 I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important and that whenever you create a direct object of the DataContext (as in: new DataContext()) you should use a using() block. My question thus is: When im retrieving my DataContext from the kernel, should I still have to use a using() block? Or does

Ninject and DataContext disposal

℡╲_俬逩灬. 提交于 2019-12-21 06:53:18
问题 I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important and that whenever you create a direct object of the DataContext (as in: new DataContext()) you should use a using() block. My question thus is: When im retrieving my DataContext from the kernel, should I still have to use a using() block? Or does

How to Avoid Coupling with an IoC Container

二次信任 提交于 2019-12-21 05:03:29
问题 I'm in the process of developing an extensible framework using DI and IoC. Users must be able override existing functionality within the framework by dropping their own implementations into the container. How can I allow users to do this without requiring them to know which IoC container I am using? My current half-way solution is to structure my assemblies as follows: 1) Define abstract assemblies containing only interfaces. 2) Define concrete assemblies which implement these interfaces.

Ninject + ASP.NET MVC + InRequestScope

假如想象 提交于 2019-12-21 04:43:30
问题 I have a problem with the Ninject. My binding rules: this.Bind<ISphinxQLServer>().To<SQLServer>(); this.Bind<IMySQLServer>().To<SQLServer>(); this.Bind<ISQLLogger>().To<StandardSQLLogger>() .InRequestScope(); this.Bind<DatabaseConnections>() .ToMethod(x => ConnectionFactory.GetConnections()) .InRequestScope(); this.Bind<SQLServer>().ToSelf() .InRequestScope() .WithConstructorArgument("connections", Kernel.Get<DatabaseConnections>()) .WithConstructorArgument("logger", Kernel.Get<ISQLLogger>())