Какой интерфейс я должен реализовать, чтобы создать пошаговые события для моего класса? - PullRequest
0 голосов
/ 13 ноября 2008

Я хочу создать поведение, аналогичное классу чтения данных, но для специальной программы электронной почты, чтобы я мог выполнить следующее

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

Есть ли рекомендуемый интерфейс или класс mustInherit для использования функции пошагового перехода, чтобы команда sender.Send () подготавливала следующее письмо для отправки и возвращала значение true, если оно существует?

Ответы [ 2 ]

1 голос
/ 13 ноября 2008

нет - все, что вам нужно сделать, это реализовать метод Send () для подготовки следующего письма к отправке и вернуть true, если оно существует

вы, вероятно, думаете об интерфейсе IEnumerable, используемом для итераторов, но вам это не нужно для того, что вы хотите

0 голосов
/ 13 ноября 2008

Вот мое решение, я использовал свой собственный интерфейс и базовый класс для отправителя электронной почты, а затем дал псевдокод для конкретного класса.

   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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...