cslaforum icon indicating copy to clipboard operation
cslaforum copied to clipboard

Business Rules

Open brembot opened this issue 5 years ago • 8 comments
trafficstars

Hi, I am now in the process of porting my old VB code to C# and upgrading my old CSLA to the latest version 5.0.1. Honestly, I am catching up with the latest changes of the new CSLA. What are the recommended approach in the new CSLA in implementing the code snippet below? I would prefer a C# code if you will give a sample code. Thanks.

Protected Overrides Sub AddBusinessRules()
    '// TODO: add validation rules
    '// ValidationRules.AddRule(AddressOf CommonRules.StringRequired, "PropertyName")
    '// ValidationRules.AddRule(AddressOf CommonRules.StringMaxLength, New CommonRules.MaxLengthRuleArgs("PropertyName", 50))
    '// ValidationRules.AddRule(AddressOf CommonRules.MinValue(Of Integer), New CommonRules.MinValueRuleArgs(Of Integer)("PropertyName", 1))
    '// ValidationRules.AddRule(AddressOf CommonRules.MinValue(Of Byte), New CommonRules.MinValueRuleArgs(Of Byte)("PropertyName", 1))

    ValidationRules.AddRule(Of AuctionBid)(AddressOf YearRule(Of AuctionBid), "Year")

    '// TODO: add dependant property
    ValidationRules.AddDependantProperty("SmokerNote", "TyreType")
    ValidationRules.AddDependantProperty("SmokerNote", "TyreSize")
    ValidationRules.AddDependantProperty("SmokerNote", "TyreCondition")
End Sub

Private Shared Function YearRule(Of T As AuctionBid)(ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean
    '// exclude the child object for checking
    If target.IsChild Then
        Return True
    End If

    If target.Year <= 0 Then
        e.Description = "Year is required."
        Return False
    End If

    '// return true if valid
    Return True
End Function

Private Shared Function TyreTypeRule(Of T As AuctionBid)(ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean
    '// exclude the child object for checking
    If target.IsChild Then
        Return True
    End If

    If Not target.SmokerNote = "On TV" AndAlso target.TyreType.Trim = String.Empty Then
        e.Description = "Tire Type is required."
        Return False
    End If

    '// return true if valid
    Return True
End Function

Private Shared Function TyreSizeRule(Of T As AuctionBid)(ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean
    '// exclude the child object for checking
    If target.IsChild Then
        Return True
    End If

    If Not target.SmokerNote = "On TV" AndAlso target.TyreSize.Trim = String.Empty Then
        e.Description = "Tire Size is required."
        Return False
    End If

    '// return true if valid
    Return True
End Function

Private Shared Function TyreConditionRule(Of T As AuctionBid)(ByVal target As T, ByVal e As Validation.RuleArgs) As Boolean
    '// exclude the child object for checking
    If target.IsChild Then
        Return True
    End If

    If Not target.SmokerNote = "On TV" AndAlso target.TyreCondition.Trim = String.Empty Then
        e.Description = "Tire Condition is required."
        Return False
    End If

    '// return true if valid
    Return True
End Function

Version and Platform CSLA version: 3.0.5 OS: Windows Platform: WinForms

brembot avatar Jan 15 '20 10:01 brembot

Hi, My 2 cents, as we undergo the same journey a few months ago (VB.NET -> C#). I would recommend you to split your journey in 2 steps, and don't upgrade CSLA at the same time. In our case, we first converted in C# (and we use a small tool that I strongly recommend: https://www.tangiblesoftwaresolutions.com/product_details/vb-to-csharp-converter.html ) then we upgraded to the most recent version (and leverage the fact that there is much examples of code in C# ). Gilles Ps: I have no interest in reselling Tangible products, but found it impressive.

GillesBer avatar Jan 15 '20 11:01 GillesBer

Regarding the rule part, what CSLA are you migrating from? It seems pre CSLA v4 right? I would recommend reading: http://www.lhotka.net/weblog/CSLA4BusinessRulesSubsystem.aspx and I believe (we did not follow this path at the time) there there is a NuGet package that provide some compatibility with the previous rule system.

GillesBer avatar Jan 15 '20 11:01 GillesBer

Yes, the Csla.UpdateValidation package contributed by @jonnybee makes it possible to wrap delegate-style rules for use in the new class-based rules engine.

I also recommend Tangible. Back in the early 2000's when I was maintaining CSLA in both C# and VB (yes, really!) I used their product a lot!

rockfordlhotka avatar Jan 15 '20 19:01 rockfordlhotka

Thank you for your input guys. I will check all your suggestions.

brembot avatar Jan 16 '20 00:01 brembot

Btw @rockfordlhotka, just one more thing. Do we have example how to use the BusinessRulesExtensions? Thanks

brembot avatar Jan 16 '20 10:01 brembot

I'm not 100% sure, but I think @jonnybee put some examples into the RuleTutorial sample.

rockfordlhotka avatar Jan 17 '20 17:01 rockfordlhotka

That's great. Thank you.

brembot avatar Jan 20 '20 07:01 brembot

There is also a blog post about this package: https://jonnybekkum.wordpress.com/2013/10/07/csla-validationthe-new-nuget-package-in-csla-net-4-5-40/

jonnybee avatar Jan 20 '20 08:01 jonnybee