Storing separate data and group on common key
Working with an API that can handle multiple connections (i.e. sessions), each of these sessions has a uniqueID (sessionID) and keeps track of all inbound and outbound messages via sequence numbers.
Class1
public void Method1(SessionID sessionID, Message message)
{
var ID = sessionID;
var foo = message.InSeqNo;
}
public void Method2(SessionID sessionID, Message message)
{
var ID = SessionID;
var bar = message.OutSeqNo;
}
I'm curious how would you recommend storing this data together so each instance can be displayed independently based on its sessionID.
There is no need to store previous numbers because th开发者_开发技巧ey are only displayed. None of the SessionIDs will change and each sequence number will update independently.
The best thing I can think of is using two separate fields like Dictionary<sessionID, number>
, .Clear each field on update, re-add with new values, then join the two via LINQ and display the information.
Why not a single class that contains the inbound and outbound sequence numbers for a particular ID?
class SessionInfo
{
public int SessionId { get; private set; }
public int InSeqNo { get; set; }
public int OutSeqNo { get; set; }
}
Dictionary Sessions = new Dictionary();
To update a session's InSeqNo, then:
SessionInfo sess;
if (Sessions.TryGetValue(sessionId, out sess))
{
++sess.InSeqNo;
}
Or am I missing something fundamental in your question?
精彩评论