2011-08-26 21 views
9

Kafam kesinlikle çok kötü çarpıyor! Bunu daha önce de yaptım ama “derinlemesine” ya da karmaşık olarak değil, bunu yapmak için farklı yollar denedim ama hepsi başarısız oldu.App.config için Özel Yapılandırma - bölümlerin koleksiyonları?

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

Yani alt unsurların bir koleksiyon (Bileşenleri topluluğudur sahip her profil ile Profil koleksiyonu, daha sonra Bileşen bir edilir:

Yani, bu ben app.config istediğiniz özel XML eleman)

Ancak şu anda tüm sahip olduğu çoklu profiller için HARİÇ. Ben sorunu görüyorum ama nasıl "düzeltmek" emin değilim.

Kodu:

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

: Kendinin bu takip ederek çalışma almak gerekir. koduyla sorun üzerinde http://www.sadev.co.za/content/appconfig-transformations-community-shines-where-microsoft-drops-ball –

+0

Herhangi bilgi: Hemen hemen istediğini yapar? – Jodrell

cevap

0

Sen bir ekstra düzeyi ihtiyaç dışında doğru uygulanması ediyoruz. Bir ConfigurationSection'dan bir ConfigurationElement öğesine Profili değiştirin, ardından bir Profil öğeleri koleksiyonu içeren ConfigurationSection Profilleri oluşturun.

Bu bir kraliyet ağrı olmadan yapılandırma bölümleri ayıklama, otomatik test için mükemmel bir durumdur.

+0

Şimdiye kadar yanıtlarınız için teşekkür ederiz. Cheetah uzantısı ilginç görünüyor. –

+0

woops .. çok hızlı bir şekilde basıldı. ssamuel - Ben yaklaşımınızı daha önce denedim ama IConfigurationSectionHandler uygulamak gerektiğini söyleyerek istisnalar dışında hiçbir yerde var inanıyorum. Bunu tekrar yapabilir miyim göreceğim./ –

+0

Ahh, ne FW sürümü kullanıyorsanız: - onun sadece hızlı/seri XML bir nesnedeki seriyi kaldırmak ve dürüst olmak kullanmak Ama eğer acaba? IConfigurationSectionHandler, 2.0'dan sonra kullanımdan kaldırıldı. Hala bakıyorsanız, [bunu deneyin] (http://msdn.microsoft.com/en-us/library/2tw134k3.aspx). Serialize-to-XML daha hızlı ve daha sezgiseldir. Yapılandırmaya yazı yazmak daha güzel. Bakım zorluğundan, sysadmin veya geliştiriciden ayrılmak istediğiniz yere bağlıdır. – ssamuel

7

tek bir seviye daha yüksek yapılandırma bölümünü taşımak gerekir.

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

Oluşturduğum bir bölüm. Size bu hafta serbest bırakıldı VS için Yavaş Cheetah uzantısı bakma neden farklı ortamlar için farklı profillere sahip çalışıyoruz görünüyor

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; 
    } 
} 
İlgili konular