Skip to content

Instantly share code, notes, and snippets.

@FransBouma
Last active December 16, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FransBouma/5362167 to your computer and use it in GitHub Desktop.
Save FransBouma/5362167 to your computer and use it in GitHub Desktop.
100% test coverage, yet a bug. Can you spot it? Don't have too much confidence in 100% test coverage. For some reason the code below can return an old value for a field, even if the new value is read back from the DB.
protected object GetValue(int fieldIndex, bool returnDefaultIfNull)
{
if(_fields == null)
{
return null;
}
if(_fields.State == EntityState.Deleted)
{
throw new ORMEntityIsDeletedException("This entity is deleted from the database and can't be used in logic.", this);
}
var toReturn = _fields.GetCurrentValue(fieldIndex);
var fieldInfo = _fields.GetFieldInfo(fieldIndex);
if(_performsPersistenceLogic)
{
CheckForRefetch();
}
else
{
if((_fields.State == EntityState.OutOfSync) && !fieldInfo.IsPrimaryKey)
{
throw new ORMEntityOutOfSyncException("The entity is out of sync with its data in the database. Refetch this entity before using this in-memory instance.", this);
}
}
// check if the field is set to a value, if that's required.
if(MakeInvalidFieldReadsFatal && (_isNew && !_fields.GetIsChanged(fieldIndex) && toReturn == null))
{
// not set to a value, illegal.
throw new ORMInvalidFieldReadException(string.Format("The field '{0}' at index {1} isn't set to a value yet, so reading its value leads to invalid results. ",
fieldInfo.Name, fieldIndex));
}
object valueToReturn = null;
bool cancel;
OnGetValue(fieldIndex, out cancel);
if(!cancel)
{
valueToReturn = toReturn;
}
bool authorizerResult = OnCanGetFieldValue(fieldIndex);
if(!authorizerResult)
{
// not allowed. Set to null
valueToReturn = null;
}
if((valueToReturn == null) && returnDefaultIfNull)
{
ITypeDefaultValue providerToUse = _typeDefaultValueProvider ?? CreateTypeDefaultValueProvider();
if(providerToUse != null)
{
valueToReturn = providerToUse.DefaultValue(fieldInfo.DataType);
}
}
OnGetValueComplete(fieldIndex);
if(authorizerResult)
{
OnAuditEntityFieldGet(fieldIndex);
}
PostProcessValueToGet(fieldInfo, ref valueToReturn);
return valueToReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment