Ninject 多个绑定的构造函数参数
Ninject constructor arguments for multiple bindings
我有一个简单的 class 用于处理通知。
public class ApplePushNotifier : IApplePushNotifier
{
public ApplePushNotifier(
ILog log,
IMessageRepository messageRepository,
IUserRepository userRepository,
CloudStorageAccount account,
string certPath)
{
// yadda
}
// yadda
}
还有一个简单的 Ninject 绑定,其中包括用于定位本地证书文件的字符串参数:
kernel.Bind<IApplePushNotifier>().To<ApplePushNotifier>()
.WithConstructorArgument("certPath",
System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
这显然是非常基础的,而且一切都运行良好。现在,我向 class:
添加了第二个接口
public class ApplePushNotifier : IApplePushNotifier, IMessageProcessor
我可以像这样添加第二个绑定:
kernel.Bind<IMessageProcessor>().To<ApplePushNotifier>()
.WithConstructorArgument("certPath",
System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
这也有效,但重复的构造函数参数让我感到荨麻疹。我尝试添加这样的显式自我绑定:
kernel.Bind<ApplePushNotifier>().To<ApplePushNotifier>()
.WithConstructorArgument("certPath",
System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
kernel.Bind<IApplePushNotifier>().To<ApplePushNotifier>();
kernel.Bind<IMessageProcessor>().To<ApplePushNotifier>();
但是没有骰子 - 我得到了旧的 "No matching bindings are available" 错误。
有没有办法像这样指定一个构造函数参数,既不将其提升为它自己的可绑定类型,也不为 class 实现的每个接口重复它?
根据 ApplePushNotifier 内部工作的性质,然后绑定到常量可能会有所帮助并防止重复自己。
kernel.Bind<ApplePushNotifier>().ToSelf()
.WithConstructorArgument("certPath", System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
var applePushNotifier = Kernel.Get<ApplePushNotifier>();
kernel.Bind<IApplePushNotifier>().ToConstant(applePushNotifier);
kernel.Bind<IMessageProcessor>().ToConstant(applePushNotifier);
希望对您有所帮助。
只需为两个服务接口创建一个绑定:
kernel.Bind<IApplePushNotifier, IMessageProcessor>().To<ApplePushNotifier>()
.WithConstructorArgument(
"certPath", System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"))
我有一个简单的 class 用于处理通知。
public class ApplePushNotifier : IApplePushNotifier
{
public ApplePushNotifier(
ILog log,
IMessageRepository messageRepository,
IUserRepository userRepository,
CloudStorageAccount account,
string certPath)
{
// yadda
}
// yadda
}
还有一个简单的 Ninject 绑定,其中包括用于定位本地证书文件的字符串参数:
kernel.Bind<IApplePushNotifier>().To<ApplePushNotifier>()
.WithConstructorArgument("certPath",
System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
这显然是非常基础的,而且一切都运行良好。现在,我向 class:
添加了第二个接口public class ApplePushNotifier : IApplePushNotifier, IMessageProcessor
我可以像这样添加第二个绑定:
kernel.Bind<IMessageProcessor>().To<ApplePushNotifier>()
.WithConstructorArgument("certPath",
System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
这也有效,但重复的构造函数参数让我感到荨麻疹。我尝试添加这样的显式自我绑定:
kernel.Bind<ApplePushNotifier>().To<ApplePushNotifier>()
.WithConstructorArgument("certPath",
System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
kernel.Bind<IApplePushNotifier>().To<ApplePushNotifier>();
kernel.Bind<IMessageProcessor>().To<ApplePushNotifier>();
但是没有骰子 - 我得到了旧的 "No matching bindings are available" 错误。
有没有办法像这样指定一个构造函数参数,既不将其提升为它自己的可绑定类型,也不为 class 实现的每个接口重复它?
根据 ApplePushNotifier 内部工作的性质,然后绑定到常量可能会有所帮助并防止重复自己。
kernel.Bind<ApplePushNotifier>().ToSelf()
.WithConstructorArgument("certPath", System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"));
var applePushNotifier = Kernel.Get<ApplePushNotifier>();
kernel.Bind<IApplePushNotifier>().ToConstant(applePushNotifier);
kernel.Bind<IMessageProcessor>().ToConstant(applePushNotifier);
希望对您有所帮助。
只需为两个服务接口创建一个绑定:
kernel.Bind<IApplePushNotifier, IMessageProcessor>().To<ApplePushNotifier>()
.WithConstructorArgument(
"certPath", System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12"))