Formatting and Sending Client Messages, use of xxxService classes
Need some advice on class design. I've just started to use xxxService classes, so not really sure when and how to use them.
A number of (identical) client applications connect to this program with socket TcpClient. Different type of messages needs to be sent from several different classes. Should I call a service class, with static methods, whenever I need to send some message (like code below suggests), or should I rather raise an event, and have some class registered to those events, that will format and send the messages. That would be a more lose coupling, so perhaps that's a better idea?
That would kind of hide that sequence of events from these classes, but on the other hand, it would be a centralized class where one can see easily all events that trigger a message send. Which feels like a benefit.
Would like to hear some advice on this. And if my last suggestion makes sense, what could such class be called, and would it be considered a ..Service class? Just to give me a better understanding.
cheers!
Example: (code is a bit "rough")
Class UserConnection
tcpClient As TcpClient
Public Sub SendMsg(msg as String)
' Sending by tcpClient
End Sub
End Class
Class UserAccount
userData as UserData
conn As UserConnection
balance as Decimal
Public Sub UpdateAc开发者_如何学Gocount(newBalance as Decimal)
' perform some logic
Dim history as new AccountHistory(me.Balance, ......, ....)
UserMessageFormatSendService(history)
End Sub
End Class
Class UserMessageFormatAndSendService()
Public Shared SendAccountUpdate(user as UserAccount, accountHistory as AccountHistory)
' Some code formatting it into a string message
Dim msg = .......
user.Conn.SendMsg(msg)
End Class
UserMessageFormatAndSendService should implement your service interface say, IUserMessageService - this way you could test it and/or replace functionality when needed
The class itself (UserMessageFormatAndSendService) is not "The Service", it is a proxy to the real service, so you could call it UserMessageServiceAgent.
精彩评论