What interface should I implement to create stepped events for my class?

I want to create similar behavior for the data reader class but for a custom email program so that I can do the following

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

      

Is there a recommended interface or mustInherit class for using the stepping functionality so that sender.Send () prepares the next email to send and returns true if it exists?

0


source to share


2 answers


no - all you have to do is implement the Send () method to prepare the next email to be sent and returns true if it exists



you are probably thinking of the IEnumerable interface which is used for iterators, but you don't need what you want

+1


source


Here's my solution, I used my own interface and base class for the email sender and then gave some pseudocode for the specific class.



   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


source







All Articles