BusinessListBase + New records save Issue

BusinessListBase + New records save Issue

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


jamie.clayton posted on Friday, March 26, 2010

G'day,

I've been working with Blake, testing the Codesmith templates (CSLA 3.8.2).  I was doing some VB nunit tests for BLB objects and some code that saves a new list item and checks to confirm the Identity column is correctly returned and populated from the Save method.

     Public Shared Function NewList() As ConsultantList
        Return DataPortal.Create(Of ConsultantList)() ' (BusinessListBase)
      End Function

    <RunLocal()> _
    Protected Overrides Sub DataPortal_Create()
    End Sub

<Test()> Sub BLB_AddNew()

     Dim test As Iims.BL.ConsultantList

        test = Iims.BL.ConsultantList.NewList
        test.AddNew()

        ' Test with no data (Will fail)
        Assert.IsFalse(test.IsValid, "New record was valid when it was not supposed to be.")

        ' Ok now setup the core requirements
        With test.Item(0)
            .State = "foo"
            .CountryID = NamedConstants.SystemCountry.Australia
           ....
        End With
        Assert.IsTrue(test.Item(0).IsNew)
        Assert.IsTrue(test.Item(0).IsDirty)
        Assert.IsTrue(test.IsDirty)

        Assert.AreEqual(0, test.Item(0).Identification)

        '   Saving at the root level will not save all children.
        test = test.Save()
        Assert.AreNotEqual(0, test.Item(0).Identification)

End  Sub

This test failes because the CSLA.Dataportal.Save method, line 239 only checks for

var bbase = obj as Core.BusinessBase;

But returns nothing for the "bbase" variable while my business object (Intellisense shows: "CSLA.BusinesBase"). The logic following then incorrectly skips over the Insert/update methods and try's to do a DataPortal_Update, which wouldn't retrieve that tables identity value.

Is this a problem with the Codesmith template configuration or a CSLA bug or PEBKAC by me?

I know I can change the my code to use ERL (via the codesmith templates) and that will work (as I have nunit tests that pass for this), I just wanted to ensure I could use either or as needed, depending on the UI save record strategy needed.

jamie.clayton replied on Friday, March 26, 2010

Note: The following passes.

Assert.IsTrue(TypeOf (test.Item(0)) Is Core.BusinessBase)

jamie.clayton replied on Friday, March 26, 2010

Guys,

Looks like an issue with the BLB DataPortal update incorrectly saving changes. I'll let Blake know.

    Protected Overrides Sub DataPortal_Update()
        Dim cancel As Boolean = False
        OnUpdating(cancel)
        If (cancel) Then
            Return
        End If

        RaiseListChangedEvents = False

        '   Current template code
        'For Each item As Consultant In Items
        '    item.Save()
        'Next
        '   Suggested template code.
        For myLoop As Integer = 0 To Me.Items.Count - 1
            Me.Items.Item(myLoop) = Me.Items.Item(myLoop).Save()
        Next

        RaiseListChangedEvents = True

        OnUpdated()
    End Sub

Copyright (c) Marimer LLC