ninject

Injection of class with multiple constructors

丶灬走出姿态 提交于 2019-12-06 15:53:20
Resolving a class that has multiple constructors with NInject doesn't seem to work. public class Class1 : IClass { public Class1(int param) {...} public Class1(int param2, string param3) { .. } } the following doesn’t seem to work: IClass1 instance = IocContainer.Get<IClass>(With.Parameters.ConstructorArgument(“param”, 1)); The hook in the module is simple, and worked before I added the extra constructor: Bind().To(); The reason that it doesn't work is that manually supplied .ctor arguments are not considered in the .ctor selection process. The .ctors are scored according to how many

Inheriting a base controller with constructor

一曲冷凌霜 提交于 2019-12-06 15:50:47
I am using ninject to inject my repositories. I would like to have a my base class to be inherited but I cant because it has a constructor. Base Controller: namespace Orcha.Web.Controllers { public class BaseController : Controller { public IRepository<string> db; public BaseController(Repository<string> db){ this.db = db; Debug.WriteLine("Repository True"); } } } Controller with inherit: Error 'BaseController' does not contain a constructor that takes 0 arguments HomeController.cs public class HomeController : BaseController { public ActionResult Index() { ViewBag.Message = "Welcome to ASP

In asp.net-mvc, is there a more elegant way using IOC to inject mutiple repositories into a controller?

与世无争的帅哥 提交于 2019-12-06 15:44:27
I have an asp.net-mvc website and i am using ninject for IOC and nhibernate for my ORM mapping Here is my IOC binding code: internal class ServiceModule : NinjectModule { public override void Load() { Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>)).InRequestScope(); } } and here is an example of how I am doing IOC into my controller code: public FAQController(IIntKeyedRepository<FAQ> faqRepository, IUnitOfWork unitOfWork) { _faqRepository = faqRepository; _unitOfWork = unitOfWork; } The issue is that up until now, each controller had a single table that it was pointing to so i

How to organize DI Framework usage in an application?

丶灬走出姿态 提交于 2019-12-06 15:29:28
EDIT: I forgot to move the kernel into a non-generic parent class here and supply a virtual method to access it. I do realize that the example below, as is, would create a plethora of kernel instances. I just learned how to do injection this past week and here's how I've got things set up currently: using Ninject; using System.Reflection; namespace Infrastructure { public static class Inject<T> { static bool b = Bootstrap(); static IKernel kernel; static bool Bootstrap() { kernel = new StandardKernel(); kernel.Load(Assembly.GetExecutingAssembly()); return true; } public static T New() { return

Ninject dynamic multiple constructor arguments

泪湿孤枕 提交于 2019-12-06 15:27:53
I have been implementing Dependency Injection into an existing Winforms project and it has been going well so far, however I want to generalise the calling of the Forms, specifically the varying quantity of constructor parameters. My code is as follows: Public Shared Function GetForm(formObject As BaseObject, _ parameters As Dictionary(Of String, Object)) As Form Select Case formObject.GetType() Case GetType(Production.Task) Return SMKernel.Kernel.Get(Of Forms.Production.Domain.ManageTask) _ (New Parameters.ConstructorArgument() _ {New Parameters.ConstructorArgument("task", _ CType(formObject,

How to rollback nHibernate transaction when an exception occurs during request having Ninject for managing sessions?

丶灬走出姿态 提交于 2019-12-06 14:11:33
I use nHibernate for ORM and Ninject for IoC. I create nHibernate sessions per some custom scope (which you can assume is per request). I begin the transaction onActivation. I commit the transaction onDeactivation. The problem is that if an exception happens during the request I want to rollback the transaction rather than committing it. Any idea how to detect (in a clean way, most probably using Ninject Context) that an exception has happened? Note: I am not concerned about the exceptions that can happen on commit which I can catch in the following code and role back easily. protected void

Ninject: How to access root object of NamedScope from factory

好久不见. 提交于 2019-12-06 12:35:59
In my application I am using Ninject and the NamedScopeExtension. One of the objects deeper in the object graph needs access to the root object that defined the named scope. It seems to me that DefinesNamedScope() does not also imply InNamedScope() and instead a new root object is created when I request the root. Example: using System; using Ninject; using Ninject.Extensions.NamedScope; using Ninject.Syntax; namespace NInjectNamedScope { public interface IScopeRoot { Guid Guid { get; } void DoSomething(); } public interface IFactory { Guid Guid { get; } IOther CreateOther(); } public interface

WCF, async, and a confusion of context

痞子三分冷 提交于 2019-12-06 12:20:29
Well, I was going to name this and a question of context , but apparently the word question isn't allowed in titles. Anyway, here's the issue: I use IErrorHandler in my WCF services in order to provide logging without cluttering up all of my service code. Until now, this has worked great. However, now that I'm trying to move to completely asynchronous services, I'm encountering the issue of the call stack being a return stack instead of a causality chain . Now, I tried using Stephen Cleary's logical call context MyStack , combined with Ninject 's Intercept extensions.. Ninject: Bind<IThing>()

Ninject setup for general repository using Nhibernate

ⅰ亾dé卋堺 提交于 2019-12-06 11:31:37
问题 I'm developing a .NET Web API application using Nhibernate and a generic repository. Now I'm trying to correctly setup dependency injection using Ninject. However, I have some problems with my current configuration: occasionally my NHibernate ISession (in UnitOfWork.cs below) object is either null or already closed when making a request that goes down to the DAL and tries to fetch data from the database. I have not been able to figure out exactly why this happens or what is wrong in my code.

How to use Ninject in constructor injection of a type in an external assembly

江枫思渺然 提交于 2019-12-06 11:30:42
问题 I am loading a type from an external assembly and want to create an instance of the type. However, this type/class is setup for constructor injection by objects currently being managed/bound by Ninject . How can I use Ninject to create an instance of this type and inject any constructor dependencies? Below is how I get this type. Assembly myAssembly = Assembly.LoadFrom("MyAssembly.dll"); Type type = myAssembly.GetType("IMyType"); 回答1: Assuming you've created a Kernel , you should be able to