2011-08-26 20 views
9

La mia testa è assolutamente martellante! L'ho già fatto prima ma non così "in profondità" o complesso come questo e ho provato diversi modi per farlo accadere, ma tutti hanno fallito.Configurazione personalizzata per app.config - raccolte di sezioni?

Quindi, questo è l'XML personalizzato che voglio nel 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> 

Quindi una collezione di profilo con ogni profilo avere una collezione di elementi secondari (Components è un insieme, allora componente è un elemento)

Tuttavia attualmente ho tutto ciò che EXCEPT per i profili multipli. Mi sembra di vedere il problema ma non sono sicuro di come "aggiustarlo".

Codice:

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

Sembra che si sta cercando di avere diversi profili per diversi ambienti, perché non si guarda l'estensione Cheetah lento per il VS che è stato pubblicato questa settimana. Praticamente fa quello che vuoi: http://www.sadev.co.za/content/appconfig-transformations-community-shines-where-microsoft-drops-ball –

+0

Qualche informazione sul problema con il codice? – Jodrell

risposta

0

Stai attuare correttamente tranne è necessario un ulteriore livello. Cambiare il profilo da ConfigurationSection a ConfigurationElement, quindi creare un profilo ConfigurationSection che contenga una raccolta di elementi del profilo.

Questo è un caso eccellente per test automatici, il debug di sezioni di configurazione senza un dolore reale.

+0

Grazie a tutti per le vostre risposte finora. L'estensione del ghepardo sembra interessante. –

+0

woops .. premuto invio troppo veloce. ssamuel - Credo di aver provato il tuo approccio in precedenza, ma non ho ottenuto dove, a parte le eccezioni che vengono lanciate, dico che è necessario implementare IConfigurationSectionHandler. Vedrò se posso farlo di nuovo. Ma mi chiedo se è più veloce serializzare/deserializzare un oggetto in XML e usarlo per essere onesto: -/ –

+0

Ugh, quale versione del FW stai usando? IConfigurationSectionHandler è deprecato dopo 2.0. Se stai ancora cercando, [prova questo] (http://msdn.microsoft.com/en-us/library/2tw134k3.aspx). Serialize-to-XML è più veloce e più intuitivo. Scrivere in configurazione è più carino. Dipende da dove vuoi lasciare la difficoltà di manutenzione, dal sysadmin o dallo sviluppatore. – ssamuel

7

È necessario spostare la sezione di configurazione di un livello più in alto.

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

Ecco una sezione che ho creato. Si dovrebbe essere in grado di ottenere il vostro lavoro seguendo questo:

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