我应该使用什么界面来为我的班级创建步骤驱动的事件?

时间:2008-11-13 13:46:23

标签: asp.net vb.net interface

我想为数据阅读器类创建一个类似的行为,但对于定制的电子邮件程序,我可以执行以下操作

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

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

2 个答案:

答案 0 :(得分:1)

不 - 你所要做的就是实现Send()方法来准备下一封发送电子邮件,如果存在则返回true

你可能正在考虑用于迭代器的IEnumerable接口,但你不需要那个你想要的东西

答案 1 :(得分: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