newbie question again - multiple criteria in a root list BO

newbie question again - multiple criteria in a root list BO

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


SethSpearman posted on Friday, December 14, 2007

I have EditRootList BO.  On page 383 of Rocky's book he says:

 "If you have a root collection in which your directly retrieving a collection of child object, the Criteria class may not define a single object, but rather act as a search filter that returns the collection populated with all matching child objects."

I want to do this very thing but I am not sure how.  Can anyone point me to an example.  Specifically I would want to return the children based on 3 different possible criteria. 

Specifically, what would the next criteria class look like (example please) and what would the DataPortal_Fetch class look like.  

Or could you create more than one nested criteria class and then create 3 overloaded DataPortal_Fetch classes (can you do that???)

Any advice will be appreciated.  Thanks for all that you do.

SEth

Michael Hildner replied on Friday, December 14, 2007

SethSpearman:
Or could you create more than one nested criteria class and then create 3 overloaded DataPortal_Fetch classes (can you do that???)

That's what I do, FWIW.

SethSpearman replied on Friday, December 14, 2007

Thanks for the reply Micheal...will give it a shot.

Seth

JoeFallon1 replied on Friday, December 14, 2007

I put my criteria classes in a separate namespace and then re-use them across BOs.

I tried to make them general enough but sometimes it is beter to just create a new one so you can tell what you are passing.

e.g.

Namespace Criteria

'Any BO that needs to pass an interger is the "key" for this criteria class.

  <Serializable()> _
  Public Class CriteriaIntegerKey
    Inherits CSLA.Server.CriteriaBase
    Public MethodName As String = ""
    Public Key As Integer = 0

    Public Sub New(ByVal BOtype As Type, ByVal methodName As String, ByVal key As Integer)
      MyBase.New(BOtype)
      Me.MethodName = methodName
      Me.Key = key
    End Sub
  End Class

'Any BO that needs to pass 2 Strings can use this criteria class.
'But it helps if the Strings are Invoice Niumber and VendorNumber.

<Serializable()> _
 Public Class CriteriaInvoiceNumberVendno
    Inherits CSLA.Server.CriteriaBase
    Public MethodName As String = ""
    Public Vendno As String = ""
    Public InvoiceNumber As String = ""

    Public Sub New(ByVal BOtype As Type, ByVal methodName As String, ByVal invoiceNumber As String, ByVal vendno As String)
      MyBase.New(BOtype)
      Me.MethodName = methodName
      Me.Vendno = vendno
      Me.InvoiceNumber = invoiceNumber
    End Sub
  End Class

End Namespace

So the first criteria class above is more genral than the 2nd and caqn be used in more BOs.

Factory methods could look like this:

Public Overloads Shared Function GetMyBO(ByVal key As Integer) As MyBO
 
Return CType(DataPortal.Fetch(New CriteriaIntegerKey(MyBO, "GetMyBO", key)), GetMyBO)
End Function

Public Shared Function GetMyBOForPO(ByVal ponumber As String, ByVal invnumber As String) As GetMyBO
 
Return CType(DataPortal.Fetch(New CriteriaPONumberInvoiceNumber(GetMyBO, "GetMyBOForPO", ponumber, invnumber)), GetMyBO)
End Function

Now you can branch on either the type of the Criteria or the methodname value. Use methodname when you have 2 or more of the same Crtieria being passed.

e.g.

Protected Overrides Sub DataPortal_Fetch(ByVal Criteria As Object)
If TypeOf (Criteria) Is CriteriaIntegerKey Then
 
Dim crit As CriteriaIntegerKey = CType(Criteria, CriteriaIntegerKey)
  doSomething(crit)

ElseIf TypeOf (Criteria) Is CriteriaPONumberInvoiceNumber Then
 
Dim crit As CriteriaPONumberInvoiceNumber = CType(Criteria, CriteriaPONumberInvoiceNumber)
 
If crit.MethodName = "GetMyBOForPO" Then
   
doSomething Else(crit)
 
End If

'etc.

 

The one DataPortal_Fetch becomes a branching point for your code.

You could also write multiple ones with strong typed criteira which should also work.

Joe

Copyright (c) Marimer LLC