P
Peter Morris
Hi all
Let's say I am creating a model to represent classes and properties. In
addition to this I need instances of classes and values for those
properties.
KEY:
[ClassName]
(AssociationEndName)
A classifier has many properties
[Classifier] (Classifier) 1-----* (Properties) [Property]
A classifier has many instances
[Classifier] (Classifier) 1-----* (Instances) [Instance]
An instance has many property values
[Instance] (Instance) 1----* (PropertyValues) [PropertyValue]
A Property has many PropertyValues
[Property] (Property) 1----* (PropertyValues) [PropertyValue]
I hope that's clear
var personClassifier = new Classifier();
var firstNameProperty = new Property();
personClassifier.Properties.Add(firstNameProperty);
At this point I want to do something like this
var personInstance = Instance.FromClassifier(personClassifier);
the implementation would look something like this
public static Instance FromClassifier(Classifier classifier)
{
var result = new Instance();
result.Classifier = classifier;
foreach (var property in classifier.Properties)
{
var propertyValue = new PropertyValue();
result.PropertyValues.Add(propertyValue);
propertyValue.Property = property;
}
}
The above is a simple example. The fact is I have a pre-defined set of
property types. So descended from Property I might have
StringProperty, NumericProperty, BooleanProperty, etc
and a corresponding PropertyValue class for each
StringPropertyValue, NumericPropertyValue, BooleanPropertyValue, etc
To achieve the creation of the correct PropertyValue descendant I added an
abstract method to Property
public abstract PropertyValue CreatePropertyValue(Property property);
and implement in each descendant
public override PropertyValue CreatePropertyValue(Property property)
{
var result = new BooleanPropertyValue();
result.StronglyTypedReference = (BooleanProperty)property;
return result;
}
Now onto the question
To me this seems logical, but a little messy. I don't like the fact that
the Property class creates the instance of the PropertyValue. What I would
expect to write in code is something like
var property = BooleanPropertyValue.FromProperty(booleanProperty);
but obviously in this case I can't hard-code the class type into my foreach
loop. So it seems I am stuck with the Property being responsible for
creating the correct PropertyValue instances. Or do I have another option?
Let's say I am creating a model to represent classes and properties. In
addition to this I need instances of classes and values for those
properties.
KEY:
[ClassName]
(AssociationEndName)
A classifier has many properties
[Classifier] (Classifier) 1-----* (Properties) [Property]
A classifier has many instances
[Classifier] (Classifier) 1-----* (Instances) [Instance]
An instance has many property values
[Instance] (Instance) 1----* (PropertyValues) [PropertyValue]
A Property has many PropertyValues
[Property] (Property) 1----* (PropertyValues) [PropertyValue]
I hope that's clear
var personClassifier = new Classifier();
var firstNameProperty = new Property();
personClassifier.Properties.Add(firstNameProperty);
At this point I want to do something like this
var personInstance = Instance.FromClassifier(personClassifier);
the implementation would look something like this
public static Instance FromClassifier(Classifier classifier)
{
var result = new Instance();
result.Classifier = classifier;
foreach (var property in classifier.Properties)
{
var propertyValue = new PropertyValue();
result.PropertyValues.Add(propertyValue);
propertyValue.Property = property;
}
}
The above is a simple example. The fact is I have a pre-defined set of
property types. So descended from Property I might have
StringProperty, NumericProperty, BooleanProperty, etc
and a corresponding PropertyValue class for each
StringPropertyValue, NumericPropertyValue, BooleanPropertyValue, etc
To achieve the creation of the correct PropertyValue descendant I added an
abstract method to Property
public abstract PropertyValue CreatePropertyValue(Property property);
and implement in each descendant
public override PropertyValue CreatePropertyValue(Property property)
{
var result = new BooleanPropertyValue();
result.StronglyTypedReference = (BooleanProperty)property;
return result;
}
Now onto the question
To me this seems logical, but a little messy. I don't like the fact that
the Property class creates the instance of the PropertyValue. What I would
expect to write in code is something like
var property = BooleanPropertyValue.FromProperty(booleanProperty);
but obviously in this case I can't hard-code the class type into my foreach
loop. So it seems I am stuck with the Property being responsible for
creating the correct PropertyValue instances. Or do I have another option?