CSLA 4 Asynchronous Business Rules in VB .Net

CSLA 4 Asynchronous Business Rules in VB .Net

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

GregDobbs posted on Thursday, June 10, 2010

Hi all.

I am attempting to port code from CSLA 3.8 to CSLA 4.0. Having a bit of trouble with the new rules methodology regarding asynchronous rules.

Under 3.8 I implemented asynch rules as follows (yes, I'm an atavist and still use VB .Net):

        'Add Asynchronous no duplicate eMail check
        ValidationRules.AddRule(AddressOf eMailExists, New AsyncRuleArgs(eMailProperty))

Which used the following methods:

    Private Shared Sub eMailExists(ByVal context As AsyncValidationRuleContext)
        Dim command As New cmdNewMemberEMailExists(context.PropertyValues("eMail"))
        Dim dp As DataPortal(Of cmdNewMemberEMailExists) = New DataPortal(Of cmdNewMemberEMailExists)()
        AddHandler dp.ExecuteCompleted, AddressOf eMailExistsCompleted
        dp.BeginExecute(command, context)
    End Sub

    Private Shared Sub eMailExistsCompleted(ByVal sender As Object, ByVal e As DataPortalResult(Of cmdNewMemberEMailExists))
        Dim context As AsyncValidationRuleContext = CType(e.UserState, AsyncValidationRuleContext)
        If e.Error IsNot Nothing Then
            context.OutArgs.Description = "Error checking for duplicate eMail Address.  " & e.Error.ToString()
            context.OutArgs.Severity = RuleSeverity.Error
            context.OutArgs.Result = False
            If e.Object.ExistsKey > 0 Then
                context.OutArgs.Description = "This eMail Address is already registered!"
                context.OutArgs.Severity = RuleSeverity.Error
                context.OutArgs.Result = False
                context.OutArgs.Result = True
            End If
        End If
    End Sub

I can't see how to translate that to the new BusinessRules.AddRule method.

Can anyone help me with what the new syntax is to achieve the same thing under CSLA 4? I tried to look at the Rolodex sample for CSLA 4, but it's still using the old method of ValidationRules that doesn't work.




RockfordLhotka replied on Friday, June 11, 2010

Rules in CSLA 4 are now a type, not a method. The conversion process is the same in VB and C#:

  1. Create a class with the name of your rule
  2. Inherit from Csla.Rules.BusinessRule
  3. Put your rule code in an override of Execute()

For simple rule methods, this is a pretty easy conversion, and you can see examples in my blog posts (in C#, but still, it is simple code).

For more complex rules this can take more work.

For async rules, you'll need a constructor, in which you set IsAsync to true. Again, my blog posts should be pretty instructive.

GregDobbs replied on Friday, June 11, 2010

You are the man, Rocky! Exactly what I needed - I'll be sure to check your Blog in the future as another resource.

I hope you and your family enjoy a much-deserved vacation next week!

- Greg

Copyright (c) Marimer LLC