Gan*_*tor 5 asp.net-mvc dependency-injection ninject
我试图将两个具体类绑定到一个接口.我应该在Ninject中使用什么命令来做到这一点?我所试图做的是结合两个具体类到控制器名称一个接口基地.那可能吗?我想,在ninject使用.当给予条件但没有教程在那里,他们告诉你如何使用.当用于ninject.
这里有几个例子.查看Ninject源项目及其测试子项目的各种使用样本,这是最好的文档,特别是因为尚未针对v2更新文档.
// usage of WhenClassHas attribute
Bind<IRepository>().To<XmlDefaultRepository>().WhenClassHas<PageAttribute>().WithConstructorArgument("contentType", ContentType.Page);
// usage of WhenInjectedInto
Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(ServicesController));
Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(PageController)).WithConstructorArgument("contentType", ContentType.Page);
Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(WidgetZoneController)).WithConstructorArgument("contentType", ContentType.WidgetZone);
// you can also do this
Bind<IRepository>().To<PageRepository>().WhenInjectedInto(typeof(PageController)).WithConstructorArgument("contentType", ContentType.Page);
Bind<IRepository>().To<WidgetZoneRepository>().WhenInjectedInto(typeof(WidgetZoneController)).WithConstructorArgument("contentType", ContentType.WidgetZone);
// or this if you don't need any parameters to your constructor
Bind<IRepository>().To<PageRepository>().WhenInjectedInto(typeof(PageController));
Bind<IRepository>().To<WidgetZoneRepository>().WhenInjectedInto(typeof(WidgetZoneController));
// usage of ToMethod()
Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current));
Run Code Online (Sandbox Code Playgroud)
HTH
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |