.NET MVC Dependency Injection with Ninject
Tag : chash , By : MJRider
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I've just started programming in .NET and I'm having some problems with implementing dependency injection (using Ninject). , From the package manager console run this command: Install-package Ninject.MVC3
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
kernel.Bind<ITownService>().To<TownService>();
kernel.Bind<IRestaurantService>().To<RestaurantService>();
kernel.Bind<IFoodService>().To<TownService>();
}
|
Create instance from web.config and with dependency injection in constructor with Ninject
Date : March 29 2020, 07:55 AM
it should still fix some issue ideally you would have ILogReporting injected into the service that would use it. public class SomeService : ISomeService
{
private readonly ILogReporting _logger;
public SomeService(ILogReporting logger)
{
_logger = logger;
}
// .... code....
}
public class SomeService : ISomeService
{
private readonly IDependencyResolver _resolver;
public SomeService(IDependencyResolver resolver)
{
_resolver = resolver;
}
public void Execute()
{
var logger = _resolver.GetService<ILogReporting>();
// .... code....
}
}
|
Nested Ninject Bindings - Dependency Injection
Tag : chash , By : user134570
Date : March 29 2020, 07:55 AM
it should still fix some issue I think ordinary kernel.Bind ().To() should work pretty well for you. I prepared a small snippet to determine, that if that's really what you want. Is this correct?using System;
using Ninject;
namespace NinjectTest
{
class Program
{
static void Main(string[] args)
{
IKernel kernel = new StandardKernel();
kernel.Bind<IInnerService>().ToMethod(c=>new InnerService("this is a test config key")); //bind InnerService implementation to be used with provided string
kernel.Bind<IOuterService>().To<OuterService>(); //bind OuterService implementation to be used, all parameters will be injected to it using previously defined configs
var outerService = kernel.Get<IOuterService>();
var result = outerService.CallInner();
Console.WriteLine(result);
Console.ReadLine();
}
public interface IInnerService
{
string GetConfigKey();
}
public class InnerService : IInnerService
{
private readonly string _configurationKey;
public InnerService(string configurationKey)
{
_configurationKey = configurationKey;
}
public string GetConfigKey()
{
return _configurationKey;
}
}
public class OuterService : IOuterService
{
private readonly IInnerService _innerService;
public OuterService(IInnerService innerService)
{
_innerService = innerService;
}
public string CallInner() //purely for testing
{
return _innerService.GetConfigKey();
}
}
public interface IOuterService
{
string CallInner();
}
}
}
|
How to set up dependency injection in Ninject?
Tag : chash , By : Sebastian Gift
Date : March 29 2020, 07:55 AM
wish helps you I figured this out. You have to use the DI() extension method as suggested by the docs. This is in the Akka.DI.Core namespace. using Akka.DI.Core;
var actor = actorSystem.ActorOf(actorSystem.DI().Props<TimeLordActor>(),
"TimeLordActor");
|
asp.net mvc 3 dependency injection ninject
Date : March 29 2020, 07:55 AM
|