Equivalent of ConvertNulledReferenceTypesToDefaultValue = false on a per-column basis

Posts   
 
    
pdonovan
User
Posts: 21
Joined: 06-Mar-2012
# Posted on: 14-Dec-2022 08:35:27   

As per the subject line, we have a single column in a single entity that we require to return null (not string.Empty) if the database column is actually null. Setting the Project setting ConvertNulledReferenceTypesToDefaultValue to false affects many columns in the project, so we have to fix all the code that uses them to check for null first.

Is there something we can do with a partial class on the Entity, or does it go further, like into the PersistanceInfoProvider? Perhaps a custom attribute on the column?

edit: So, we effectively want to change the true in this code to false:

        public virtual System.String Key
        {
            get { return (System.String)GetValue((int)ReportAccessFieldIndex.Key, true); }
            set { SetValue((int)ReportAccessFieldIndex.Key, value); }
        }

which we could do by hand after generating source code, but that's nasty. What clever tricks am I missing?

Cheers,

Paul

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39738
Joined: 17-Aug-2003
# Posted on: 14-Dec-2022 11:02:56   

In a partial class of the generated entity class, override this method:

/// <summary>
/// Post-processes the value to return from GetValue. Override this method to be able to post-process any value to return from an entity field's property.
/// </summary>
/// <param name="fieldInfoOfFieldToGet">The field info of field to get.</param>
/// <param name="valueToReturn">The value to return.</param>
/// <remarks>
/// It's recommended that you don't change the type of the value passed in. If you want to change the type, use a type-converter.
/// Use the IFieldInfo specified to obtain values and other info for the field through the IEntityFieldsCore interface implemented on the
/// Fields object
/// </remarks>
protected virtual void PostProcessValueToGet(IFieldInfo fieldInfoOfFieldToGet, ref object valueToReturn)

In your override check the fieldInfoOfFieldToGet what field it's called for and if it's the field 'Key', set valueToReturn to null, otherwise do nothing

Frans Bouma | Lead developer LLBLGen Pro
pdonovan
User
Posts: 21
Joined: 06-Mar-2012
# Posted on: 18-Dec-2022 23:32:37   

Thanks, that works well.