I have a business rule, Where i need to assign a value to the property. However, the tricky part is the casting the value to the type of property.
in business rule call: BusinessRules.AddRule(New AssignValueRule(NameProperty))
Here, name is string and Age is integer. So when i assign value in the busines rule "AssignValueRule", i need to cast it to the property type, its something like below.
Context.AddOutValue(PrimaryProperty, Cast(inputValue, TypeOf(PrimaryProperty))
when rule called on "NameProperty" the inputValue should be casting to String. And when called on "AgeProperty", inputValue should be casting to Integer.
Please help me in implementing this.
Thanks in advance.
can you have your AssignValueRule be a generic? AssignValueRule<T> : BusinessRule
Maybe I'm not understanding your question fully though!
So you mean, there should be separate rule for every type ?? one for integer and one for String and so on ??
I am trying to write a business rule that applies to all properties irrespective of type. Any ideas ?
What does your AssignValueRule do? What are you trying to accomplish?
You do want to add a rule to each property though, no?
Please see my example. This business rule will be called for all properties in my business base class. Say I have 10 properties, (5 integers and 5 string). This business rule fetches values from other list and assign to corresponding property.
the business rule call will be like this.
....................and so on
Now, the business rule should handle assigning the values as per the property types.
Your initial e-mail lists:
Since PrimaryProperty is an IPropertyInfo you're probably more wanting to do something like PrimaryProperty.Type (which would be the int or string)).
Is your current problem that it's not casting right? Because what you have would try to cast the inputValue as a PropertyInfo.
yes I have a problem with casting. I am trying like this.
Context.AddOutValue(PrimaryProperty, DirectCast(<ValueToBeCasted>, PrimaryProperty.Type))
Here value to be casted is a property of string type. Whever PrimaryProperty is Integer, I want the <ValueToBeCasted> to convert to the type of primary property.
But the statement I tried above is throwing a compile time error. So need help in way where i can achieve this conversion.
No, you have just one rule but this rule instance is aware of the actual type of the field.
Look at CommonRules.MaxValue<T> and CommonRules.MinValue<T> rules.
These rules use the generic constraint to know the actual type of the property.
You mean to say I need not do any casting ? But I got a runtime error when i implemented the rule without casting (like below).
This work when PrimaryProperty is String. But when I invoke this rule on Integer, this throws an error. Please suggest an idea.
My suggetion is to use a generic constraint on the rule (so you know the type of the field) and then use Csla.Utilities.CoerceValue<T> to convert the value to the new type.
Csla.Utilities.CoerceValue uses every technique available to convert the value into the desired type.
Copyright (c) Marimer LLC