2008-11-13 39 views
0

我想创建一个类似的行为在数据读取器类,但对于一个定制的电子邮件使用者的程序,这样我可以做跟随我应该实现什么接口来为我的课程创建步驱动事件?

Dim sender As New EmailSender(emailTemplate) 
While sender.Send() 
    Response.Write(sender("HTMLContent")) 
End While 

是否有建议接口或为MustInherit类利用步进功能,以便发件人.Send()命令准备发送下一封电子邮件,并返回true(如果存在)?

回答

1

没有 - 所有你需要做的是落实send()方法,以准备下一个电子邮件发送,如果它存在

你可能在想用于迭代IEnumerable接口的返回true,但你不需要你想要的东西

+0

够公平的 - 我会发布我的代码 – digiguru 2008-11-13 14:07:13

0

这是我的解决方案,我用我自己的接口和基类来发送邮件,然后为具体类提供一些伪代码。

Namespace Emailer 

     Public Interface IBatchableEmailSender 
      Function SendNextEmail() As Boolean 
      Sub PrepareBatchEmail() 
      Property EmailOutput() As EmailOutput 
     End Interface 

     Public MustInherit Class BaseBatchEmailSender 
      Implements IBatchableEmailSender 

      Private _emailOutput As EmailOutput 
      Public Property EmailOutput() As EmailOutput Implements IBatchableEmailSender.EmailOutput 
       Get 
        Return _emailOutput 
       End Get 
       Set(ByVal value As EmailOutput) 
        _emailOutput = value 
       End Set 
      End Property 
      Public MustOverride Sub PrepareBatchEmail() Implements IBatchableEmailSender.PrepareBatchEmail 
      Public MustOverride Function SendNextEmail() As Boolean Implements IBatchableEmailSender.SendNextEmail 

      Public Sub New() 
       PrepareBatchEmail() 
      End Sub 

     End Class 
Public Class BatchCustomerEmail 
     Inherits BaseBatchEmailSender 

     Private EmailItems As New Generic.List(Of EmailItem) 
     Private EmailItemNumber As Integer 
     Private NextEmailItem As EmailItem 

     Protected Class EmailItem 
      Public MemberID As Integer 
      Public Sub New(ByVal memberID As Integer) 
       Me.MemberID = memberID 
      End Sub 
     End Class 

     Public Overrides Function SendNextEmail() As Boolean 
      Dim hasEmail As Boolean = EmailItemNumber < EmailItems.Count 
      If hasEmail Then 
       ' Run script to send email 
       ' If necessary mark email as sent in the database   
       EmailItemNumber = EmailItemNumber + 1 
      End If 
      Return hasEmail 

     End Function 

     Public Overrides Sub PrepareBatchEmail() 
      ' 
      ' Creates a Generic.List(of EmailItems) to email. 
      ' 
      EmailItemNumber = 0 
     End Sub 


    End Class 


    Public Class EmailOutput 
     Private _text As String 

     Public Property Text() As String 
      Get 
       Return _text 
      End Get 
      Set(ByVal value As String) 
       _text = value 
      End Set 
     End Property 
     Private _html As String 
     Public Property HTML() As String 
      Get 
       Return _html 
      End Get 
      Set(ByVal value As String) 
       _html = value 
      End Set 
     End Property 
     Private _error As String 
     Public Property ErrorMessage() As String 
      Get 
       Return _error 
      End Get 
      Set(ByVal value As String) 
       _error = value 
      End Set 
     End Property 
     Public Sub New(ByVal errorMesage As String, ByVal html As String, ByVal text As String) 
      Me.ErrorMessage = errorMesage 
      Me.HTML = html 
      Me.Text = text 
     End Sub 
    End Class 

End Namespace 
+0

你应该编辑你的原始问题,而不是发布答案 – 2008-11-13 15:34:08

相关问题