将事件添加到接口/实现 [英] Adding events to an interface / implementation

查看:148
本文介绍了将事件添加到接口/实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个界面:



IEmailDispatcher



看起来像这样:

  public interface IEmailDispatcher 
{
void SendEmail(string [] to,string fromName,string fromAddress,string subject,string body,bool bodyIsHTML,Dictionary< string,byte []>附件) ;
}

有点背景:



我有一个静态的EmailDispatcher类,其方法如下:
SendEmail(string [] to,string fromName,string fromAddress,string subject,string body,bool bodyIsHTML,Dictionary Attachments);



这样,通过IoC,然后加载相关的IEmailDispatcher实现,并调用该方法。



我的应用程序可以简单地调用EmailDispatcher .SendEmail(.........



我想添加事件,例如OnEmailSent,OnEmailFail等...
这样每个实施可以处理发送电子邮件的成功和失败,并相应地记录。



我该怎么做?



/ p>或者,是否有更好的方法?



目前,我使用的是一个基本上使用System.Net命名空间的BasicEmailDispatcher,创建一个MailMessage,并发送它。



将来,我将创建另一个cl屁股,不同的处理邮件...将它添加到sql数据库表以报告等....所以将处理OnEmailSent事件不同于BasicEmailDispatcher

解决方案

看起来像将静态类放在一起,让你在这里做尴尬的事情(具体来说,实现模板模式使用静态类)。如果调用者(应用程序)只需要了解 SendEmail 方法,那么这是界面中唯一需要的。



如果确实是这样,你可以让你的基本调度器类实现模板模式:

  public class EmailDispatcherBase :IEmailDispatcher {
//欺骗这里的参数来节省空间
public void SendEmail(object [] args){
try {
// basic implementation here
this.OnEmailSent();
}
catch {
this.OnEmailFailed();
throw;
}
}
protected virtual void OnEmailSent(){}
protected virtual void OnEmailFailed(){}
}

更复杂的实现将继承自 BasicEmailDispatcher (因此实现 IEmailDispatcher )并覆盖一个或两个虚拟方法来提供成功或失败的行为:

  public class EmailDispatcherWithLogging:EmailDispatcherBase {
protected override OnEmailFailed(){
//将错误日志条目写入数据库
}
}
/ pre>

Know this has been asked before, however my question is slightly different.

I have an interface:

IEmailDispatcher

It looks like this:

public interface IEmailDispatcher
{
    void SendEmail(string[] to, string fromName, string fromAddress, string subject, string body, bool bodyIsHTML, Dictionary<string, byte[]> Attachments);
}

As a bit of background:

I have a static EmailDispatcher class that has the method: SendEmail(string[] to, string fromName, string fromAddress, string subject, string body, bool bodyIsHTML, Dictionary Attachments);

This, throught IoC, then loads the relevant IEmailDispatcher implementation, and calls that method.

My applications can then simply call EmailDispatcher.SendEmail(.........

I want to add events to it, for example OnEmailSent, OnEmailFail etc... So that each implementation can handle successes and failures of sending emails, and log them accordingly.

How would I go about doing this?

Or, is there a better way?

At the moment, I'm using a "BasicEmailDispatcher" that basically uses the System.Net namespaces, creates a MailMessage, and sends it.

In the future, I will create another class, that handles mail differently... adds it to a sql db table for reporting etc.... and so will handle the OnEmailSent events differently to the BasicEmailDispatcher

解决方案

It looks like trying to fit everything into the static class is making you do awkward things here (specifically, implementing the template pattern using a static class). If callers (applications) only need to know about the SendEmail method, that's the only thing that should be in the interface.

If that is indeed the case, you can just make your base dispatcher class implement the template pattern:

public class EmailDispatcherBase: IEmailDispatcher {
    // cheating on the arguments here to save space
    public void SendEmail(object[] args) {
        try {
            // basic implementation here
            this.OnEmailSent();
        }
        catch {
            this.OnEmailFailed();
            throw;
        }
    }
    protected virtual  void OnEmailSent() {}
    protected virtual  void OnEmailFailed() {}
}

More complex implementations would each inherit from BasicEmailDispatcher (and therefore implement IEmailDispatcher) and override one or both of the virtual methods to provide success or failure behavior:

public class EmailDispatcherWithLogging: EmailDispatcherBase {
    protected override OnEmailFailed() {
        // Write a failure log entry to the database
    }
}

这篇关于将事件添加到接口/实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆