Child_Fetch executing on client and not on server

Child_Fetch executing on client and not on server

Old forum URL:

shawndewet posted on Thursday, January 28, 2010

I use lazy loading on my header class to retrieve its associated lines only when the header's MyLines property is accessed. The code in MyLines is this:
Public Overridable ReadOnly Property MyIndentSaleDetailCollection() As IndentSaleDetailCollection
If Not FieldManager.FieldExists(_myIndentSaleDetailCollection) Then
LoadProperty(Of IndentSaleDetailCollection)(_myIndentSaleDetailCollection, _
End If
Return GetProperty(Of IndentSaleDetailCollection)(_myIndentSaleDetailCollection)
End Get
End Property

Now on IndentSaleDetailCollection, the FetchByParent factory method looks like this...

Public Shared Function FetchByParent(ByVal parent As IndentSaleHeader) As IndentSaleDetailCollection
Return DataPortal.FetchChild(Of IndentSaleDetailCollection)(New CriteriaByParent(parent))
End Function

This DataPortal.FetchChild method translates into a call to Child_Fetch, which looks like this...

Private Overloads Sub Child_Fetch(ByVal oCriteria As CriteriaByParent)
_MyIndentSaleHeader = oCriteria.cparent
Using ctx = ConnectionManager(Of SqlConnection).GetManager(oCriteria.strEnvironmentDatabaseKey)
Using cmd = ctx.Connection.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "EXEC sp_executesql " + oCriteria.FullSelectStatement
DoDataReader(cmd, oCriteria.strEnvironmentDatabaseKey)
End Using
End Using
End Sub

Right, now you have the whole context. Can anyone please help me figure out why the Child_Fetch method is being executed on the client and not on the server.

All other calls via the Dataportal correctly execute on the server. From what I can figure out in looking at the DataPortal code, it seems that the FetchChild method does not concern itself with checking on which side of the DataPortal it should execute. If my finding here is correct, I must ask why this is so? Is my scenario for lazy loading child collections not a valid one? Or am I doing something wrong?

Please help

btw, I'm using CSLA 3.7

ajj3085 replied on Thursday, January 28, 2010

All of the xzyChild dataportal methods execute where ever they are called; they assume they are already running on the server.  Call the non-child fetch, and implement a DataPortal_Fetch.  You'll have to remember to call MarkAsChild on your own.

rxelizondo replied on Thursday, January 28, 2010

Hi Andy,

Well, I have never coded a lazy loaded collection so I have no real experience here but my gut feeling would be to basically go ahead and create some kind of CSLA command object that returned the data that I needed to populate my lazy loaded collection. I would execute my CSLA command from within the “Child_Fetch” function.

Why would I want to go into all that trouble when I could achieve (much more easily) the exact same thing they way you are proposing it?

I don’t know, I guess I rather don’t mess around with calling functions such as the MarkAsChild() one. I much prefer letting the CSLA handle that for me and let the CSLA command object be the one that goes to the server.

Just a thought. Whats your opinion?


RockfordLhotka replied on Thursday, January 28, 2010

This command object approach is probably the more elegant solution, but it does require more code, so it is a trade-off.

ajj3085 replied on Thursday, January 28, 2010

Either way will work, but its easier to change the call, the method name, and then add a single line of code than it would be to create a command object.

The child data portal concept is relatively new as well; it wasn't that long ago that you had no choice but to call it yourself.  Its nice because it does take care of the details for you, and makes coding child objects similar to coding root objects, but you don't need to use it (and in this case it's not an option).

Copyright (c) Marimer LLC