2011-08-26 55 views
9

我的頭絕對糟糕!我之前做過這些,但沒有像這樣「深入」或複雜,我嘗試了不同的方法來實現這一點,但都失敗了。app.config的自定義配置 - 部分的集合?

所以,這是自定義XML我想在App.config:

<Profiles> <!--Collection--> 
<Profile Name="Live"> 
    <Components> 
    <Component Name="Portal" Type="Web" /> 
    <Component Name="Comms" Type="Web" /> 
    <Component Name="Scheduler" Type="WindowsService" ServiceName="LiveScheduler" /> 
    </Components> 
    <Databases> 
    <Database Name="Main" Connection="Data Source=.\SQL2008" /> 
    <Database Name="Staging" Connection="Data Source=SomeSite.co.uk" /> 
    </Databases> 
</Profile> 
<Profile Name="Test"> 
    <Components> 
    <Component Name="Portal" Type="Web" /> 
    <Component Name="Comms" Type="Web" /> 
    <Component Name="Scheduler" Type="WindowsService" ServiceName="TestScheduler" /> 
    </Components> 
    <Databases> 
    <Database Name="Main" Connection="Data Source=.\SQL2008" /> 
    <Database Name="Staging" Connection="Data Source=Internal" /> 
    </Databases> 
</Profile> 
</Profiles> 

因此,與每個配置文件有子元素的集合(組件是一個集合檔案的收集,那麼組件是元素)

但是我目前除了多個配置文件外都有其他所有的功能。我有點看到問題,但不知道如何「修復」它。

代碼:

public class Profile : ConfigurationSection 
{ 
    [ConfigurationProperty("Name", IsRequired=true)] 
    public string Name 
    { 
     get 
     { 
      return base["Name"] as string; 
     } 
      set 
      { 
       base["Name"] = value; 
      } 
     } 

     [ConfigurationProperty("Components")] 
     public ComponentCollection Components 
     { 
      get { return ((ComponentCollection)(base["Components"])); } 
     } 

     [ConfigurationProperty("Databases")] 
     public DatabaseCollection Databases 
     { 
      get 
      { 
       return ((DatabaseCollection)(base["Databases"])); 
      } 
     } 
    } 

    [ConfigurationCollection(typeof(Component), AddItemName="Component")] 
    public class ComponentCollection : ConfigurationElementCollection 
    {   
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new Component(); 
     } 

     protected override object GetElementKey(ConfigurationElement element) 
     { 
      return ((Component)(element)).Name; 
     } 

     public Component this[int idx] 
     { 
      get 
      { 
       return base.BaseGet(idx) as Component; 
      } 
      set 
      { 
       if (base.BaseGet(idx) != null) 
       { 
        base.BaseRemoveAt(idx); 
       } 
       this.BaseAdd(idx, value); 
      } 
     } 

     public Component this[string key] 
     { 
      get 
      { 
       return base.BaseGet(key) as Component; 
      } 
      set 
      { 
       if (base.BaseGet(key) != null) 
       { 
        base.BaseRemove(key); 
       } 
       this.BaseAdd(this.Count, value); 
      } 
     } 
    } 

    public class Component : ConfigurationElement 
    { 
     [ConfigurationProperty("Type", IsRequired = true)] 
     public string Type 
     { 
      get 
      { 
       return this["Type"] as string; 
      } 
      set 
      { 
       this["Type"] = value; 
      } 
     } 

     [ConfigurationProperty("Name", IsRequired = true, IsKey = true)] 
     public string Name 
     { 
      get 
      { 
       return this["Name"] as string; 
      } 
      set 
      { 
       this["Name"] = value; 
      } 
     } 

     [ConfigurationProperty("ServiceName", IsRequired = false)] 
     public string ServiceName 
     { 
      get 
      { 
       return this["ServiceName"] as string; 
      } 
      set 
      { 
       this["ServiceName"] = value; 
      } 
     } 
    } 

    [ConfigurationCollection(typeof(Database), AddItemName = "Database")] 
    public class DatabaseCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new Database(); 
     } 

     protected override object GetElementKey(ConfigurationElement element) 
     { 
      return ((Database)(element)).Name; 
     } 


     public Database this[int idx] 
     { 
      get 
      { 
       return base.BaseGet(idx) as Database; 
      } 
      set 
      { 
       if (base.BaseGet(idx) != null) 
       { 
        base.BaseRemoveAt(idx); 
       } 
       this.BaseAdd(idx, value); 
      } 
     } 

     public Database this[string key] 
     { 
      get 
      { 
       return base.BaseGet(key) as Database; 
      } 
      set 
      { 
       if (base.BaseGet(key) != null) 
       { 
        base.BaseRemove(key);; 
       } 

       this.BaseAdd(this.Count, value); 
      } 
     } 
    } 

    public class Database : ConfigurationElement 
    { 
     [ConfigurationProperty("Name", IsKey = true, IsRequired = true)] 
     public string Name 
     { 
      get 
      { 
       return this["Name"] as string; 
      } 
      set 
      { 
       this["Name"] = value; 
      } 
     } 

     [ConfigurationProperty("Connection", IsKey = false, IsRequired = true)] 
     public string Connection 
     { 
      get 
      { 
       return this["Connection"] as string; 
      } 
      set 
      { 
       this["Connection"] = value; 
      } 
     } 
    } 
+1

似乎你想有不同的環境,你爲什麼不看慢獵豹擴展VS這是本週公佈的不同的配置文件。幾乎沒有你想要的:http://www.sadev.co.za/content/appconfig-transformations-community-shines-where-microsoft-drops-ball –

+0

關於代碼問題的任何信息? – Jodrell

回答

0

你正確地執行,除非你需要一個額外的水平。將配置文件從ConfigurationSection更改爲ConfigurationElement,然後製作包含配置文件項目集合的ConfigurationSection配置文件。

這是一個很好的自動測試案例,調試配置部分沒有是一個皇室的痛苦。

+0

謝謝你的回覆到目前爲止。 Cheetah擴展看起來很有趣。 –

+0

woops ..按下輸入得太快。 ssamuel - 我相信我之前嘗試過你的方法,但沒有除了拋出異常的地方說它需要實現IConfigurationSectionHandler。我會看看我能否再次做到這一點。但是我不知道它是否更快地將一個對象序列化或反序列化爲XML並且使用它是誠實的: -/ –

+0

呃,你使用哪個版本的FW? IConfigurationSectionHandler在2.0之後被棄用。如果你仍然在尋找,[試試這個](http://msdn.microsoft.com/en-us/library/2tw134k3.aspx)。序列化到XML更快,更直觀。寫入配置更漂亮。取決於您想要在系統管理員或開發人員身上留下維護難度的位置。 – ssamuel

7

您需要將配置部分移動一級。

public class Profiles : ConfigurationSection 
{ 
    [ConfigurationProperty("Profile")] 
    public ProfileCollection Profile 
    { 
     get 
     { 
      return this["profile"] as ProfileCollection; 
     } 
    } 
}  

這是我創建的部分。你應該能夠得到你的工作由以下這一點:

public class ImportConfiguration : ConfigurationSection 
{ 
    [ConfigurationProperty("importMap")] 
    public ImportMapElementCollection ImportMap 
    { 
     get 
     { 
      return this["importMap"] as ImportMapElementCollection; 
     } 
    } 
} 

public class ImportColumnMapElement : ConfigurationElement 
{ 
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)] 
    public string LocalName 
    { 
     get 
     { 
      return this["localName"] as string; 
     } 
     set 
     { 
      this["localName"] = value; 
     } 
    } 

    [ConfigurationProperty("sourceName", IsRequired = true)] 
    public string SourceName 
    { 
     get 
     { 
      return this["sourceName"] as string; 
     } 
     set 
     { 
      this["sourceName"] = value; 
     } 
    } 
} 

public class ImportMapElementCollection : ConfigurationElementCollection 
{ 
    public ImportColumnMapElement this[object key] 
    { 
     get 
     { 
      return base.BaseGet(key) as ImportColumnMapElement; 
     } 
    } 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get 
     { 
      return ConfigurationElementCollectionType.BasicMap; 
     } 
    } 

    protected override string ElementName 
    { 
     get 
     { 
      return "columnMap"; 
     } 
    } 

    protected override bool IsElementName(string elementName) 
    { 
     bool isName = false; 
     if (!String.IsNullOrEmpty(elementName)) 
      isName = elementName.Equals("columnMap"); 
     return isName; 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new ImportColumnMapElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((ImportColumnMapElement)element).LocalName; 
    } 
}