Visual Basic

Removing and Deleting

What about removing worker objects from factories? Well, you have two options-sack 'em or whack 'em!

A worker object can be removed from the factory, which has no effect on the underlying data source. Maybe the factory is acting as a temporary collection for worker objects while they wait for an operation to be performed on them. For example, a collection of Employee objects needs to have the IncreaseSalary method called (yeah, I know what you're thinking-that would be a pretty small collection). For one reason or another, you need to remove an Employee worker object from this factory (maybe the worker object had the SpendAllDayAtWorkSurfingTheWeb property set to True), so you would call the Remove method. This method just removes the worker object from this factory with no effect on the underlying data. This is the sack 'em approach.

You use the other method when you want to permanently delete an object from the underlying data source as well as from the factory. This involves calling the Delete method on the factory and is known as the whack 'em approach. Calling Delete on the factory will completely remove the worker object and mark its row in the database for deletion the next time a Persist is executed.

This is an important point worth repeating-if you delete an object from a factory, it is not automatically deleted from the data source. So if you want to be sure that your worker objects data is deleted promptly and permanently, make sure that you call the Persist method! Some of you might ask, "Well, why not call the Persist directly from within the Delete method?" You wouldn't do this because of performance. If you wanted to delete 1000 objects, say, you wouldn't want a database update operation to be called for each one-you would want it to be called only at the end when all objects have been logically deleted.

Dim ocDAL            As New cDAL
  Dim ocfEmployees     As New cfEmployees
  Dim ocwEmployee      As New cwEmployee
  ocfEmployees.Create ocDAL
  For Each ocwEmployee In ocfEmployees
      With ocwEmployee
          If .Salary = "Peanuts" Then
              ocfEmployees.Remove .Index    ' Save the peasants.
              ocfEmployees.Delete .Index    ' Punish the guilty.
          End If
      End With
  Next  'ocwEmployee
  ocfEmployees.Persist     ' Exact revenge.

One important point to note here is that worker objects cannot commit suicide! Only the factory object has the power to delete or remove a worker object.

Public Sub Remove(i_vKey As Variant)
      colPicwWorkers.Remove i_vKey
  End Sub
  Public Sub Delete(i_vKey as Variant)
      If VarType(i_vKey) = vbString Then
          oPicRecordset.AbsolutePosition = _
          colPicwWorkers.Remove oPicRecordset.AbsolutePosition
          oPicRecordset.AbsolutePosition = i_vKey
          colPicwWorkers.Remove i_vKey
      End If
  End Sub

by BrainBellupdated