Passing configuration to Ninject - c#

I need to pass an IConfiguration to my DbClient class and my repositories depend on this DbClient. I couldn't get this to work.
My DbClient:
public class DbClient
{
public DbClient(IConfiguration config)
{
// Perform some initialization
}
}
My repository depends on the DbClient:
public class MyRepository : IMyRepository
{
private DbClient _client;
public MyRepository(DbClient client)
{
_client = client;
}
}
My Bindings class
public class NinjectBindings : NinjectModule
{
public override void Load()
{
Bind<DbClient>().To<DbClient>(); // ?? Not sure about this
Bind<IMyRepository>().To<MyRepository>();
}
}
And the Main in my console app:
static void Main()
{
var config = new Configuration();
config.AddJsonFile("settings.json");
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
var myRepository = kernel.Get<IMyRepository>();
}
What am I missing? What do I need to do to pass IConfiguration into the DbClient and make sure the repository initializes properly?

You need to bind IConfiguration.
You can do it in Load method:
Bind<IConfiguration>().ToMethod(ctx =>
{
var config = new Configuration();
config.AddJsonFile("settings.json");
return config;
});
Or in Main method:
kernel.Bind<IConfiguration>().ToMethod(...);
Binding type depends on your need. Maybe you should bind config as constant without context dependency.
Also line Bind<DbClient>().To<DbClient>(); isn't necessary because DbClient will be automatically bind to self.

Related

Get an instance of a class by its name

I have StrategyName set in appsettings.json which represents the name of the strategy class. I need to get an instance of it.
ITradingStrategy _tradingStrategy = StrategyUtils.GetStrategyInstance(logger, _tradeOptions.StrategyName)
which is equal to
ITradingStrategy _tradingStrategy = new RsiStrategy(logger);
Is it possible to be made in a better way? It works but looks ugly. Since we know the strategy name in the beginning (from appsettings.json), there should probably be a way to obtain it in a better ASP.NET Core way. Maybe some cool extension method, I don't know.
appsettings.json
{
"TradeConfiguration": {
"StrategyName": "RsiStrategy",
...
}
}
Code
public class LiveTradeManager : ITradeManager
{
private readonly ILogger _logger;
private readonly IExchangeClient _exchangeClient;
private readonly ITradingStrategy _tradingStrategy;
private readonly ExchangeOptions _exchangeOptions;
private readonly TradeOptions _tradeOptions;
public LiveTradeManager(ILogger logger, IConfiguration configuration, IExchangeClient exchangeClient)
{
_logger = logger;
_exchangeClient = exchangeClient;
_exchangeOptions = configuration.GetSection("ExchangeConfiguration").Get<ExchangeOptions>();
_tradeOptions = configuration.GetSection("TradeConfiguration").Get<TradeOptions>();
_tradingStrategy = StrategyUtils.GetStrategyInstance(logger, _tradeOptions.StrategyName); // This is the questioned line
}
}
public static ITradingStrategy GetStrategyInstance(ILogger logger, string strategyName)
{
var strategyType = Assembly.GetAssembly(typeof(StrategyBase))
.GetTypes().FirstOrDefault(type => type.IsSubclassOf(typeof(StrategyBase)) && type.Name.Equals(strategyName));
if (strategyType == null)
{
throw new ArgumentException($"The strategy \"{strategyName}\" could not be found.", nameof(strategyName));
}
var strategy = Activator.CreateInstance(strategyType, logger) as ITradingStrategy;
return strategy;
}
// Strategies
public interface ITradingStrategy
{
IReadOnlyList<TradeAdvice> Prepare(IReadOnlyList<OHLCV> candles);
}
public abstract class StrategyBase : ITradingStrategy
{
private readonly ILogger _logger;
protected StrategyBase(ILogger logger)
{
_logger = logger;
}
public abstract IReadOnlyList<TradeAdvice> Prepare(IReadOnlyList<OHLCV> candles);
}
public class RsiStrategy : StrategyBase
{
private readonly ILogger _logger;
public RsiStrategy(ILogger logger) : base(logger)
{
_logger = logger;
}
public override IReadOnlyList<TradeAdvice> Prepare(IReadOnlyList<OHLCV> candles)
{
... _logger.Information("Test");
}
}
// Main
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
})
.ConfigureServices((hostingContext, services) =>
{
services.AddSingleton(
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostingContext.Configuration)
.CreateLogger());
services.AddSingleton<ITradeManager, LiveTradeManager>();
services.AddSingleton<IExchangeClient, BinanceSpotClient>();
services.AddHostedService<LifetimeEventsHostedService>();
})
.UseSerilog();
}
Your problem can be solved multiple ways and using reflection would be the last one.
From your problem statement, I figure that you have multiple strategy classed implementing ITradingStrategy interface, and you configuration value from appsettings.json file decides which strategy to use.
One of the approach you can use here is to use factory to initialize appropriate strategy class based on the configuration value.
Following is the factory class and interface which will create Strategy class object based on the strategy name passed to it.
public interface IStrategyFactory
{
ITradingStrategy GetStrategy(string strategyName);
}
public class StrategyFactory : IStrategyFactory
{
private IServiceProvider _serviceProvider;
public StrategyFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public ITradingStrategy GetStrategy(string strategyName)
{
switch (strategyName)
{
case "Rsi":
// Resolve RsiStrategy object from the serviceProvider.
return _serviceProvider.GetService<RsiStrategy>();
case "Dmi":
// Resolve DmiStrategy object from the serviceProvider.
return _serviceProvider.GetService<DmiStrategy>();
default:
return null;
}
}
}
This strategy can now be used in controller and call its GetStrategy method by passing the strategy name which in-turn is retrieved from the configuration.
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
// Strategy factory.
private IStrategyFactory _strategyFactory;
// Configuration
private IConfiguration _configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration, IStrategyFactory strategyFactory)
{
_logger = logger;
_strategyFactory = strategyFactory;
_configuration = configuration;
}
public IActionResult Index()
{
// Get Configuration value "StrategyName" from configuration.
// In your case this will be your own custom configuration.
var strategyName = _configuration.GetValue<string>("StrategyName");
// Pass strategyName to GetStrategy Method.
var strategy = _strategyFactory.GetStrategy(strategyName);
// Call Prepare method on the retrieved strategy object.
ViewBag.PreparedList = strategy.Prepare(new List<OHLCV>());
return View();
}
}
For the above code to work you need to register strategy classed in to serviceCollection.
services.AddSingleton<RsiStrategy>();
services.AddSingleton<DmiStrategy>();
And also the StrategyFactory.
services.AddSingleton<IStrategyFactory, StrategyFactory>();
EDIT
Based on your comment below, you need to be able to resolve the strategy types without additional overhead of registering them in DI as when new types are created and also without making changes in the factory.
You need to use reflection for this. Using reflection you can determine the types which you want to register in the DI. As following.
//Get all the types which are inheriting from StrategyBase class from the assembly.
var strategyTypes = Assembly.GetAssembly(typeof(StrategyBase))
?.GetTypes()
.Where(type => type.IsSubclassOf(typeof(StrategyBase)));
if (strategyTypes != null)
{
//Loop thru the types collection and register them in serviceCollection.
foreach (var type in strategyTypes)
{
services.Add(new ServiceDescriptor(typeof(StrategyBase), type, ServiceLifetime.Singleton));
}
}
With the above code, all the types which are inheriting from StrategyBase are registered in serviceCollection. Now using serivceProvider we can get all the registered instances and look for the instance which has correct strategyName.
So the factory's GetStrategy method will look like as following.
public ITradingStrategy GetStrategy(string strategyName)
{
var strategies = _serviceProvider.GetServices<StrategyBase>();
var strategy = strategies.FirstOrDefault(s => s.GetType().Name == strategyName);
if (strategy == null)
{
throw new ArgumentException($"The strategy \"{strategyName}\" could not be found.", nameof(strategyName));
}
return strategy;
}
I hope this will help you resolve your issue.

Property Injection does not work when using RegisterApiControllers extension method

Consider my simple controller class where I want to use a logger (ILogger is coming from Castle in this case).
[RoutePrefix("api/orders")]
public class SignalController : ApiController
{
public ILogger Logger { get; set; } = new NullLogger();
// POST api/orders/update
[HttpPost, Route("update")]
public virtual void UpdateHandler(ChangeStateDto update)
{
this.Logger.Info($"Received ChangeStateDto with status {update.Status}");
}
}
Then I've got a self-hosted webhost, that looks like this.
public class WebHost
{
private readonly string url;
private IDisposable disposable;
private readonly ILifetimeScope scope;
public WebHost(string url, ILifetimeScope scope)
{
this.url = url;
this.scope = scope;
}
public ILogger Logger { get; set; } = new NullLogger();
// ...
public void Start()
{
try
{
this.Logger.Info($"Starting web host at {url}");
this.disposable = WebApp.Start(this.url, app =>
{
var config = new HttpConfiguration
{
DependencyResolver = new AutofacWebApiDependencyResolver(scope)
};
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
});
// ...
}
catch (Exception e)
{
// ...
this.Logger.Error(e.Message, e);
}
}
}
And this is the calling type
public class SomeCallerClass
{
public SomeCallerClass()
{
var webHostLogger = new SomeILoggerImplementation(this, "WebHost");
var builder = new ContainerBuilder();
builder.RegisterApiControllers()
.WithProperty("Logger", webHostLogger);
var container = builder.Build();
this.webHost = new WebHost("http://localhost:9000", container)
{
Logger = webHostLogger
};
}
}
Now the issue I am having is that property injection is not working for my SignalController type. It always holds a reference to the NullLogger instance. I just can't figure out why.
I solved it by using .RegisterType<> instead of .RegisterApiControllers
So the following works as expected. However I still don't understand why my first approach wasn't working.
var builder = new ContainerBuilder();
builder.RegisterType<SignalController>()
.WithProperty("Logger", webHostLogger)
.InstancePerRequest();
var container = builder.Build();
It probably wasn't working because it wasn't registering any controllers.
In your code you have:
builder
.RegisterApiControllers()
.WithProperty("Logger", webHostLogger);
However, you have to tell the registration extension which assemblies your controllers are in. You can see that in the examples shown in the docs.
Try:
builder
.RegisterApiControllers(Assembly.GetExecutingAssembly())
.WithProperty("Logger", webHostLogger);

how to configure dependencies in asp.net core

I have an ASP.Net Core 2.1 application. I need to register & inject few dependencies of AWS.
Currently, the implementation looks like this:
public abstract class BaseService
{
protected readonly IConfiguration _configuration;
protected readonly RegionEndpoint _region;
protected readonly IAmazonDynamoDB _dynamoClient;
protected IPocoDynamo _pocoDynamo;
public BaseService(IConfiguration configuration)
{
_configuration = configuration;
var awsSettings = configuration.GetSection("AWS");
_dynamoClient = SetDynamoClient(awsSettings);
_pocoDynamo = SetPocoDynamoClient();
}
protected IAmazonDynamoDB SetDynamoClient(IConfigurationSection configuration)
{
AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
return new AmazonDynamoDBClient(credentials, _region);
}
protected IPocoDynamo SetPocoDynamoClient()
{
return new PocoDynamo(_dynamoClient);
}
}
While unit testing, AWS services can't be mocked due to this.
I want to register all these dependencies in Startup.cs in ConfigureServices()
This is what I was trying:
public void ConfigureServices(IServiceCollection services)
{
AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
services.AddTransient(IAmazonDynamoDB, (a) =>
{
return new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(""))
});
// here I need to pass the IAmazonDynamoDB to below IOC
// services.AddSingleton<IPocoDynamo,new PocoDynamo()> ();
return services;
}
But this is throwing an error
Error CS0119 'IAmazonDynamoDB' is a type, which is not valid in the given context
How to configure dependencies as required here?
Thanks!
Use factory delegate to call the registered service
public void ConfigureServices(IServiceCollection services) {
AWSCredentials credentials = new BasicAWSCredentials(configuration["AccessKey"], configuration["AccessSecret"]);
services.AddTransient<IAmazonDynamoDB>(sp =>
new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(""))
);
//here pass the IAmazonDynamoDB to below IOC
services.AddSingleton<IPocoDynamo>(serviceProvider => {
var pocoDynamo = new PocoDynamo(serviceProvider.GetRequieredService<IAmazonDynamoDB>());
pocoDynamo.SomeMethod();
return pocoDynamo;
});
}
The target class should no longer need to be dependent on IConfiguration as the dependencies can be explicitly injected via constructor injection.
public abstract class BaseService {
protected readonly IAmazonDynamoDB dynamoClient;
protected readonly IPocoDynamo pocoDynamo;
public BaseService(IAmazonDynamoDB dynamoClient, IPocoDynamo pocoDynamo) {
this.dynamoClient = dynamoClient;
this.pocoDynamo = pocoDynamo;
}
}

Unit testing the dependency injection

I am using Autofac for IoC
Here is my container initiator class, which the responsibility is to register the dependencies.
public class ContainerInit
{
public static IContainer BuildContainer()
{
var conFac = new ContainerFactory();
var builder = new ContainerBuilder();
builder.Register(conFac).As<IContainerFactory>().SingleInstance();
builder.Register(c=> new MainClass(conFac)).As<IMainClass>().SingleInstance();
builder.Register(c=> new Database(conFac)).As<IDatabase>().SingleInstance();
var logger = LoggUtil.CreateLogger();
builder.Register(logger).As<ILogger>().SingleInstance();
var container = builder.Build();
ContainerFactory.SetContainer(container);
return container;
}
}
Problem with this approach is, I need to pass IContainerFactory to the constructor of every class I use in my application as follow
public class MainClass: IMainClass
{
private readonly ILogger _logger;
private readonly IDatabase _db;
public MainClass(IContainerFactory containerFactory)
{
_logger = containerFactory.GetInstance<ILogger>();
_db = containerFactory.GetInstance<IDatabase>(); //example
}
public AddDetails(Data data)
{
//do some business operations
_db.Add(data);
_logger.Information("added");
}
}
So it is difficult to unit test these classes.
How can come up with a good solution?
A better approach would be to pass the dependencies you need in your class into your constructor:
public class MainClass : IMainClass
{
private readonly ILogger _logger;
private readonly IDatabase _db;
public MainClass(ILogger logger, IDatabase db)
{
_logger = logger;
_db = db;
}
public void AddDetails(Data data)
{
//do some business operations
_db.Add(data);
_logger.Information("added");
}
}
Then you could use a mocking framework such as Moq to mock your class dependencies and perform verifications on whether the dependencies were called:
[TestClass]
public class UnitTest1
{
private Mock<ILogger> _mockLogger = new Mock<ILogger>();
private Mock<IDatabase> _mockDb = new Mock<IDatabase>();
[TestMethod]
public void TestMethod1()
{
// arrange
var mainClass = new MainClass(_mockLogger.Object, _mockDb.Object);
var data = new Data();
// act
mainClass.AddDetails(data);
// assert
_mockDb
.Verify(v => v.Add(data), Times.Once);
}
}
I would not verify your log message though as this could change and make the test brittle. Only verify functionality which is essential to doing what the method is intended for.
Your current Service Locator Anti-Pattern is what makes your code difficult to test in isolation as well as makes the class misleading about what it actually depends on.
MainClass should be refactored to follow Explicit Dependencies Principle
public class MainClass : IMainClass
private readonly ILogger logger;
private readonly IDatabase db;
public MainClass(ILogger logger, IDatabase db) {
this.logger = logger;
this.db = db;
}
public void AddDetails(Data data) {
//do some business operations
db.Add(data);
logger.Information("added");
}
}
The same pattern should also be followed for any other class you have that depends on the container factory, like Database.
You would however need to also refactor the container registration accordingly
public class ContainerInit {
public static IContainer BuildContainer() {
var builder = new ContainerBuilder();
builder.RegisterType<MainClass>().As<IMainClass>().SingleInstance();
builder.RegisterType<Database>().As<IDatabase>().SingleInstance();
var logger = LoggUtil.CreateLogger();
builder.Register(logger).As<ILogger>().SingleInstance();
var container = builder.Build();
return container;
}
}
Testing MainClass would required you to mock only the necessary dependencies of the class under test.
[TestClass]
public class MainClassTests {
[TestMethod]
public void Should_AddDetails_To_Database() {
// Arrange
var mockDb = new Mock<IDatabase>();
var data = new Data();
var mainClass = new MainClass(Mock.Of<ILogger>(), mockDb.Object);
// Act
mainClass.AddDetails(data);
// Assert
mockDb.Verify(_ => _.Add(data), Times.Once);
}
}
Here I would like to share solution, which I use in my project
To do unit testing of particular function, I use below structure
[TestClass]
public class TestSomeFunction
{
public IComponentContext ComponentContext { get; set; }
[TestInitialize]
public void Initialize()
{
//Registering all dependencies required for unit testing
this.ComponentContext = builder.Build(); //You have not build your container in your question
}
[TestMethod]
public void Testfunction()
{
//Resolve perticular dependency
var _logger = containerFactory.Resolve<ILogger>();
//Test my function
//use _logger
}
}

Ninject: property injection into ActionFilterAttribute

I want to implement DI inside ActionFilterAttribute by Ninject. I've found this manual (actually I've read tens of them today) but it's for Unity. My attempt to implement it on Ninject is here:
public class MyFilter : ActionFilterAttribute
{
[Inject]
public IDepend Depend { get; set; }
// another code
}
bindings:
public class NinjectResolver : IDependencyResolver
{
// another code
private IKernel AddRequestBindings(IKernel kernel)
{
// another code
kernel.Bind<IDepend>().To<Depend>();
return kernel;
}
}
custom FilterProvider:
public class WebApiNinjectActionFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
private readonly IKernel _kernel;
private readonly IEnumerable<IFilterProvider> _filterProviders;
public WebApiNinjectActionFilterProvider(IKernel kernel, IEnumerable<IFilterProvider> filterProviders)
{
_kernel = kernel;
_filterProviders = filterProviders;
}
public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
{
var filters = _filterProviders.SelectMany(fp => fp.GetFilters(configuration, actionDescriptor)).ToList();
foreach (var filter in filters)
{
// GET ActivationException HERE!!!!!
_kernel.Inject(filter.Instance);
}
return filters;
}
public static void RegisterFilterProviders(IKernel ninjectKernel)
{
var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
GlobalConfiguration.Configuration.Services.Add(typeof(IFilterProvider),
new WebApiNinjectActionFilterProvider(ninjectKernel, providers));
var defaultprovider = providers.First(i => i is ActionDescriptorFilterProvider);
GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
}
}
is called from WebApiConfig:
public static void Register(HttpConfiguration config)
{
var ninjectKernel = new StandardKernel();
config.DependencyResolver = new NinjectResolver(ninjectKernel);
WebApiNinjectActionFilterProvider.RegisterFilterProviders(ninjectKernel);
}
and finally action:
[MyFilter]
public async Task<string> Test()
{
return "Hello world";
}
The problem is I get an ActivationException in this line:
_kernel.Inject(filter.Instance);
ActivationException's Activation path:
2) Injection of dependency IDepend into property Depend of type MyFilter
1) Request for MyFilter
If to inject same property to controller - it injects fine. Any ideas how to improve my code?
I was doing IActionFilter DI in wrong way. The right way I've found in this blogpost of Mark Seemann.
DI via Ninject was implemented this way:
var myFilter = new MyFilter(ninjectKernel.Get<IDepend>());
config.Filters.Add(myFilter);

Categories