Best options for BusinessRules on readonly properties

Best options for BusinessRules on readonly properties

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


g.beraudo@castsoftware.com posted on Tuesday, October 23, 2012

Hi all,

I would need to define some readonly properties on my object, eg. a boolean
HasPaid, which is based on another managed property, let say a DateTime? PayDate

Public bool HasPaid {

   get { return PayDate.HasValue(); } //PayDate is a CSLA managed property

}

or

Public bool Amount {

 get { return SubAmountA + SubAmountB; }//Both SubAmountA and SubAmountB are CSLA managed properties

}

Can I then define BusinessRules that include this HasPaid readonly property (with getter only) as my InputProperties?

I do not know how to proceed, as creating/registring a static property does not seem useful (no data to store) on this particular field?

Most of the business rule architecture is based on registered properties with the:

context.InputPropertyValues(InputProperties(0))

Any feedback?

Regards,

Gilles

Note: I never used the context.Target pattern in my rule implementation

JonnyBee replied on Tuesday, October 23, 2012

You have 2 options:

1. Create a PropertyInfo object (required for the RuleEngine and InputProperties), mark it with RelationshipTypes.PrivateField and override the Non-Generic ReadProperty metod to supply the value to the rule engine.

2. Use Context.Target to read the property value.

IE: For PrivateFields -.  the developer must override the non-generic ReadProperty/LoadProperty meth toods get or update the property value. This will also allow you to declare the readonly properties as AffectedProperties and automatically get OnPropertyChanged on the fields by the RuleEngine as well as InputProperties.

Yes - that will leave an empty slot in the FieldManager but the value will always be null (not initialized) and I do not consider that to be an issue.

g.beraudo@castsoftware.com replied on Wednesday, October 24, 2012

Hi Jonny,

Good. I will go with option 1, as it allows Rule re-use.

Digging in the code, I saw, that the CSLA
BusinessBase.ReadProperty methods (l. 1'912) handles private backing fields, by calling the MethodCaller.CallPropertyGetter
but this method only supports public getter (there is an explicit error message).

Is there any reason CSLA does not support private/protected properties getters ?

This is not a problem on my case anyway.

 

From your feedback, I understand that you were recommending to override the BusinessBase.ReadProperty instead. Am I correct?

Thanks already,

Gilles

JonnyBee replied on Wednesday, October 24, 2012

 

The recommended solution is to override the non-generic ReadProperty/LoadProperty.

Csla implements a "falllback" to the only known solution we have - which is to call the actual property getter.
It is only your code that will know the name of the actual backing field variable - hence you should implement this in your own overloads.

Copyright (c) Marimer LLC