Nested properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Corrado Labinaz

    #1

    Nested properties

    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.D atabase.Server. IPAddress
    instead of
    configuration.D atabase_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


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Nested properties

    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.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Corrado Labinaz @ngi.it>" <corradolab<n o-s-pam> wrote in message
    news:e7LGsa4FGH A.3900@TK2MSFTN GP10.phx.gbl...[color=blue]
    > 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.D atabase.Server. IPAddress
    > instead of
    > configuration.D atabase_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
    >[/color]


    Comment

    Working...