Help - Generic UniqueIndex Rule

Help - Generic UniqueIndex Rule

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


cdkisa posted on Tuesday, April 28, 2009

Hi,

I saw an example (http://www.codeproject.com/KB/cs/CslaERLB2.aspx) of a NoDuplicates rule and thought it was a great idea. It would save an error from being returned from the database and provide a consistent way to ensure a column/field is unique within the collection.

My question is, is there a way to make this rule generic?
Private Shared Function UniqueIndex(Of T As SomeBusinessObject)(ByVal target As T, ByVal e As Csla.Validation.RuleArgs) As Boolean
	Dim parent As SomeBusinessObjectList = CType(target.Parent, SomeBusinessObjectList)

	If parent IsNot Nothing Then

		For Each item As SomeBusinessObject In parent

			If item.Name.ToUpperInvariant() _
			= target.Name.ToUpperInvariant() _
			AndAlso Not ReferenceEquals(item, target) Then

				e.Description = e.PropertyFriendlyName & " must be unique."
				Return False

			End If

		Next

	End If

	Return True
End Function

Copyright (c) Marimer LLC