codesmithtools / Templates

CodeSmith Generator Templates
http://www.codesmithtools.com/product/generator
54 stars 35 forks source link

CSLA: Add unique rule to check to see if item exists when IsValid is called. #236

Closed GoogleCodeExporter closed 4 years ago

GoogleCodeExporter commented 9 years ago
Add unique rule to check to see if item exists when IsValid is called.

Original issue reported on code.google.com by bniemyjski on 28 Jan 2010 at 3:21

GoogleCodeExporter commented 9 years ago
Blake here is an example of a single unique field validation rule based on your
Public Shared Function Exists(criteria) as boolean method.

Protected Function AddBusinessValidationRules() As Boolean

        ValidationRules.AddRule(Of BDM, RuleArgs)(AddressOf BDM.IsUnique, New
RuleArgs("FullName", "Business Development Manager"))
        Return False
    End Function

   ''' <summary>
    ''' Uses a generic Exists(criteria) method to determine if a database column(s)
are unique before the record gets saved to the database.
    ''' This works incombination with database unique contraints.
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <param name="target"></param>
    ''' <param name="e"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' We check for IsDirty or IsNew to ensure we are not running this IsUnique
check when we use the DataPortal_Fetch to get the information from the 
database.  The
database should manage constraints, so we should duplicate that effort.
    ''' TODO: Should look at making this a generic solution vs copying this to every
business object.</remarks>
    Private Shared Function IsUnique(Of T As BDM)(ByVal target As T, ByVal e As
RuleArgs) As Boolean
        Dim result As Boolean

        If e.PropertyFriendlyName = String.Empty Then
            Throw New ArgumentOutOfRangeException("e", "Rule arguments must include
PropertyFriendlyName so the users can get a meaningful broken rule message")
        End If

        If target.IsDirty OrElse target.IsNew Then

            Dim criteria As BDMCriteria = New BDMCriteria
            '   Note:   You could add multiple field criteria if you had a 
            With criteria
                .FullName = target.FullName
            End With

            '   Use the codesmith Exists method to check for existing values.
            If BDM.Exists(criteria) Then                
                e.Description = String.Format("{0} value of '{1}' already exists and
cannot be used.", e.PropertyFriendlyName, target.FullName)
                result = False
            Else
                result = True
            End If
        Else
            ' Passed because we did not test it...
            result = True
        End If

        Return result
    End Function

Original comment by JenasysD...@gmail.com on 25 Feb 2010 at 11:12

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 8 Jul 2010 at 8:58

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 15 Jul 2010 at 3:48

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 9 Mar 2012 at 1:12