ninject

MVC3 Controller constructor + Ninject

匆匆过客 提交于 2019-12-10 21:49:15
问题 I'm at the moment working on a MVC3 Web application and ecountered a new problem with Ninject. I'm using the following code in my controller: public class TestController : Controller { public IRepository<CustomerModel> rep; public TestController(IRepository<CustomerModel> repository) { this.rep = repository; } public ActionResult Index() { return View(); } } And my Ninject Module: public class RepositoryModule : NinjectModule { public override void Load() { Bind(typeof(IRepository<>)).To

Ninject factory extension and InCallScope does not give expected results

白昼怎懂夜的黑 提交于 2019-12-10 21:23:55
问题 I am struggling with using the factory extensions for Ninject. When using the extension in combination with InCallScope, I expected the same instance to be returned from the factory's create method, but instead I get two different instances. Have I misunderstood the InCallScope concept or do I need to add something else? using System; using Ninject; using Ninject.Extensions.Factory; using Ninject.Extensions.NamedScope; namespace MyTest { class Program { static void Main() { var kernel = new

Ninject basics with example please

流过昼夜 提交于 2019-12-10 20:37:10
问题 Scenario: Quite new to DI and Ninject but would love to master it so that I know what'm doing and why. While going through few examples and documentation I noticed the following: 1. ToConstructor. 2. ToMethod 3. Self If someone could help me to understand when and how above can be used, will be good. An example will be good. Thanks. 回答1: Hy, Self bindings declare a binding of a certain type to itself. Self bindings are not needed for types which have a parameterless constructor. Ninject can

Ninject Providers -> Get another dependency inside the provider

感情迁移 提交于 2019-12-10 18:53:46
问题 I'm wondering what the best practices is here. I need to construct a DbContext for my multi tenanted application, so I have made a Dependency provider like this: public class TenantContextFactoryProvider : Provider<DbContext> { protected override DbContext CreateInstance(IContext context) { var tenant = // How to get the tenant through ninject?? return new DbContext(tenant.ConnectionString); } } I need ninject to resolve the tenant dependency, but I'm not sure how to do this? 回答1: While

Why is ASP.NET Web Api's Dependency Resolver being disposed?

我们两清 提交于 2019-12-10 18:49:01
问题 I have a custom IDependencyResolver : internal class NinjectResolver : IDependencyResolver { private IKernel _kernel; internal NinjectResolver(params ApplicationModule[] modules) { _kernel = new StandardKernel(modules); } public IDependencyScope BeginScope() { return this; } public object GetService(Type serviceType) { return _kernel.TryGet(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return _kernel.GetAll(serviceType); } protected void Dispose(bool disposing) {

How to inject different NHibernate Sessions (multi-db) to same repository with Controller controlling which sessions with Ninject

纵饮孤独 提交于 2019-12-10 18:37:10
问题 Using: ASP.NET MVC3 Ninject 2 Fluent nHibernate I have 2 databases (DB1 & DB2). I have one base repository class (Repository) and many Controllers (Controller1, Controller2). public MyController(IRepository<SomeModelFromDB1> someModelFromDB1Repository, IRepository<SomeModelFromDB2> someModelFromDB2Repository) { [...] } public class Repository<T> : IRepository<T> where T : Entity { private readonly ISession _session; public Repository(ISessionFactory sessionFactory) { _session = sessionFactory

Implementing a Service Locator with injected variations of Generic Type

馋奶兔 提交于 2019-12-10 17:36:06
问题 I have the following: public interface IConverter<TValue, TConverted> { } public interface IConverterProvider { IConverter<TValue, TConverted> GetConverter<TValue, TConverted>(); } With an example binding at setup: Bind<IConverter<System.Int32, System.String>>().To<Int32ToStringConverter>(); Bind<IConverter<System.Guid, System.String>>().To<GuidToStringConverter>(); So I have a collection of fixed converters and no duplicate bindings. [Question] My question is how do I go about implementing

How to initialize Ninject in a class project part of an mvc site

℡╲_俬逩灬. 提交于 2019-12-10 15:39:19
问题 I have used Ninject in a small project, but am now converting a larger web app to mvc and need help with using Ninject. In the new solution, I have the mvc site and have split some of the functionality out into separate class projects, for example, my ReportGenerator. I would like to use Ninject within ReportGenerator to resolve the dependencies it has, but I do NOT want the MVC project to know about the internal workings of ReportGenerator. So where would I create the bindings/kernel? I've

Getting “MissingMethodException: Cannot create an instance of an interface” when binding generic interface to repository with Ninject

霸气de小男生 提交于 2019-12-10 15:02:28
问题 Following the guide here, but instead of StructureMap attempting to use Ninject. It throws up the "MissingMethodException: Cannot create an instance of an interface" error any time I attempt to inject an IRepository<SomeEntityType> into a parameter in an action method. Update: Also giving bootstrapper.cs not found, I used the MVC3 Ninject Nuget package. public ActionResult Index(IRepository<SomeEntityType> repo) { return View(); } NinjectWebCommon.cs private static void RegisterServices

What is the equivalent of Container.GetAllInstances<T> in NInject?

拜拜、爱过 提交于 2019-12-10 14:53:54
问题 I'm building a message broker with NInject, and I need to find all instances in the container that implement Consumes, an interface that marks the class as being able to consume a particular message type. Is this scenario supported? 回答1: Answer from Nate: Multi-resolution (via GetAll ) is currently not polymorphic. That means that it will only consider bindings from the exact interface you specify. If you do this: kernel.Bind<IWorker>().To<WorkerA>(); kernel.Bind<IWorker>().To<WorkerB>();