Design Question: The Ubiquitous Property

time to read 2 min | 294 words

I'm currently refactoring SchemaEditor (NHibernate Query Analyzer Hbm.Xml Editor, if you prefer ;-) ), and I encounter a strange case in designing the API. I want to be able to set a field without caring what is the backing store, whatever it is an array or a regular field. The problem is that sometimes when you are assigning an array to a field which is an array, the whole array should be replaced.

It should look like the following;

public object Value
{
   get { return val; }
   set 
   {
       if(valType==value.GetType())
            val = value;
       else if (valType.IsArray && valType.GetElementType()==value.GetType())
            AddToArray(val,value);
       else
           throw new InvalidTypeException();
    }
}

This just strike me as ugly, no matter the reasons for this (trasperant data manupulation without caring for the backing store).

Any suggestions?