2011-08-26 19 views
9

私の頭は絶対にひどく打たれています!私は前にこれをやったことがありますが、これほど深くないか複雑ではなく、これを実現するためにさまざまな方法を試しましたが、すべて失敗しました。app.configのカスタム設定 - セクションのコレクション?

だから、これは私がApp.configファイルにするカスタムXMLです:

<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のSlow Cheetah拡張機能をご覧ください。あなたは何をしたいのですか?http://www.sadev.co.za/content/appconfig-transformations-community-shines-where-microsoft-drops-ball –

+0

コードの問題に関する情報はありますか? – Jodrell

答えて

0

あなたは1つの余分のレベルを必要とする以外、正しく実装しようとしています。プロファイルをConfigurationSectionからConfigurationElementに変更してから、プロファイル項目のコレクションを含むConfigurationSectionプロファイルを作成します。

これは、自動化されたテストのための優れたケースであり、設定セクションをデバッグすることは王冠の痛みではありません。

+0

これまでの返事に感謝します。 Cheetahの拡張機能は面白そうです。 –

+0

woops ..押されすぎてしまった。 ssamuel - 私は前にあなたのアプローチを試みたが、IConfigurationSectionHandlerを実装する必要があるという例外から離れてどこにも到達していないと思う。私はこれをやり直すことができるかどうかを見るでしょう。しかし、オブジェクトをXMLにシリアライズ/逆シリアル化して正直に使うのが速いのだろうか? -/ –

+0

あなたは、どのバージョンのFWを使用していますか? IConfigurationSectionHandlerは2.0以降では非推奨です。あなたがまだ見ているなら、[これを試してください](http://msdn.microsoft.com/en-us/library/2tw134k3.aspx)。 Serialize-to-XMLはより高速で直感的です。構成への書き込みはよりきれいです。システム管理者または開発者に、メンテナンスの難しさを残す場所によって異なります。 – ssamuel

7

設定セクションを1つ上に移動する必要があります。

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

ここには私が作成したセクションがあります。

関連する問題