Calls to update child objects

Calls to update child objects

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


JohnB posted on Thursday, August 13, 2009

I have a question regarding the updating of child objects. I've done it a certain way and it's been working fine but while browse some of the old (3.0.4) Project Tracker code I noticed that it was different than what I am doing. Just wanted to get some feedback from the group.

My Current Config:
- Csla 3.0.4
- Ported the connection manager from newer version of Csla

Current Code:
Protected Overrides Sub DataPortal_Update()
    If Me.IsDirty Then
        Using pConn As ConnectionManager(Of SqlConnection) = ConnectionManager(Of SqlConnection).GetManager(DSConfiguration.DataConnection)
            Using pCmd As SqlCommand = pConn.Connection.CreateCommand
                With pCmd
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "[Some Proc]"

                    .ExecuteNonQuery()
                End With
            End Using

            _ChildObject.Update()
        End Using
    End If

    MarkOld()
End Sub


Project Tracker Code - Csla 3.0.4
Method #1

Protected Overrides Sub DataPortal_Insert()
  Using cn As New SqlConnection(Database.PTrackerConnection)
    cn.Open()
      ApplicationContext.LocalContext("cn") = cn
      
      Using cm As SqlCommand = cn.CreateCommand
        With cm
          .CommandType = CommandType.StoredProcedure
          .CommandText = "addResource"
          .Parameters.AddWithValue("@lastName", mLastName)
          .Parameters.Add(param)

          .ExecuteNonQuery()
        End With
      End Using
      
      ' update child objects
      mAssignments.Update(Me)    
  End Using
End Sub

Method #2
Protected Overrides Sub DataPortal_Insert()
  Using cn As New SqlConnection(Database.PTrackerConnection)
    cn.Open()
      Using cm As SqlCommand = cn.CreateCommand
        cm.CommandText = "addProject"
        DoInsertUpdate(cm)
      End Using
  End Using

  ' update child objects
  mResources.Update(Me)
End Sub


As you can see from the old PT code, calls to the child update are being called both in and outside of the Using block for the connection.

My question is this. Given my code at the top and using the ConnectionManager, does it matter where I call to update my child objects? My thought process that is if I move the call outside of the Using for the connection then that connection would get disposed and a new connection would be created on the child update. If left where it is now, the ConnectionManager would "reuse" the existing connection.

Thoughts?

RockfordLhotka replied on Thursday, August 13, 2009

You are looking at ProjectTracker code that isn't using ConnectionManager, so the coding style is different.

If you are using ConnectionManager, then all data access must be within the root object's using block or you won't get the benefit of the ConnectionManager behavior.

JohnB replied on Thursday, August 13, 2009

That makes sense but I just wanted to make sure.

Thanks

Copyright (c) Marimer LLC