I am d开发者_StackOverflow社区esigning a list-based app in C# WinForms where I want to store my data in an XML file (which will eventually get serialized into a database). I have a custom control that displays my list data based on my XML, but I'm having trouble determining the best structure to handle the data. I have a ListManager class that keeps track of an XMLDocument, and has methods like CreateList and AddListItem. Should I be using a local copy of the XML structure as what my UI Control reads from, or should I use the actual XML File itself? Is there something better than the XMLDocument that I should be storing as a member variable to keep hold of the data?
Also, how should I go about linking the controller with the UI control? In other words, how should my ListManager alert the ListControl when a new item was added? (Or should it not be doing the update notification at all, and instead the Main Form should do that?)
Thanks!
I would create a custom class to hold my data using strongly typed collections such as the generic collections System.Collections.Generic.List. Then, you can just serialize this class to and from XML on the client until you are ready to push the content into a database. Using serialization means you can work with plain old C# objects instead of trying to shoe-horn the XMLDocument class into your business object.
Take a look at this article for some details on how serialization works.
imho the scenario you describe could lend itself to several strategies, and key criteria for which implementation strategy you choose might be the dynamic nature of the "data flow," and the sheer size of the data you are handling.
If you are describing a situation where you are dealing with vast stores of data, so that you will not be able to maintain "entire" the internal representation of the external file because of memory constraints : that's one case : in that case you will require "virtualisation" in the sense of that word used to describe presenting "partial views" of the data at any one given moment in time, and, probably, caching so that you keep as much data as you can "ready to display." Or you may elect to use another control for display (like DataGridView) which has "virtual partial display" built-in.
If you are dealing with "asynchronous flows" of new data being added, existing data being removed or edited, hundreds (or thousands) of times per minute, in any order (perhaps from multiple users ?) that's another case.
So, when I suggest you to consider a strategy where :
the ListManager raises custom events to notify subscribers of data change
the custom control (that we assume handles presentation) subscribes to the events of interest raised by the ListManager class, and "does the right thing" : which might involve requesting another "serving" of data from the ListManager, or whatever, to make the current presentation up-to-date.
Please consider that may reflect the simplest possible scenario.
精彩评论