Visual Basic

Creating new workers

You use the AddNew method when you want to request a new worker object from the factory. In the factory, this involves adding a row to the recordset and creating a new worker object that references this added row. One minor complication here: what if I don't have already have a recordset?

Suppose that I've created a factory object, but I haven't populated it. In this case, I don't have a recordset at all, so before I can create the new worker object I have to create the recordset. Now, when I get a recordset from the DAL, it comes back already containing the required fields. But if I don't have a recordset, I'm going to have to build one manually. This is a slightly laborious task because it means performing AddField operations for each property on the worker object. Another way to do this would be to retrieve an empty recordset from the DAL, either by requesting the required recordset with parameters that will definitely return an empty recordset, or by having an optional parameter on the OpenRecordset call. In our current implementation, however, we build empty recordsets inside the factory object itself.

But before we look at the process of creating a recordset manually, let's see the AddNew procedure in action:

Dim ocDAL           As New cDAL
  Dim ocfEmployees    As New cfEmployees
  Dim ocwEmployee     As New cwEmployee
  ocfEmployees.Create ocDAL
  Set ocfEmployee = ocfEmployees.AddNew
  ocfEmployee.Name = "Adam Magee"

This is how it is implemented:

Public Function AddNew() As cwEmployee
      Dim ocwEmployee As cwEmployee
      If oPicRecordset Is Nothing Then
          Set oPicRecordset = New cRecordset
          With oPicRecordset
              .AddField "ID"
              .AddField "Department"
              .AddField "FirstName"
              .AddField "LastName"
          End With
      End If
      oPicRecordset.AddNew    ' Add an empty row for the
                              ' worker object to reference.
      Set ocwEmployee = New cwEmployee
      ocwEmployee.Create oPicRecordset, oPicDAL
      colPicwWorkers.Add ocwEmployee
      Set New = ocwEmployee
  End Property

This introduces an unavoidable maintenance problem, though. Changes to the worker object must now involve updating this code as well-not the most elegant solution, but it's worth keeping this in mind whenever changes to the worker objects are implemented.

Persistence (Perhaps?)

So now we can retrieve worker objects from the database, we can add them to other factories, and we can create new ones-all well and good-but what about saving them back to the data source? This is the role of persistence. Basically, persistence involves sending the recordset back to the database to be updated. The DAL has a method that does exactly that-UpdateRecordset-and we can also supply and retrieve any parameters that might be appropriate for the update operation (although most of the time UpdateRecordset tends to happen without any reliance on parameters at all).

Dim ocDAL            As New cDAL
  Dim ocfEmployees     As New cfEmployees
  Dim ocwEmployee      As New cwEmployee
  Dim ocParams         As New cParams
  ocfEmployees.Create ocDAL
  ocParams.Add "Department", "Engineering"
  ocfEmployees.Populate ocParams
  For Each ocwEmployee In ocfEmployees
      .Salary = "Peanuts"
  Next  'ocwEmployee
  ocfEmployees.Persist     ' Reduce costs

What is this Persist method doing, then?

Public Sub Persist(Optional i_ocParams As cParams)
      oPicDAL.UpdateRecordset oPicRecordset, i_ocParams
  End Sub

Consider it persisted.