views:

64

answers:

3

Short Version

I have a list of ints that I need to figure out how to persist through Application Shutdown. Not forever but, you get the idea, I can't have the list disappear before it is dealt with. The method for dealing with it will remove entry's in the list.

What are my options? XML?

Background

We have a WinForm app that uses Local SQL Express DB's that participate in Merge Replication with a Central Server. This will be difficult to explain but we also have(kind of) an I-Series 400 Server that a small portion of data gets written to as well. For various reasons the I-Series is not available through replication and as such all "writes" to it need to be done while it is available.

My first thought to solve this was to simply have a List object that stored the PK that needed to be updated. Then, after a successful sync, I would have a method that checks that list and calls the UpdateISeries() once for each PK in the list. I am pretty sure this would work, except in a case where they shut down innappropriately or lost power, etc. So, does anyone have better ideas on how to solve this? XML file maybe, though I have never done that. I worry about actually creating a Table in SQL Express because of Replication....maybe unfounded but...

For reference, UpdateISeries(int PersonID) is an existing Method in a DLL that is used internally. Re-writting it, as a potential solution to this issue, really isn't viable at the time.

+2  A: 

Sounds like you need to serialize and deserialize some objects.

See these .NET topics to find out more.

From the linked page:

Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be easily stored and transferred.

If it is not important for the on-disk format to be human readable, and you want it to be as small as possible, look at binary serialization.

Oded
A: 

If you already have and interact with a SQL database, use that, to get simpler code with fewer dependencies. Replication can be configured to ignore additional tables (even if you have to place them in another schema). This way, you can avoid a number of potential data corruption problems.

Pontus Gagge
A: 

Using the serialization mechanism is probably the way to go. Here is an example using the BinaryFormatter.

public void Serialize(List<int> list, string filePath)
{
  using (Stream stream = File.OpenWrite(filePath))
  {
    var formatter = new BinaryFormatter();
    formatter.Serialize(stream, list);
  }
}

public List<int> Deserialize(string filePath)
{
  using (Stream stream = File.OpenRead(filePath)
  {
    var formatter = new BinaryFormatter();
    return (List<int>)formatter.Deserialize(stream);
  }
}
Brian Gideon
Wow, thanks for the code sample. Is it a lot different it I'd like it "Human Readable"? Would that just be a different `Formatter`?
Refracted Paladin
@Refracted: Yep, that's right. Just use a different formatter.
Brian Gideon