我正在编写一个VB.Net应用程序,该应用程序需要通过TCP套接字接收消息,但是我的问题确实与套接字无关。为了简单起见,假设我要创建一个类来处理来自Socket类事件的数据,该事件是我们公司创建的类库的一部分。在研究依赖项注入时,this article和this one表示不要仅将Socket实例注入消息处理器的构造函数中,以便处理器可以订阅事件。相反,我应该给我的处理器一个公共函数,让组合根创建一个套接字和一个处理器,并从该套接字连接处理程序,以便它在处理器上调用公共函数。起初效果很好。
但是现在我有一种情况,需要动态创建和处理套接字。上述技术的问题在于套接字,处理器和事件订阅将在应用程序的生命期内有效。我以为可以使用工厂来创建处理器对象。然后,工厂将创建一个套接字,处理器,并订阅事件处理程序。但是它只会将处理器返回给客户端,那么如何取消订阅处理程序呢?我不想因为未能取消订阅事件处理程序而导致泄漏。
已更新
以下代码是完整的控制台应用程序。希望我在简单性和解释问题之间找到了适当的平衡。
Imports System.Collections.Concurrent
Module Module1
Sub Main()
'Create a connection
Dim ConnectionClient As New ConnectionClient(New MessageConnectionFactory)
'Do some sending and receiving
ConnectionClient.DoSomething()
'Wait so the user can see results
Console.WriteLine("Press any key")
Console.ReadLine()
End Sub
End Module
Public Class DataProducedEventArgs
Inherits EventArgs
Public Property Data As Byte()
End Class
Public Class Socket
Public Event DataProduced As EventHandler(Of DataProducedEventArgs)
'In a real application, the event would fire when we receive data on the socket.
'This provides a way to simulate that.
Public Sub SimulateReceivedData(Data As Byte())
RaiseEvent DataProduced(Me, New DataProducedEventArgs With {.Data = Data})
End Sub
Public Sub Send(Data As String)
Console.WriteLine("Sending over socket: {0}", Data)
End Sub
End Class
Public Class MessageProcessor
Public Sub Consume(Data As Byte())
Console.WriteLine("Received {0} bytes of data", Data.Length)
End Sub
End Class
Public Class MessageSender
Private _socket As Socket
Public Sub New(socket As Socket)
_socket = socket
End Sub
Public Sub Send(Message As String)
_socket.Send(Message)
End Sub
End Class
Public Class MessageConnection
Public Property Processor As MessageProcessor
Public Property Sender As MessageSender
End Class
Public Class ConnectionClient
Private _Factory As MessageConnectionFactory
Private Connections As New ConcurrentQueue(Of MessageConnection)
Public Sub New(factory As MessageConnectionFactory)
_Factory = factory
End Sub
Public Sub DoSomething()
'Get a connection
Dim Connection As MessageConnection = _Factory.Create
'Do something with it
Connection.Sender.Send("Test")
'TODO - I could dispose of it here but that doesn't
'remove the event handlers
End Sub
End Class
Public Class MessageConnectionFactory
Public Function Create() As MessageConnection
'Create the objects
Dim MySocket As New Socket
Dim Connection As New MessageConnection
Connection.Processor = New MessageProcessor
Connection.Sender = New MessageSender(MySocket)
'How could this handler ever get removed?
AddHandler MySocket.DataProduced, Sub(sender, e) Connection.Processor.Consume(e.Data)
'The client only cares about the message connection object, not the socket
Return Connection
End Function
End Class
答案 0 :(得分:0)
这对您有用吗?
Public Class ConnectionClient
Private _Factory As MessageConnectionFactory
Public Sub New(factory As MessageConnectionFactory)
_Factory = factory
End Sub
Public Sub DoSomething()
_Factory.Using(Sub(c) c.Sender.Send("Test"))
End Sub
End Class
Public Class MessageConnectionFactory
Public Sub [Using](action As Action(Of MessageConnection))
Dim MySocket As New Socket
Dim Connection As New MessageConnection
Connection.Processor = New MessageProcessor
Connection.Sender = New MessageSender(MySocket)
Dim handler = New EventHandler(Of DataProducedEventArgs)(Sub(sender, e) Connection.Processor.Consume(e.Data))
AddHandler MySocket.DataProduced, handler
action(Connection)
RemoveHandler MySocket.DataProduced, handler
End Sub
End Class