0

We would like to mask some information in our business objects based on roles, seeing that our codebase is shared between multiple projects we would like to do this in the business logic instead of in the UI.

Our idea was to override the LoadProperty method within the CSLA so that we can change the value once instead of setting it to the unmasked value, then masking after DataPortal_Fetch. The issue is that the virtual LoadProperty method is never fired, see code below:

    protected override void LoadProperty(IPropertyInfo propertyInfo, object newValue)
    {
        //Do masking
        newValue = DoMask(newValue, maskAttribute);
        base.LoadProperty(propertyInfo, newValue);
    }

Below are the two methods within the BusinessBase, but only one is virtual:

protected void LoadProperty<P>(PropertyInfo<P> propertyInfo, P newValue);
protected virtual void LoadProperty(IPropertyInfo propertyInfo, object newValue);
Chrisjan Lodewyks
  • 1,162
  • 4
  • 13
  • 25

1 Answers1

0

The issue is that the virtual LoadProperty method is never fired

I'm assuming your calls to LoadProperty are type specific (your newValue is being passed as the actual type, not an object type).

The virtual non-generic LoadProperty is less type specific than the generic LoadProperty because the parameters types are IPropertyInfo and object. This method exists in the cases where the data type of the value cannot be determined until run time.

So unless you are passing newValue as an object type, LoadProperty<P>(PropertyInfo<P> propertyInfo, P newValue) would be called instead.

but only one is virtual

Since the method you actually want to override is not virtual, you will have to workaround that.

I would create my own method with a different name which does the masking and then call the base LoadProperty method. But if you really want to "override" LoadProperty, you could use new to hide the base method as an alternative:

protected new void LoadProperty<P>(PropertyInfo<P> propertyInfo, P newValue)
{
    //Do masking
    newValue = DoMask(newValue, maskAttribute);
    base.LoadProperty(propertyInfo, newValue);
}

Refer to this answer to see the implications of hiding instead of overriding which may or may not be an issue for you.

singularhum
  • 5,072
  • 2
  • 24
  • 32