【发布时间】:2014-03-27 09:21:34
【问题描述】:
我正在创建一个后台任务控制器,如下所示:
public class TaskController
{
private TaskBase task;
public TaskController(ITask task)
{
this.task = task;
}
public void DoSomething()
{
task.DoSomething();
}
}
ITask接口:
interface ITask
{
void DoSomething();
}
TaskBase抽象类:
public abtract class TaskBase : ITask
{
\\some common fields/properties/methods
public void DoSomething()
{
\\perform action here
}
}
Task 实现:
public class Task1 : TaskBase
{
public Task1(string arg, int arg1)
{
}
}
public class Task2 : TaskBase
{
public Task2(bool arg, double arg)
{
}
}
这是一个如何使用它的示例:
public void DoTask(string arg, int arg1)
{
Task1 task = new Task1(arg, arg1);
TaskController controller = new TaskController(task);
controller.DoSomething();
}
如您所见,我在这种方法中使用手动注入。现在我想改用像 NInject 这样的 IoC,但是在做了一些研究之后,我仍然有两件事困扰。
1. How can I tell the binding which concrete task to use in particular context?
2. How to pass dynamic arguments (`arg` and `arg1` on above example) to `Bind<T>` method
注意: 如果您认为我的问题值得一票否决,请发表评论,以帮助我避免将来犯错误
【问题讨论】:
标签: c# ninject ioc-container