0

I'm new to C# and Windows Phone developing, I need to do app for school project.
I have simple class, and I want to save it to have access later, on next start of application.
What is the best (and easiest) method to do that? Using file, database or some other application memory?

Here is my class:

public class Place
{
    private string name;
    private string description;
    private int distance;
    private bool enabled;
    private GeoCoordinate coordinates;
}

I need to store multiple instances of class.

Djent
  • 2,877
  • 10
  • 41
  • 66

2 Answers2

2

There is no "best" way to do it; it depends on how you're going to use it.

A simple way is to use serialization, to XML, JSON, binary or whatever you want. I personally like JSON, as it's very lightweight and easy to read for a human. You can use the JSON.NET library to serialize objects to JSON.

For instance, if you want to serialize a collection of Place to a file, you can do something like that:

static async Task SavePlacesAsync(ICollection<Place> places)
{
    var serializer = new JsonSerializer();
    var folder = ApplicationData.Current.LocalFolder;
    var file = await folder.CreateFileAsync("places.json", CreationCollisionOption.ReplaceExisting);
    using (var stream = await file.OpenStreamForWriteAsync())
    using (var writer = new StreamWriter(stream))
    {
        serializer.Serialize(writer, places);
    }
}

And to read it back from the file:

static async Task<ICollection<Place>> LoadPlacesAsync()
{
    try
    {
        var serializer = new JsonSerializer();
        var folder = ApplicationData.Current.LocalFolder;
        var file = await folder.GetFileAsync("places.json");
        using (var stream = await file.OpenStreamForReadAsync())
        using (var reader = new StreamReader(stream))
        using (var jReader = new JsonTextReader(reader))
        {
            return serializer.Deserialize<ICollection<Place>>(jReader, places);
        }
    }
    catch(FileNotFoundException)
    {
        return new List<Place>();
    }
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Do you mind if I ask why you don't just do something like `string json=JsonConvert.Serialize(places); File.WriteAllText("places.json", places);`? That seems like a much easier way to go about serializing them. – mason Apr 22 '14 at 18:16
  • @mason, in most cases, you could do that and it would be fine. But what if you're trying to serialize a very large list? You would end up with an enormous string that will use a lot of memory... Using JsonSerializer, the JSON is written directly to the file; the complete JSON string is never in memory. So in general, I prefer to do it like that, even if it's usually not necessary. – Thomas Levesque Apr 22 '14 at 18:18
  • Okay, makes sense. But if I were explaining it to somebody that doesn't know how to serialize and deserialize to JSON, I'd probably stick to the simplest example rather than a more specialized. Not everyone is as experienced as you! – mason Apr 22 '14 at 18:21
1

I think the easiest way to make persistent your object is to store them in a file. However this is not the best way due to the time spent in IO operations, low security, etc.

Here you have a nice example:

How to quickly save/load class instance to file

Community
  • 1
  • 1
gyss
  • 1,753
  • 4
  • 23
  • 31