Dynamic Property groups

  • Thread starter Thread starter Codex Twin
  • Start date Start date
C

Codex Twin

Hi

I am designing a class (Q) which will contain provision for dynamic complex
properties.

For example, these properties could be any or all of the following complex
properties:
a) TeamMembers
b) Pets

Furthermore, the above properties may decompose further into sub-properties:

TeamMembers.MemberName
TeamMembers.Age
TeamMembers.Sex

Pets.Species
Pets.PetName
Pets.OwnerName
Pets.Age

The properties and their sub-properies will be defined and stored in
database.
What is the best way to design the Q class so that it can easily access and
assign values to the subproperties?

I've been considering creating a sub-property class called SubProp with the
single member:
Value (type object)
Multiple instances of SubProp can be contained in an indexer property of the
Parent property called ParentProp.
And then multiple instances of ParentProp can be stashed into an indexer
property called CustomPropGroup in the class Q.

The end result is horribly unwieldly though:
For example, the Pets properties shown above would be
Q.CustomPropGroup[2].SubProp[0].Value [Q.Pets.Species]
Q.CustomPropGroup[2].SubProp[1].value [Q.Pets.PetName]
Q.CustomPropGroup[2].SubProp[2].value [Q.Pets.OwnerName]
Q.CustomPropGroup[2].SubProp[3].value [Q.Pets.Age]

It might more intuitive if I overload the indexers to take a string value
for the property name:
Q.CustomPropGroup["Pets"].SubProp["Species"].Value = "Chimpanzee";
Q.CustomPropGroup["Pets"].SubProp["PetName"].Value = "Bubbles";
Q.CustomPropGroup["Pets"].SubProp["OwnerName"].Value = "Michael Jackson";
Q.CustomPropGroup["Pets"].SubProp["Species"].Value = "2";

But as you can see, its still very unwieldly.
Is there a better, more intuitive way or a design pattern solution to this
problem?

Thanks
 
Codex said:
Hi

I am designing a class (Q) which will contain provision for dynamic complex
properties.

For example, these properties could be any or all of the following complex
properties:
a) TeamMembers
b) Pets

Furthermore, the above properties may decompose further into sub-properties:

TeamMembers.MemberName
TeamMembers.Age
TeamMembers.Sex

Pets.Species
Pets.PetName
Pets.OwnerName
Pets.Age

The properties and their sub-properies will be defined and stored in
database.
What is the best way to design the Q class so that it can easily access and
assign values to the subproperties?

I've been considering creating a sub-property class called SubProp with the
single member:
Value (type object)
Multiple instances of SubProp can be contained in an indexer property of the
Parent property called ParentProp.
And then multiple instances of ParentProp can be stashed into an indexer
property called CustomPropGroup in the class Q.

The end result is horribly unwieldly though:
For example, the Pets properties shown above would be
Q.CustomPropGroup[2].SubProp[0].Value [Q.Pets.Species]
Q.CustomPropGroup[2].SubProp[1].value [Q.Pets.PetName]
Q.CustomPropGroup[2].SubProp[2].value [Q.Pets.OwnerName]
Q.CustomPropGroup[2].SubProp[3].value [Q.Pets.Age]

It might more intuitive if I overload the indexers to take a string value
for the property name:
Q.CustomPropGroup["Pets"].SubProp["Species"].Value = "Chimpanzee";
Q.CustomPropGroup["Pets"].SubProp["PetName"].Value = "Bubbles";
Q.CustomPropGroup["Pets"].SubProp["OwnerName"].Value = "Michael Jackson";
Q.CustomPropGroup["Pets"].SubProp["Species"].Value = "2";

But as you can see, its still very unwieldly.
Is there a better, more intuitive way or a design pattern solution to this
problem?
I remember myself doing similar things about 4 years ago. As i remember i was doing the following:
You declare a NameValue collection and store your properties as delimited names, eg


CustomGroup = new NameValueCollection();

Q.CustomGroup.Add("Pets.Species", "Chimpanzee")
Q.CustomGroup.Add("Pets.PetName", "Bubbles")

etc...

And then you can read all properties, parsing the name, eg

foreach(string key in Q.CustomGroup.AllKeys) {
string[] s = Split(key);
<do something>
}

where s[0] might be "Pets" and s[1] would be "Species" or "PetName"

That's how you access those properties/subproperties later.

I guess that's the easiest way to build a dynamic set of properties.

Hope it helps,
Andrey aka MuZZy
 
MuZZy said:
Codex said:
Hi

I am designing a class (Q) which will contain provision for dynamic complex
properties.

For example, these properties could be any or all of the following complex
properties:
a) TeamMembers
b) Pets

Furthermore, the above properties may decompose further into sub-properties:

TeamMembers.MemberName
TeamMembers.Age
TeamMembers.Sex

Pets.Species
Pets.PetName
Pets.OwnerName
Pets.Age

The properties and their sub-properies will be defined and stored in
database.
What is the best way to design the Q class so that it can easily access and
assign values to the subproperties?

I've been considering creating a sub-property class called SubProp with the
single member:
Value (type object)
Multiple instances of SubProp can be contained in an indexer property of the
Parent property called ParentProp.
And then multiple instances of ParentProp can be stashed into an indexer
property called CustomPropGroup in the class Q.

The end result is horribly unwieldly though:
For example, the Pets properties shown above would be
Q.CustomPropGroup[2].SubProp[0].Value [Q.Pets.Species]
Q.CustomPropGroup[2].SubProp[1].value [Q.Pets.PetName]
Q.CustomPropGroup[2].SubProp[2].value [Q.Pets.OwnerName]
Q.CustomPropGroup[2].SubProp[3].value [Q.Pets.Age]

It might more intuitive if I overload the indexers to take a string value
for the property name:
Q.CustomPropGroup["Pets"].SubProp["Species"].Value = "Chimpanzee";
Q.CustomPropGroup["Pets"].SubProp["PetName"].Value = "Bubbles";
Q.CustomPropGroup["Pets"].SubProp["OwnerName"].Value = "Michael Jackson";
Q.CustomPropGroup["Pets"].SubProp["Species"].Value = "2";

But as you can see, its still very unwieldly.
Is there a better, more intuitive way or a design pattern solution to this
problem?
I remember myself doing similar things about 4 years ago. As i remember i was doing the following:
You declare a NameValue collection and store your properties as delimited names, eg


CustomGroup = new NameValueCollection();

Q.CustomGroup.Add("Pets.Species", "Chimpanzee")
Q.CustomGroup.Add("Pets.PetName", "Bubbles")

etc...

And then you can read all properties, parsing the name, eg

foreach(string key in Q.CustomGroup.AllKeys) {
string[] s = Split(key);
<do something>
}

where s[0] might be "Pets" and s[1] would be "Species" or "PetName"

That's how you access those properties/subproperties later.

I guess that's the easiest way to build a dynamic set of properties.

Hope it helps,
Andrey aka MuZZy

Muzzy
Thanks a great deal, mate. I will use your recommendation.
cT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top