cslaforum
cslaforum copied to clipboard
Business Rules
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
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.
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.
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!
Thank you for your input guys. I will check all your suggestions.
Btw @rockfordlhotka, just one more thing. Do we have example how to use the BusinessRulesExtensions? Thanks
I'm not 100% sure, but I think @jonnybee put some examples into the RuleTutorial sample.
That's great. Thank you.
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/