我們使用Ninject進行依賴注入。我們使用自由格式解釋的DDD設計我們的代碼,其中域對象是IThing。通用類型Ninject自動解析
在下面的代碼中,如何使用IThing實例獲取IThingControl?
interface IThing {}
class Thing : IThing {}
interface IThingControl<T> where T:IThing {}
class ThingControl<Thing> {}
class Module : NinjectModule {
public override void Load() {
Bind<IThingControl<Thing>>().To<ThingControl>();
}
}
class SomewhereInCode {
void AddControls() {
List<IThing> things = new List<IThing> {
new Thing()
};
foreach (IThing thing in things) {
IThingControl<IThing> control = _kernel.Get(); // <----- eh?
this.Controls.Add(control);
}
}
}