传递Ninject内核是一个好习惯吗? [英] Is it a good practice to pass the Ninject kernel around?
问题描述
我正在编写一个执行一些任务的小型框架. 有些任务需要通过Ninject注入的特定属性.
I'm writing a small framework that executed a couple of tasks. Some om the tasks needs specific properties which are injected through Ninject.
假设我们在基类中具有以下表示单个Task的构造函数:
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;
}
所有任务都需要这些属性,因此我将Ninject与以下Ninject模块一起使用.
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
}
我的调度程序对象本身是否是Ninject需要解决的链中的第一个条目,所以我要通过Ninject手动解决此问题.
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>();
现在,我的调度程序具有一种将任务添加到列表的方法,因此不使用Ninject:
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)
};
然后,所有这些任务都在执行. 现在,其中一些任务确实需要额外的依赖关系,我想通过Ninject解决这些依赖关系,但是我应该怎么做呢?
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?
在任务中,我创建了一个具有Inject
属性的属性,但该对象保留为空.
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.
亲切的问候
推荐答案
在这种情况下,我通常使用Factory模式.在调度程序中,您可以将任务工厂作为依赖项.该工厂还可以使用多种方法来创建不同类型的任务.
In such cases I usually use a Factory pattern. In the scheduler you can have the task factory as a dependency. This factory can also have multiple methods for creating different types of tasks.
class DDPSchedulerTaskFactory : IDDPSchedulerTaskFactory
{
DDPSchedulerTaskFactory(ILogger logger, List<string> platforms, IBackOfficeDataStore backOfficeDataStore, ICommonDataStore commonDataStore)
{
_logger = logger;
_platforms = platforms;
_backOfficeDataStore = backOfficeDataStore;
_commonDataStore = commonDataStore;
}
public IDDPSchedulerTask CreateNormal(){
return new DDPSchedulerTask(
_logger,
_platforms,
_backOfficeDataStore,
_commonDataStore);
}
public IDDPSchedulerTask CreateSpecial(someAdditionalParameter){
return new AnotherDDPSchedulerTask(
_logger,
_platforms,
_backOfficeDataStore,
_commonDataStore,
someAdditionalParameter);
}
public IDDPSchedulerTask CreateTaskWithDifferentDependenties(yetAnotherParameter){
return new AnotherDDPSchedulerTask(
_logger,
_platforms,
yetAnotherParameter);
}
}
与您的调度程序相比,您可以添加以下任务:
Than in your scheduler you can add tasks like that:
class Scheduler{
IDDPSchedulerTaskFactory _taskFactory;
public Scheduler(IDDPSchedulerTaskFactory taskFactory){
_taskFactory = taskFactory; // factory gets all the needed dependencies for all tasks from DI
}
...
public void ConfigureTasks(){
_tasks.Add(_taskFactory.CreateNormal());
_tasks.Add(_taskFactory.CreateSpecial("Some important message"));
_tasks.Add(_taskFactory.CreateTaskWithDifferentDependenties(123));
}
}
这篇关于传递Ninject内核是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!