Strings irk me.. They're awkward to maintain conventions with especially when publicly editable and they are unpleasant to deal with when calling API's.
And they cause bugs...
In the Umbraco CMS at present it possible to add two distinct properties with the following aliases to a single doctype.
and
When querying IPublishedContent using:
GetPropertyValue(this IPublishedContent content, string alias, bool recurse)
The alias is case insensitive which produces ambiguity.
When using the Content API, those aliases are case sensitive. That's poor enforcement of convention/style.
IContentBase.SetValue(string propertyAlias, object value);
Personally I would abstract those property aliases away; the developer/user shouldn't have to care about them; make them invariant upper-case or hash them and that ambiguity is gone. Deep in the API you can have a single method that creates those hashes that can be called in turn from methods such as the one above.
With greater convention the second method could be rewritten as.
public static void SetValue<T>(this IContentBase contentBase, Expression<Func<T>> propertyLambda)
{
MemberExpression expression = propertyLambda.Body as MemberExpression;
if (expression == null)
{
throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
}
PropertyInfo property = expression.Member as PropertyInfo;
if (property != null)
{
MemberExpression member = (MemberExpression)expression.Expression;
ConstantExpression constant = (ConstantExpression)member.Expression;
object fieldInfoValue = ((FieldInfo)member.Member).GetValue(constant.Value);
object value = property.GetValue(fieldInfoValue, null);
// Use whatever hashing method we use.
string name = property.Name.ToSafeAlias();
// Call string,string method probably internal and hidden away.
contentBase.SetValue(name, value.ToString());
}
}
With a signature as follows.
IContent.SetValue(() => object.Property);
Lambda expressions are ubiquitous and I think it would be unfair to suggest that developers wouldn't understand the syntax. We use similar expressions in MVC for helpers anyway. Plus, all the difficult stuff like MemberExpressions are tucked away in core.
Would it be feasible to change the API like this in V8? I'd love to help contribute.