Question

I'm writing a little tool to connect to a MS SCOM Server and add subcriptions for monitoring alerts. How ever, I want to save the connection and subscription settings somwhere/some how. Here is the rough structure

public class ConnectionManager
{
    ...
    public BindingList<Connection> Connections // All the connection we know
    ...
}

Then we have the connections. Basicly this class saves the needed connection fields like Server Name, User Domain, connects to the server, keeps the connection alive etc etc

public class Connection
{
    ...
    public string Name;                               // the name of this connection
    public string ServerName;                         // the scom server name
    public string UserDomain;                         // the domain, NETBIOS or FQDN
    public BindingList<Subscription> Subscriptions;   // The subscriptions for this connection
    ...
}

Finally, the connection class has Alert Criterias. Alert criterias are basically some kind of regular expressions. The are currently only "extended" (just a string) but I'm going to build simple expression which are going to be build on the fly. It will look like this

public class Subscription
{
    ...
    public string ExtededExpression;                        // if not empty, use this
    public BindingList<CriteriaOperator> SimpleOperators;   // otherwhise build a regular expression from this stuff
    ...
}

with another subclass

public class CriteriaOperator
{
    ...
    public string Field;                  // The field, g.g. LastModified
    public LogicalOperator Operator;      // The logical operator, e.g. LIKE
    public string CompareFieldTo;         // A regular expression, e.g. "%SQL%"
    ...
}

And now my question: What do you think is the best way to save such a structure? I'm new to .NET so I'm not sure: Use the Application Settings? Or serialze/deserialize this structure with a XML Serializer? Or write some wrapper classes and then use a XML Serializer? Or is there maybe a completely other approach?

Thanks for help!

Was it helpful?

Solution

I recommend NOT TO use ApplicationSettings. IMHO ApplicationsSettings is to store and reload your UI state e.g. the last 10 used files in a text editor or "do not show this message again" for the specific user!

If you are thinking of a load/save scenario I do recommend to create a single class to store all fields needed to save your application state. Do not directly store some of your logic classes with serialization it will mess up things and reduce your maintainability.

By using one single save item you have all your load/store actions centralized in one place.

So...

public class SaveItem
{
    // ----------------------------------------------
    // version 1.02:
    public bool New { get; set; }
    public string VeryNew { get; set; }

    // ----------------------------------------------
    // version 1.01:

    public bool Bugfix { get; set; }
    public List<string> AList { get; set; } 

    public SaveItem()
    {
        // ...
    }

    public bool TrySave(string fullFilePath)
    {
        bool result = false;
        TextWriter textWriter = new StreamWriter(fullFilePath);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveItem));
        try
        {
            xmlSerializer.Serialize(textWriter, this);
            result = true;
        }
        catch (IOException)
        {

        }
        finally
        {
            try
            {
                textWriter.Close();
            }
            catch
            { }
        }
        return result;
    }


    public static bool TryLoad(string fullFilePath, out SaveItem saveItem)
    {
        bool result = false;
        saveItem = new SaveItem();
        TextReader textReader = null;
        try
        {
            textReader = new StreamReader(fullFilePath);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveItem));
            saveItem = (SaveItem)xmlSerializer.Deserialize(textReader);
            if (saveItem != null)
            {
                result = true;
            }
        }
        catch (FileNotFoundException)
        {
            if (saveItem != null)
            {
            }
        }
        finally
        {
            if (textReader != null)
            {
                textReader.Close();
            }
        }
        return result;
    }

}

OTHER TIPS

As for me, for that purposes you should:

  • Write a singleton class (here is a great article about it on my blog) for handling settings.
  • Use binary or XML serialization - as it is easy and customizable
  • Save the settings to generic path, I have already answered where to store it
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top