I've got a quite simple WPF application, trying to implement a correct MVVM pattern.
The application in itself is simple: one listItem and one textbox + button for searching. On init, the entire content of a "large" (~50k to ~1million lines) file is loaded into the listItem. When the users enters text in textbox and clicks on the button, I go through the entire file and get the corresponding elements and refresh the listItem.
Right now, everything is fairly dirty, as you can see in the code below. I'm wondering how I can deal with this file. Should I keep everything in memory like I'm doing right now? Or should I reparse the file every time I need to? And how would all of this fit in MVVM pattern? I was thinking of a "Messages" class as a Model.
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
this.LoadConversation();
}
private MessagesProcessor _messageProcessor = new MessagesProcessor("c.txt");
private void LoadConversation()
{
foreach (var msg in _messageProcessor.Messages)
{
ListBoxConversation.Items.Add(msg.ToString());
}
}
private void onSearchBtnClick(object sender, RoutedEventArgs e)
{
var results = new List<Message>();
results.AddRange(this._messageProcessor.GetSearchResults(word));
foreach (var result in results.OrderBy(p => p.Date))
{
ListBoxConversation.Items.Add(result.ToString());
}
}
}
MessageProcessor.cs
public class MessagesProcessor
{
private List<Message> _messages;
public List<Message> Messages
{
get
{
return _messages;
}
}
public MessagesProcessor(string pathOfBaseFile)
{
this._messages = ParseBaseMessageFile(pathOfBaseFile);
}
public IEnumerable<Message> GetSearchResults(string word)
{
return this._messages.Where(msg => msg.Content.Contains(word, StringComparison.OrdinalIgnoreCase));
}
}
Message.cs
public class Message
{
public DateTime Date { get; set; }
public string Content { get; set; }
public string Author { get; set; }
public override string ToString()
{
return "[" + this.Date.ToString("d") + " " + this.Date.ToString("t") + "]" + this.Author + " : " + this.Content;
}
}