I am writing a chat. I use the static list tempMessages to store messages in RAM.

But each time when the studio application is restarted, the list is filled with strange garbage. I do step by step debugging, but it's not clear where this garbage comes from. Every time I run a step-by-step debugging, the static tempMessages list increases its size. Tell me why this list is increasing?

Code

public partial class Messages : System.Web.UI.Page { public static List<string> tempMessages = new List<string>(); protected void Page_Load(object sender, EventArgs e) { Message message = new Message(); message.ReadFromDataBase(); tempMessages.Add(String.Format("{0}: {1} \n", message.ReceivedDate, message.ReceivedMessage)); message.DeleteMessageFromDb(); string formattedText = string.Empty; foreach (var tempMessage in tempMessages) { formattedText += tempMessage; } chatMenu.InnerHtml = formattedText; } } public class Message { private string sendedMessage; private DateTime sendedDate; public string ReceivedMessage { get; private set; } public DateTime ReceivedDate { get; private set; } private int messageID; public Message() { } public Message(string message) { sendedMessage = message; sendedDate = DateTime.Now; } public void SaveToDataBase() { WebChatEntities webChatDb = new WebChatEntities(); ChatMessage chatMessage = new ChatMessage() { ChMessage = sendedMessage, MessageDate = sendedDate }; webChatDb.ChatMessages.AddObject(chatMessage); webChatDb.SaveChanges(); } public void ReadFromDataBase() { WebChatEntities webChatDb = new WebChatEntities(); var result = from chMessage in webChatDb.ChatMessages let maxDate = webChatDb.ChatMessages.Max(message => message.MessageDate) where chMessage.MessageDate == maxDate select new { chatMessage = chMessage.ChMessage, messageDate = chMessage.MessageDate, ID = chMessage.MessageID }; foreach (var message in result) { ReceivedMessage = message.chatMessage; ReceivedDate = message.messageDate; messageID = message.ID ; } } public void DeleteMessageFromDb() { WebChatEntities webChatDb = new WebChatEntities(); IEnumerable<ChatMessage> result = from message in webChatDb.ChatMessages where message.MessageID == messageID select message; foreach (ChatMessage chatMessage in result) { webChatDb.ChatMessages.DeleteObject(chatMessage); } webChatDb.SaveChanges(); } } 

    1 answer 1

    1. What does garbage mean?
    2. Why keep messages in a static list? How are you going to limit access? It is logical to assume that after each page refresh, a page event fires on the page in the code of which you add messages to the list again ... I do not see the code that clears the list.

    In addition, classes of the domain model that can (I have methods) work with the database clearly contradict the principle of common responsibility.

    • Empty messages are understood as garbage. With the event load I understood. Is it better to check for null? If the message is empty, then do not add it to memory. The static list I use for storage in RAM - ArniLand
    • If your chat is supposed to be "open" (any user sees all messages), then checking for NULL will most likely solve the problem. I still do not understand the logic of the program: you access the database with each request to this page, therefore why bother to keep messages in memory? Just pull them out every time and that's all .... - wind
    • As a feedback, I asked a normal question or could I find a solution myself? - ArniLand