Problem with Rules and comparisons on nullable types

I'm building an application (using .NET 3.0 RC1) where I want to allow my customers to create simple Rules to do a consistency check on amounts on an invoice. They might create a condition like

this.totalAmount > 500

to flag invoices that total more than $500.00.

This works OK. But they also need to be able to check whether the total amount is more than the total amount of the last invoice, which would be a condition like

this.totalAmount > this.previous.totalAmount

where I set previous to be a reference to the previous invoice.

This also works OK, except for one case. At times there won't be a previous invoice (for example, the oldest invoice in the database won't have a previous invoice.) Of course, we could add a check for previous != null at the beginning of the condition. But I'd really prefer not to do this, as it's one more thing to explain, and one more thing for the customers to keep in mind. Also, I'm sure they'll often forget to do this, which will cause an exception in the Rules engine. So, I'd like to find a better solution.

Nullable types seem to be just what I need. I can declare each of the amounts in the invoice as double? rather than double. Now when a previous invoice does not exist, I don't set previous to null. Instead, I make it a reference to an invoice, each of whose amounts is null. So now, in a condition like

this.totalAmount > this.previous.totalAmount

this.previous.totalAmount is a double? whose value is null, so by the rules for comparisons on nullable types, the comparison is false, which is exactly what I want.

I prototyped this approach in C#, and it worked fine. But when I try it with Rules, the comparisons don't work they way I expected. For example, if this.previous.totalAmount is null, the conditions

this.previous.totalAmount < 1.0
this.previous.totalAmount < -1.0
this.previous.totalAmount < -1000.0

all evaluate to true, and

this.previous.totalAmount > -1000.0
this.previous.totalAmount > 1000.0

all evaluate to false.

A null seems to be treated similarly to negative infinity, in that it acts like it's smaller than any number.

This isn't my understanding of how comparisons with nullable types should work. I don't know if this behavior is by design, or a bug, or if I'm missing something.

I basically have 3 questions:

1) Is it possible to get nullable types in rules to work the way I'd like them to?

2) If not, can anyone suggest an alternative approach that makes it easier for my customers to deal with cases where the previous invoice doesn't exist?

3) Is there any documentation on the syntax and semantics of Conditions, Then Actions, and Else Actions? I've read the few papers and watched the videos, and those have a lot of nice examples of these, but what I'm missing is the syntax that shows what I can put in a Condition and what I can't, what kinds of types the fields can have, how comparisons are evaluated, etc.

Thanks in advance for any assistance!

Wayne Vucenic
No Bugs Software

[3165 byte] By [WayneVucenic] at [2007-12-24]
# 1

Wayne,

There are some discrepancies with the CLR in how the rules engine is handling these operators on nullable types. We're planning on addressing this in our next release.

In the meantime, doing the null test is probably the best option.

One other option would be to use custom expressions; you can find an example below. In this case, your custom expression could do the null check before accessing the field. This probably isn't the designer experience that you'd want in this scenario, though.

http://wf.netfx3.com/files/folders/rules_samples/entry4315.aspx

JurgenWillis at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 2

Hi Jurgen,

Thanks for your response, and for the pointer to the custom expression sample!

I think I'll do the null test and eagerly await the next release.

Thanks,

Wayne

WayneVucenic at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...

Software Development for Windows Vista

Site Classified