Nested properties

  • Thread starter Thread starter Corrado Labinaz
  • Start date Start date
C

Corrado Labinaz

Hi everyone,

I've a Configuration class with almost 200 read only properties.
This class makes available configuration data to my whole application.

I would like to group realted properties together.
The idea is to access the properties this way
configuration.Database.Server.IPAddress
instead of
configuration.Database_Server_IPAddress

The above is only a sample but should get you the idea.

At first glance I should create other classes (in the example above I would
need at least the Database and Server classes) and nest them inside my
Configuration class.
But this approach requires a lot of work.
1. I have to create dozen of classes
2. It's difficult to make the classes read-only: they would be read only
also for the container class!
3. The point 2 above can be addresses at construction time: I could create
constructors which setup the read only properties, but again it's a lot of
work to do. If my nested properties has a 2 level depth, I should assign the
values at least 3 times (and that's 600 assignments for 200 properties!)...

Any idea?

Kind regards,
Corrado
 
Corrado,

With .NET 2.0, you can create properties where the get and set accessors
with different access levels. So, you could set your get property as
public, and your set as internal, like so:

public int MyValue
{
get
{
return myValue;
}
internal set
{
myValue = value;
}
}

You would have the set be internal so you can set from within your
assembly.


You would still have to create separate classes to create groupings, but
that's a good thing. After all, you are trying to encapsulate
functionality.

Hope this helps.
 
Back
Top