简体   繁体   English

CSLA.NET覆盖LoadProperty

[英]CSLA.NET Overriding LoadProperty

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. 我们希望根据角色掩盖业务对象中的某些信息,因为我们希望代码库在多个项目之间共享,所以我们希望在业务逻辑中而不是在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 . 我们的想法是在CSLA中override LoadProperty方法,以便我们可以更改一次值,而不是将其设置为未屏蔽的值,然后在DataPortal_Fetch之后进行屏蔽。 The issue is that the virtual LoadProperty method is never fired, see code below: 问题是虚拟LoadProperty方法从不触发,请参见下面的代码:

    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 : 以下是BusinessBase中的两种方法,但只有一种是virtual

protected void LoadProperty<P>(PropertyInfo<P> propertyInfo, P newValue);
protected virtual void LoadProperty(IPropertyInfo propertyInfo, object newValue);

The issue is that the virtual LoadProperty method is never fired 问题是虚拟LoadProperty方法从不触发

I'm assuming your calls to LoadProperty are type specific (your newValue is being passed as the actual type, not an object type). 我假设您对LoadProperty的调用是特定于类型的(您的newValue将作为实际类型而不是object类型传递)。

The virtual non-generic LoadProperty is less type specific than the generic LoadProperty because the parameters types are IPropertyInfo and object . 由于参数类型为IPropertyInfoobject因此虚拟非通用LoadProperty的类型特定性低于通用LoadProperty。 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. 因此,除非您将newValue作为object类型传递,否则将object LoadProperty<P>(PropertyInfo<P> propertyInfo, P newValue)

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. 我将使用不同的名称创建自己的方法进行屏蔽,然后调用基本LoadProperty方法。 But if you really want to "override" LoadProperty, you could use new to hide the base method as an alternative: 但是,如果您真的想“覆盖” LoadProperty,则可以使用new来隐藏基本方法作为替代方法:

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. 请参考此答案,以了解隐藏而不是覆盖的含义对您来说可能是,也可能不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM