如何使用NinjectConventionsExtension进行绑定?我喜欢使用Ninject自动绑定来绑定以下代码。是否可以在单个项目中同时使用手动和自动绑定?让我们来看看我想通过自动绑定实现的手动绑定。请告诉我如何实现这一目标。kernel.Bind().ToSelf().InRequestScope();kernel.Bind().To();下面的所有接口都继承自基本接口:IRepository3.kernel.Bind().To();4.内核.Bind().To();5.kernel.Bind().To().WithConstructorArgument("apikey",AppSettingsManager.GetSmsApiKey)Extra我需要为多个类编写.Exclude()如果我需要这样做.Exclude().Exclude().Exclude()?我需要为1和2单独手动绑定吗?或者1可以使用BindToSelf()'和.Configure(b=>b.InRequestScope())`来完成?是的,可以在同一个项目中使用约定绑定和单独绑定,甚至是在同一个模块中。IBindingRoot.Bind(x=>x.FromThisAssembly().IncludingNonePublicTypes().SelectAllClasses().InheritedFrom(typeof(IRepository)).BindDefaultInterface().Configure(y=>y.InRequestScope()));但是,您无法将构造函数参数传递给特定的类。所以我建议用一个包装对配置的访问的接口替换构造函数参数(无论如何这是一个很好的设计)。或者你也可以这样做:IBindingRoot.Bind(x=>x.FromThisAssembly().IncludingNonePublicTypes().SelectAllClasses().InheritedFrom(typeof(IRepository)).Exclude().BindDefaultInterface().Configure(y=>y.InRequestScope()));IBindingRoot.Bind().To).WithConstructorArgument("apikey",AppSettingsManager.GetSmsApiKey).InRequestScope();->你可以为每个存储库做一个.Exclude(),其中约定是绑定的当然不够。对于每个排除的绑定,您必须自己指定一个。如上:所有实现IRepository的类的条件绑定,MessageRepository类除外,它获得自己的绑定。另请查看:https://github.com/ninject/ninject.extensions.conventions/wiki/Projecting-Services-to-Bind附录:请注意,您可以指定多个通用绑定,例如:IBindingRoot。Bind(x=>x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IFoo)).BindDefaultInterface().Configure(y=>y.InRequestScope()));IBindingRoot.Bind(x=>x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IBar)).BindToSelf().Configure(y=>y.InRequestScope()));完全没问题。如果您只有少量异常,则以前的解决方案有效。如果它们更多,你会得到很多没有意义的约定。使用IBindingRoot.Rebind方法覆盖与约定已涵盖的绑定重叠的绑定。以上是C#学习教程:HowtouseNinjectConventionsExtensionforbinding?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——IBindingRoot.Bind(x=>x.FromThisAssembly().SelectAllClasses().BindAllInterface());IBindingRoot。Rebind().To).WithConstructorArgument("apikey",AppSettingsManager.GetSmsApiKey).InRequestScope();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: