Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Nested ConfigurationElementCollections

Reply
Thread Tools

Nested ConfigurationElementCollections

 
 
Mark Olbert
Guest
Posts: n/a
 
      01-25-2006
I'd like to create a ConfigurationSection to handle something that looks like this:

<SiteSettings>
<site name="develop" smtp="mail.arcabama.com" host="localhost">
<mappings>
<membership name="a" column="cola" />
<membership name="b" column="colb" />
</mappings>
</site>
<site name="production" smtp="smtp.alentus.com" host="www.arcabama.com">
<mappings>
<membership name="a" column="cola" />
<membership name="b" column="colb" />
</mappings>
</site>
</SiteSettings>

I thought I could do this with the following classes (sorry about the length):

public class SiteSection : ConfigurationSection
{
[ ConfigurationProperty("", IsDefaultCollection = true) ]
public SiteCollection Sites
{
get { return (SiteCollection) base[""]; }
}
}

public class SiteCollection : ConfigurationElementCollection
{
public new SiteElement this[string name]
{
get
{
if( IndexOf(name) < 0 ) return null;

return (SiteElement) BaseGet(name);
}
}

public SiteElement this[int index]
{
get { return (SiteElement) BaseGet(index); }
}

public int IndexOf( string name )
{
name = name.ToLower();

for( int idx = 0; idx < base.Count; idx++ )
{
if( this[idx].Name.ToLower() == name )
return idx;
}

return -1;
}

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

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

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

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

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

[ ConfigurationProperty("smtp", DefaultValue = "mail.arcabama.com", IsRequired = true, IsKey = false) ]
public string SmtpServer
{
get { return (string) this["smtp"]; }
set { this["smtp"] = value; }
}

[ ConfigurationProperty("host", DefaultValue = "localhost", IsRequired = true, IsKey = false) ]
public string Host
{
get { return (string) this["host"]; }
set { this["host"] = value; }
}

[ ConfigurationProperty("mappings", IsDefaultCollection = false) ]
public MembershipCollection Mappings
{
get { return (MembershipCollection) base["mappings"]; }
}
}

public class MembershipCollection : ConfigurationElementCollection
{
public new MembershipElement this[string name]
{
get
{
if( IndexOf(name) < 0 ) return null;

return (MembershipElement) BaseGet(name);
}
}

public MembershipElement this[int index]
{
get { return (MembershipElement) BaseGet(index); }
}

public int IndexOf( string name )
{
name = name.ToLower();

for( int idx = 0; idx < base.Count; idx++ )
{
if( this[idx].Name.ToLower() == name )
return idx;
}
return -1;
}

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

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

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

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

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

[ ConfigurationProperty("column", DefaultValue = "", IsRequired = true, IsKey = false) ]
public string Column
{
get { return (string) this["column"]; }
set { this["column"] = value; }
}
}

But while I can watch the methods in MembershipCollection get called -- and see that the web.config entries corresponding to the
MembershipElements are being parsed -- the Mappings collection in SiteElement always ends up being empty.

How do I go about nesting one ConfigurationElementCollection within another?

- Mark
 
Reply With Quote
 
 
 
 
Luke Zhang [MSFT]
Guest
Posts: n/a
 
      01-25-2006
Hello Mark,

I got different results with same code, the Mappings collection in
SiteElement return correct result. Here is the code I used (I tested in a
winform application):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>



<configSections>

<section
name="SiteSettings"
type="WindowsApplication8.SiteSection,WindowsAppli cation8"
allowLocation="true"
allowDefinition="Everywhere" />


</configSections>


<SiteSettings>
<site name="develop" smtp="mail.arcabama.com" host="localhost">
<mappings>
<membership name="a" column="cola" />
<membership name="b" column="colb" />
</mappings>
</site>
<site name="production" smtp="smtp.alentus.com" host="www.arcabama.com">
<mappings>
<membership name="a" column="cola" />
<membership name="b" column="colb" />
</mappings>
</site>



</SiteSettings>


</configuration>



SiteSection configSection =
(SiteSection)System.Configuration.ConfigurationMan ager.GetSection("SiteSetti
ngs");

propertyGrid1.SelectedObject = configSection;

 
Reply With Quote
 
 
 
 
Mark Olbert
Guest
Posts: n/a
 
      01-25-2006
Interesting. But I'm using this as part of a website. Why would the same code work in a WinForm, but not in a website?

- Mark
 
Reply With Quote
 
Mark Olbert
Guest
Posts: n/a
 
      01-25-2006
Okay, problem solved (it was a boneheaded mistake on my end).

- Mark
 
Reply With Quote
 
Luke Zhang [MSFT]
Guest
Posts: n/a
 
      01-26-2006
Thank you for update. I am glad the problem was resolved.

Luke

 
Reply With Quote
 
=?Utf-8?B?Q3JhaWc=?=
Guest
Posts: n/a
 
      02-28-2006


"Mark Olbert" wrote:

> Okay, problem solved (it was a boneheaded mistake on my end).
>
> - Mark
>

 
Reply With Quote
 
=?Utf-8?B?Q3JhaWc=?=
Guest
Posts: n/a
 
      02-28-2006
Hi Mark

Any change you could post the code to SimpleSiteSetting class?

Thanks

"Mark Olbert" wrote:

> Okay, problem solved (it was a boneheaded mistake on my end).
>
> - Mark
>

 
Reply With Quote
 
Anderson Olympio Anderson Olympio is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-23-2011
Hi folk!

Just substitute SimpleSiteSetting by SiteElement and it works!

Kind Regards,
Anderson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments