Properly managing a collection from deserialized XML file throughout app's lifetime I'm developing a WinForms app to manage some settings and profiles for multiple video games. The list of video games is specified by the user, and I'm trying to figure out how to properly maintain that list/collection throughout the lifetime of my application.

I decided on an XML file to manage the games list which looks like this:



&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;Games&gt;
  &lt;Game&gt;
    &lt;Name&gt;NBA 2K13&lt;/Name&gt;
    &lt;FolderPath&gt;C:\Games\NBA2K13&lt;/FolderPath&gt;
  &lt;/Game&gt;
  &lt;Game&gt;
    &lt;Name&gt;First Person Shooter&lt;/Name&gt;
    &lt;FolderPath&gt;C:\Games\FPS&lt;/FolderPath&gt;
  &lt;/Game&gt;
&lt;/Games&gt;


It was suggested to me that I should not continually read/write to the XML file; instead, I should load the XML file into a collection (I'm using serialization for this), let the app work off of the collection, and then persist the collection back to the XML file when the app is closed. For the game collection class (Games), I'm also treating it as a hybrid "repository" (see the Add() method).

I came up with the following classes for that:



public class Game
    {
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("FolderPath")]
        public string FolderPath { get; set; }

        public Game() {}

        public Game(string name, string folder)
        {
            Name = name;
            FolderPath = folder;
        }
    }

[XmlRootAttribute("Games")]
    public class Games
    {
        public Games()
        {
            Items = new List&lt;Game&gt;();
        }

        [XmlElement("Game")]
        public List&lt;Game&gt; Items { get; set; }

        public void Add(Game game)
        {
            if (!Items.Any(g =&gt; g.Name.EqualsIgnoreCase(game.Name)))
            {
                if (Directory.Exists(game.FolderPath))
                    Items.Add(game);
                else
                    MessageBoxUtil.ShowError("Invalid directory entered.");
            }
            else
                MessageBoxUtil.ShowError("Game names must be unique. " + game.Name + " already exists.");
        }
    }


This code seems to work fine, and loading/persisting the data is working as planned (I'd still appreciate suggestions on whether this is a good approach). What I'm struggling with is how to maintain that collection throughout the app's lifecycle. Right now, there are 2 forms that need access to this collection (Games class). Should the Games class be made static since the Items collection is derived from a single XML file and could possibly be considered global data? Would it be better to implement the singleton pattern and pass around a single instance of Games?
Both a singleton solution and a static instance are acceptable.  What I would recommend is that you add a static factory to Games, like:

public static Games LoadFromFile(String xmlFile);


You would call this near the top level of your application and then you would pass it into any of your other code that requires it.  It's essentially dependency injection, in that you design the code that's going to use the Games object to have it injected it.  It makes testing easier and it makes the design more flexible.