【发布时间】:2011-11-22 22:38:56
【问题描述】:
我想我差不多明白了。
假设我希望我的应用发送短信。但我还不确定是否应该使用 Twilio 或 SomeOtherSMSService。事实上,我还真的不在乎。所以我有这么简单的东西,所以我可以继续开发我的应用程序。
public interface ISMSService{
public bool SendMessage(string to, string message);
}
现在我想试试 Twilio。这就是我感到困惑的地方。我可以将它安装为 Nuget 包,但我认为他们使用 REST API 的 C# 包装器根本不会匹配我的界面。修改它似乎也不是一个好主意。
从自述文件中我可以看到,要使用它,我需要做这样的事情
var msg = twilio.SendSmsMessage("+15551112222", "+15553334444", "Can you believe it's this easy to send an SMS?!");
我最好的猜测是我应该将它包装到我自己的接口实现中。 像这样。
using Twilio;
public TwilioSMSService : ISMSService
{
TwilioRestClient twilio;
public TwilioSMSService()
{
twilio = new TwilioRestClient("accountSid", "authToken");
}
public bool SendMessage(string to, string message)
{
var msg = twilio.SendSmsMessage("+15551112222", to, message);
if (msg != null) return true;
return false;
// this would obviously need more logic.
}
我想确保我保持依赖注入原则,但对我来说,我需要在默认构造函数中实例化 TwilioRestClient 似乎有些可疑,这正是我应该帮助您避免的依赖注入:s。
这种方法正确吗?如果没有,你会怎么做?
请帮忙。
【问题讨论】:
标签: asp.net asp.net-mvc dependency-injection dependencies