I'm writing a small framework that executed a couple of tasks. Some om the tasks needs specific properties which are injected through Ninject.
Let's say that I have the following constructor in my base class that represents a single Task:
protected DDPSchedulerTask(ILogger logger, List<string> platforms, IBackOfficeDataStore backOfficeDataStore, ICommonDataStore commonDataStore)
{
_logger = logger;
_platforms = platforms;
_backOfficeDataStore = backOfficeDataStore;
_commonDataStore = commonDataStore;
}
Those properties are needed for all the tasks, so I inject them using Ninject with the following Ninject module.
public class DDPDependencyInjectionBindingConfiguration : NinjectModule
{
#region NinjectModule Members
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
Bind<Scheduler>().ToSelf(); // Make sure that the 'Scheduler' is resolved to itself.
Bind<ILogger>().ToMethod(context => LogFactory.Create()); // Make sure that an instance of an ILogger is created through the LogFactory.
// Straightforward binding.
Bind<ICommonDataStore>().To<Common>();
Bind<IBackOfficeDataStore>().To<BtDbInteract>();
Bind<IDirectoryResolver>().To<Demo>();
}
#endregion
}
My scheduler object itself if the first entry in the chain which needs to be resolved by Ninject, so I'm resolving this manually through Ninject.
var schedulerInstance = kernel.Get<Scheduler>();
Now, my scheduler have a method which adds tasks to a list, so not by using Ninject:
var tasksList = new List<DDPSchedulerTask>
{
new AWNFileGeneratorTask(_logger, availablePlatforms, _backOfficeDataStore, _commonDataStore)
};
Then, all those tasks are being executed. Now, some of those tasks does require additional dependencies which I would like to resolve through Ninject but how should I do this?
Inside a task, I've created a property with the Inject
attribute, but the object stays nulls.
[Inject]
private IDirectoryResolver directoryResolver { get; set; }
Anyone has an idea on how this can be resolved?
I can pass the kernel around to the different tasks, but something's telling me that this isn't the correct approach.
Kind regards
DDPSchedulerTask
but you're returning aDDPSchedulerTaskFactory
. Also, can you explain a bit more your solution? From where should I call the create method because I want the additional properties also be injected by Unity. – Alula