Data type conversion in Business rule

Data type conversion in Business rule

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


Arjun posted on Friday, September 06, 2013

Hi,

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.

For example:

in business rule call:        BusinessRules.AddRule(New AssignValueRule(NameProperty))

                                              BusinessRules.AddRule(New AssignValueRule(AgeProperty))

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.

Arjun.

 

skagen00 replied on Friday, September 06, 2013

can you have your AssignValueRule be a generic? AssignValueRule<T> : BusinessRule

new AssignValueRule<int>(AgeProperty)

new AssignValueRule<string>(NameProperty)

Maybe I'm not understanding your question fully though!

Arjun replied on Friday, September 06, 2013

Hi,

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 ?

 

 

skagen00 replied on Friday, September 06, 2013

What does your AssignValueRule do? What are you trying to accomplish?

You do want to add a rule to each property though, no?

Arjun replied on Friday, September 06, 2013

Hi,

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.

BusinessRule.AddRule(New AssignValueRule(NameProperty))

BusinessRule.AddRule(New AssignValueRule(AddressProperty))

BusinessRule.AddRule(New AssignValueRule(IDProperty))

BusinessRule.AddRule(New AssignValueRule(AgeProperty))

 

....................and so on

 

Now, the business rule should handle assigning the values as per the property types.

 

 

 

skagen00 replied on Friday, September 06, 2013

Your initial e-mail lists:

Cast(inputValue, TypeOf(PrimaryProperty))

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.

Arjun replied on Tuesday, September 10, 2013

Hi,

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.

Thanks,

Arjun

 

JonnyBee replied on Saturday, September 07, 2013

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. 

Arjun replied on Tuesday, September 10, 2013

Hi JonnyBee,

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).

Context.AddOutValue(PrimaryProperty, <InputValue>)

This work when PrimaryProperty is String. But when I invoke this rule on Integer, this throws an error. Please suggest an idea.

 

Thanks,

Arjun

 

 

 

JonnyBee replied on Friday, September 20, 2013

Hi,

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