ninject

Using default parameter values with Ninject 3.0

試著忘記壹切 提交于 2019-11-30 18:23:30
I have a class with a constructor having a parameter with a default value. With Ninject 2.2, it would honor the [Optional] attribute and work fine with no binding defined against a constructor declared like so: public EmployeeValidator([Optional] IValidator<PersonName> personNameValidator = null) Since upgrading to Ninject 3.0, construction of this object fails with a message stating that the provider returned null: Test method ValidatorIsolated.Tests.EmployeeValidatorTest.CreateEmployeeValidatorTest threw exception: Ninject.ActivationException: Error activating IValidator{PersonName} using

Using property injection instead of constructor injection

徘徊边缘 提交于 2019-11-30 18:15:46
Long story short, I'm trying to use ELMAH with MVC 2 and Ninject, and I need to use parameterless constructors. I created an initial post about it here: Using a parameterless controller constructor with Ninject? I was advised to use property injection instead of constructor injection. So I moved from this: public class DepartmentsController : Controller { private IDepartmentsRepository departmentsRepository; public DepartmentsController(IDepartmentsRepository departmentsRepository) { this.departmentsRepository = departmentsRepository; } ... } to this: public class DepartmentsController :

Ninject - In what scope DbContext should get binded when RequestScope is meaningless?

徘徊边缘 提交于 2019-11-30 17:37:40
In an MVC / WebAPI environment I would use InRequestScope to bind the DbContext . However, I am now on a Console application / Windows service / Azure worker role (doesn't really matter, just there's no Web request scope), which periodically creates a number of Tasks that run asynchronously. I would like each task to have its own DbContext , and since tasks run on their own thread, I tried binding DbContext using InThreadScope . Unfortunately, I realize that the DbContext is not disposed when a task is finished. What actually happens is, the thread returns to the Thread Pool and when it is

Using Ninject with Udi Dahan's Domain Events

情到浓时终转凉″ 提交于 2019-11-30 17:24:55
问题 I'm using Ninject in an MVC project and am trying to implement Domain Events following Udi Dahan's pattern http://www.udidahan.com/2009/06/14/domain-events-salvation/ In the extract below, the "Container" is used to resolve all the event-handlers for the particular type of event that has been raised. My question (& apologies if I am missing something basic) is how to do this with Ninject? In other words: How does the "Container" get set in this static class? Once I have a Container (Kernel?)

Looking for Ninject equivalent of StructureMap's ObjectFactory.GetInstance() method

本小妞迷上赌 提交于 2019-11-30 16:12:39
I'm using Ninject in an MVC project and I've used the autoregistration features in Ninject.Mvc and have my bindings set up in my application class. However, I have a place where I want to create an instance separate from those bindings. In StructureMap, you can do var foo = ObjectFactory.GetInstance<IFoo>(); and it will resolve it for you. Is there an equivalent in Ninject 2? I can't seem to find it anywhere. AFAIK, NInject doesn't have static method like this so all resolving should go to some kernel. But you can implement it easily; class ObjectFactory { static IKernel kernel = new

Looking for Ninject equivalent of StructureMap's ObjectFactory.GetInstance() method

南笙酒味 提交于 2019-11-30 16:10:46
问题 I'm using Ninject in an MVC project and I've used the autoregistration features in Ninject.Mvc and have my bindings set up in my application class. However, I have a place where I want to create an instance separate from those bindings. In StructureMap, you can do var foo = ObjectFactory.GetInstance<IFoo>(); and it will resolve it for you. Is there an equivalent in Ninject 2? I can't seem to find it anywhere. 回答1: AFAIK, NInject doesn't have static method like this so all resolving should go

Need help understanding how Ninject is getting a Nhibernate SessionFactory instance into a UnitOfWork?

限于喜欢 提交于 2019-11-30 16:08:22
问题 So using some assistance from tutorials I have managed to wire up a Nhibernate session to my repositories and my repositories to my controllers using Ninject. However, there is one peice of the setup that I am not grasping the "automagic" of what Ninject is doing and was hoping someone could explain. Below is my Ninject ModuleRepository that inherits from NinjectModule that does all the binding. public class ModuleRepository : NinjectModule { public override void Load() { var helper = new

Does anyone know of a good guide to get Ninject 2 working in ASP.NET MVC?

我怕爱的太早我们不能终老 提交于 2019-11-30 15:21:33
I'm struggling with the documentation to figure out exactly what I need to. The documentation (to my understanding) is for 1.5 anyway. N.B: I don't want to extend NinjectHttpApplication I've configured it to use the NinejctControllerFactory in Application_Start() but I get a null reference exception on the KernelContainer.Kernel when it tries to create a controller. Where do I configure the Kernel if I'm not extending NinjectHttpApplication? Take a look at this blog post. It should help clarify the process you need to perform on how to configure the kernel. http://www.kevinrohrbaugh.com/blog

Ninject dependency injection with Decorator pattern

和自甴很熟 提交于 2019-11-30 14:28:16
问题 Say, I have such classes hierarchy: public interface IRepository { } public class SomeSimpleRepository : IRepository {} Now I want to "decorate" SomeSimpleRepository with additional functions public class MoreAdvancedRespository : IRepository { private readonly IRepository _originalRepository; public MoreAdvancedRespository(IRepository original) { } } After awhile another one.. public class TrickyRepository : IRepository { private readonly IRepository _originalRepository; public

Setup filter attribute for dependency injection to accept params in constructor

喜夏-厌秋 提交于 2019-11-30 14:16:09
I'm following a ninject filter attribute setup on this page . For them, they have: .WithConstructorArgumentFromControllerAttribute<LogAttribute>( "logLevel", attribute => attribute.LogLevel); The second parameter is expecting a Func<LogAttribute, object> callback . Their actual param list is setup as follows: Log(LogLevel = Level.Debug) But my filter attribute is setup as follows: public class AuthAttribute : FilterAttribute { } public class AuthFilter : IAuthorizationFilter { private readonly IUserService userService; private string[] roles; //Stuck on the constructor also. How do I accept