[CSLA 3.5] [BUG?] LoadProperty with null values

[CSLA 3.5] [BUG?] LoadProperty with null values

Old forum URL: forums.lhotka.net/forums/t/4570.aspx


Skafa posted on Thursday, March 27, 2008

I tried to load a property using LoadProperty, but I get a NullReferenceException when the new value is null.

the property I tried to load is of type Nullable<Guid>.

I narrowed it down to the following code in FieldDataManager:

    internal IFieldData LoadFieldData(IPropertyInfo prop, object value)
    {
      value = Utilities.CoerceValue(prop.Type, value.GetType(), null, value);
      var field = GetOrCreateFieldData(prop);
      field.Value = value;
      field.MarkClean();
      return field;
    }

ofcourse this will fail if value is null (value.GetType() fails)
Should a null value be coerced?

Edit: also, I should note that I'm using the non-generic version of LoadProperty (in this specific case I cannot use the generic ones).

Adam replied on Thursday, May 14, 2009

I too have been haivng this problem and after some searching have found a bug in the csla framework in Utilities.cs in CoerceValue ln:181 where

if ((desiredType.IsPrimitive || desiredType.Equals(typeof(decimal))) &&
          valueType.Equals(typeof(string)) && string.IsNullOrEmpty((string)value))
        value = 0;

looks like it should be
if ((desiredType.IsPrimitive || desiredType.Equals(typeof(decimal))) &&
           string.IsNullOrEmpty(value.ToString()))
        value = 0;

as currently it's type checking for a string but not all primatives are strings

but to overcome this I have adapted my templates to use
LoadProperty<long, long>(myIDProperty , data.myID.GetValueOrDefault(...default value here...));
from this post

Hope this helps anyone in the same situation :)

Copyright (c) Marimer LLC