2013-11-28 29 views
1

我在寫一個連接到MS SCOM服務器的小工具,並添加了用於監視警報的子集。如何,我想保存連接和訂閱設置somwhere /一些如何。這裏是粗糙的結構在.NET C中保存複雜的用戶設置#

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

然後,我們有連接。 Basicly該級撲救像服務器名稱,用戶域名所需的連接領域,連接到服務器,保持連接活着等等等等

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 
    ... 
} 

最後,連接類有警報指標分析。警報標準基本上是某種正則表達式。這些目前只是「擴展」(只是一個字符串),但我要構建簡單的表達式,這些表達式將隨時隨地構建。它看起來像這樣

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

與另一個子

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%" 
    ... 
} 

現在我的問題:你認爲什麼是保存這種結構的最佳方式?我是.NET新手,所以我不確定:使用應用程序設置?或者使用XML序列化器對此結構進行序列化/反序列化?或者寫一些包裝類,然後使用XML序列化程序?或者也許還有其他方法?

感謝您的幫助!

+2

在我看來,最好的辦法是創建自己的自定義配置節http://msdn.microsoft.com/en-us/library/2tw134k3.aspx –

+0

實際上取決於在場景中。我不同意Yevgeniy使用配置部分,因爲它聽起來像這可能是一個「公共用戶設置」...(我知道你可能只有一個用戶公關安裝,但仍然)...序列化通常是一個體面的方法海事組織。 – Jens

回答

1

我建議不使用的applicationSettings。恕我直言,ApplicationsSettings用於存儲和重新加載用戶界面狀態,例如特定用戶在文本編輯器中最近使用過的10個文件或「不再顯示此消息」!

如果您正在考慮加載/保存場景,我建議您使用創建一個類來存儲保存應用程序狀態所需的所有字段不要直接存儲你的一些邏輯類與序列化它會弄亂的東西,並減少您的可維護性。

通過使用一個保存項目,您可以將所有加載/存儲操作集中在一個位置。

所以......

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; 
    } 

} 
+0

這就是我故意要做的。現在,我編寫了一些包裝類,並將它們序列化並保存並加載數據結構。工作正常。 – letmejustfixthat

0

對於我來說,對於目的,你應該:

  • 類(here是它在我的博客一個偉大的文章),用於處理設置。
  • 使用二進制或XML序列化 - 因爲它很容易和可定製
  • 將設置保存到通用路徑,我已經回答了where to store it
+0

感謝您與用戶的文件夾後面!正常工作 – letmejustfixthat