Upgrading from 1.1 to 2.0

Upgrading from 1.1 to 2.0

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


danielw posted on Wednesday, August 30, 2006

We have previous written a form that will automatically check if a business object is dirty when the form that the business object is on  is closed and prompt the user to save the changes if required.

However the use of generics has scotched our plans in version 2.0.

Can anybody give a pointer/ advice on how we could write the following code in 2.0

Cheers

Daniel Wood

Imports System.Windows.Forms
Imports CSLA

Public Class BusinessBaseForm
    Inherits System.Windows.Forms.Form

    Protected mBizObj As BusinessBase
    ' Set this field to TRUE on the inherited form in the
    ' cancel button's click event
    Protected cancelledByUser As Boolean = False


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'BusinessBaseForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(328, 206)
        Me.Name = "BusinessBaseForm"
        Me.Text = "BusinessBaseForm"

    End Sub

#End Region
    ' Set the main buisness object of the form
    ' so if the user closes the form by clicking
    ' the X, we can decide if the Business object is savable
    ' and prompt the user accordingly
    ' Note, this only works if the main Business object on a form inherits from
    ' Businessbase, not if it inherits from a CollectionBase!

    Public Property Bizobj() As BusinessBase
        Get
            Return mBizObj
        End Get
        Set(ByVal Value As BusinessBase)
            If Not Value Is mBizObj Then
                mBizObj = Value
            End If
        End Set
    End Property

    Private Sub BusinessBaseForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        If Me.DialogResult = DialogResult.OK Then
            If Not Bizobj.IsValid Then
                MessageBox.Show(Bizobj.BrokenRulesString)
                e.Cancel = True
                Exit Sub
            Else
                Exit Sub
            End If
        End If

        If Bizobj.IsDirty Then
            If Not cancelledByUser Then
                Dim saveChanges As DialogResult = MessageBox.Show("Do you wish to save changes ?", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3)
                If saveChanges = DialogResult.Yes Then
                    ' If the bizobj is dirty but not valid
                    ' Inform the user of the broken rules
                    If Not Bizobj.IsValid Then
                        MessageBox.Show(Bizobj.BrokenRulesString)
                        e.Cancel = True
                        Exit Sub
                    End If
                    Me.DialogResult = DialogResult.OK
                    Bizobj.ApplyEdit()
                    Bizobj.Save()
                ElseIf saveChanges = DialogResult.No Then
                    Me.DialogResult = DialogResult.Cancel
                ElseIf saveChanges = DialogResult.Cancel Then
                    e.Cancel = True
                End If
            End If
        End If

    End Sub

    Private Sub BusinessBaseForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

RockfordLhotka replied on Wednesday, August 30, 2006

The Csla.Core.IEditableBusinessObject interface (version 2.0.1+) exposes the IsDirty property, along with various other properties.

The Csla.Core.ISavable interface (version 2.1+) exposes a non-generic Save() method you can use to save any root object.

The Csla.Core.BusinessBase class (version 2.0+) exposes everything you need except the Save() method, which is the reason for adding ISavable.

Copyright (c) Marimer LLC