2
\$\begingroup\$

I have library which sending/logging information by diffrent log channels like by email, sms or save to file. It works fine but i would like to get your opinion and get code review regarding my current code. Is it clear? Is it fine enough according to OOP and interface usage. Morover i would like to change it a bit and counting on your support.

Description:

For some specific class (for example SpecificClass) i am registering which channels i want to use and passing it via NotificationChannelEngine in ctor. One issue here is once i register all channels, every place when i do Send all registered classes are used. The point is i would like sometime say - ok even if I have 3 channels registered for some specific line i want to use just 2 of 3 registered channels or maybe just one of them. Any proposition for change/tune up much appreciated as answer.

Example: Below line will call all egistered channels:

NotificationChannelEngine.Process(New Message() With {.Text = "DoSomething1 --> Some operation 1"}

How to say for instance for this line only Email channel to be used only?

Entire current code:

Interfaces:

Public Interface INotificationChannel
       Sub Send(message As Message)
End Interface

Public Interface INotificationChannelEngine
       Sub Process(message As Message)
End Interface

Concrete channels:

Public Class FileNotificationChannel : Implements INotificationChannel          
       Public Path as string

       Public Sub Send(message As Message) Implements INotificationChannel.Send
              'Log class to log message to file
               Logger.Log(message.Text, Path)
       End Sub
End Class

Public Class MailNotificationChannel : Implements INotificationChannel 
       Public SmtpIp As String
       Public FromAddress As String
       Public ToAddress As String

Public Async Sub Send(message As Message) Implements INotificationChannel.Send
       Await New Email().Send()
End Sub
End Class

Public Class SmsNotificationChannel: Implements INotificationChannel
        Public Sub Send(message As Message) Implements INotificationChannel.Send
             'Real Sms method to be immplemented here, right now Console for testing
              Console.WriteLine("Sending sms")
        End Sub
End Class

Now having some specific class like this:

Public Class SpecificClass

      Public NotificationChannelEngine As INotificationChannelEngine

      Sub New(notificationengine As INotificationChannelEngine)
            NotificationChannelEngine = notificationengine
      End Sub

      Public Sub Encode(whatever As String)
           DoSomething1()
           DoSomething2()
           DoSomething3()
      End Sub

      Public Sub DoSomething1()
           'Some operation 1
           NotificationChannelEngine.Process(New Message() With {.Text = "DoSomething1 --> Some operation 1"})

           'Some operation 2
           NotificationChannelEngine.Process(New Message() With {.Text = "DoSomething1 --> Some operation 2"})
       End Sub

       Public Sub DoSomething2()
           'Some operation 1
           NotificationChannelEngine.Process(New Message() With {.Text = "DoSomething2 --> Some operation 1"})

           'Some operation 2
           NotificationChannelEngine.Process(New Message() With {.Text = "DoSomething2 --> Some operation 2"})
        End Sub

        Public Sub DoSomething3()
            'Some operation 1
            NotificationChannelEngine.Process(New Message() With {.Text = "DoSomething3 --> Some operation 1"})
        End Sub

End Class

This class is used to be used in concrete specific classes:

Public Class NotificationChannelEngine :Implements INotificationChannelEngine

      Private ReadOnly _notificationChannels As IList(Of INotificationChannel)

      Sub New()
          _notificationChannels = New List(Of INotificationChannel)
      End Sub


      Public Sub RegisterNotificationChannel(channel As INotificationChannel)
          _notificationChannels.Add(channel)
      End Sub

      Public Sub Send(message As Message) Implements INotificationChannelEngine.Process
          For Each channel In _notificationChannels
              channel.Send(message)
          Next
      End Sub
End Class

Test to see on some exampe specific class:

Module Module1

Sub Main()
        Dim notificationChannel As New NotificationChannelEngine
        With notificationChannel
            .RegisterNotificationChannel(New MailNotificationChannel With {.SmtpIp = "192.168.0.1", .FromAddress = "from@email.com", .ToAddress = "to@email.com"})
            .RegisterNotificationChannel(New SmsNotificationChannel)
            .RegisterNotificationChannel(New FileNotificationChannel() With {.Path = "C:\Users\HP\Desktop\Log.txt"})
        End With

        Call New SpecificClass(notificationChannel).Encode("New Video")
        Call New SpecificOtherClass(otherNotificationChannel).Encode("New Video")
        Console.ReadLine()
End Module
\$\endgroup\$
  • \$\begingroup\$ anyone to give any feedback? \$\endgroup\$ – Mickey25 Mar 29 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.