1. I create a new project as name Common.
2. I move CCustomer,CProduct,COrderHeader and COrderLine to CommonProject.
3. I remove reference to AtomsFramework in UI (VBSample).
4. I add reference to AtomsFramework in CommonProject and add reference to CommonProject in UI (VBSample).
and then...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Public Class frmOrders
Inherits System.Windows.Forms.Form
Dim m_order As COrderHeader
Dim m_orderline As COrderLine
'Dim persbroker As CPersistenceBroker
Dim CPOAccess As CPersistenseObjectAccess
#Region " Windows Form Designer generated code "
.
.
#End Region
Private Sub frmOrders_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'persbroker = New CPersistenceBroker
'persbroker.init("..\AtomsFramework.XML")
CPOAccess = New CPersistenseObjectAccess
End Sub
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
m_order = New COrderHeader
If TextBox1.Text.Length > 0 Then
m_order.OrderNo = CInt(TextBox1.Text)
CPOAccess.find(m_order)
'm_order.find(m_order)
Else
TextBox1.Text = "New"
End If
ObjectToInterface()
End Sub
Private Sub ObjectToInterface()
If m_order.Customer Is Nothing Then
TextBox2.Text = Nothing
Label4.Text = Nothing
Else
TextBox2.Text = m_order.Customer.CustomerID
Label4.Text = m_order.Customer.CustomerName
End If
TextBox3.Text = m_order.OrderDate.ToShortDateString
If m_order.OrderLines.Count > 0 Then
m_orderline = CType(m_order.OrderLines, IList).Item(0)
Else
m_orderline = Nothing
End If
LineToInterface()
End Sub
Private Sub InterfaceToObject()
m_order.OrderDate = CDate(TextBox3.Text)
End Sub
Private Sub TextBox2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
Dim c As CCustomer
c = New CCustomer
c.CustomerID = TextBox2.Text
'c.Find(c)
CPOAccess.find(c)
If Not CPOAccess.IsPersistent(c) Then
e.Cancel = True
MsgBox("Customer does not exist")
Else
Label4.Text = c.CustomerName
m_order.Customer = c
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InterfaceToObject()
'm_order.Save()
CPOAccess.save(m_order)
MsgBox("Saved order number " & m_order.OrderNo.ToString)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'm_order.Delete()
CPOAccess.delete(m_order)
m_order = New COrderHeader
ObjectToInterface()
End Sub
Private Sub TextBox4_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox4.Validating
m_orderline = New COrderLine
If TextBox4.Text.Length = 0 Then
e.Cancel = True
MsgBox("You must enter a line number")
End If
m_orderline.LineNumber = CInt(TextBox4.Text)
'm_orderline.Find(m_orderline)
CPOAccess.find(m_orderline)
LineToInterface()
End Sub
Private Sub LineToInterface()
Dim m_line As COrderLine
Dim lvi As ListViewItem
If m_orderline Is Nothing Then
TextBox4.Text = Nothing
TextBox5.Text = Nothing
Label7.Text = Nothing
TextBox6.Text = Nothing
ListView1.Items.Clear()
Exit Sub
End If
TextBox4.Text = m_orderline.LineNumber.ToString
If m_orderline.Product Is Nothing Then
TextBox5.Text = Nothing
Label7.Text = Nothing
Else
TextBox5.Text = m_orderline.Product.ProductCode
Label7.Text = m_orderline.Product.Name
End If
TextBox6.Text = m_orderline.Quantity.ToString
ListView1.Items.Clear()
For Each m_line In m_order.OrderLines
lvi = New ListViewItem
lvi.Text = m_line.LineNumber.ToString
lvi.SubItems.Add(m_line.Product.ProductCode)
lvi.SubItems.Add(m_line.Quantity)
ListView1.Items.Add(lvi)
Next
End Sub
Private Sub InterfaceToLine()
m_orderline.Quantity = CInt(TextBox6.Text)
End Sub
Private Sub TextBox5_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox5.Validating
Dim p As New CProduct
p.ProductCode = TextBox5.Text
'p.Find(p)
CPOAccess.find(p)
If Not CPOAccess.IsPersistent(p) Then
e.Cancel = True
MsgBox("Product does not exist")
Else
Label7.Text = p.Name
m_orderline.Product = p
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
InterfaceToLine()
If Not CPOAccess.IsPersistent(m_orderline) Then
m_orderline.Order = m_order
'm_order.OrderLines.Add(m_orderline)
Dim x As IList = m_order.OrderLines
x.Add(m_orderline)
End If
m_orderline = New COrderLine
LineToInterface()
End Sub
End Class
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1. You are doing a strict n-tiered application where there is no inheritance between the DAL and the business objects.
2. (I assume here) Inheriting from CPersistentObject is not desired as you then have to include extra data for .NET remoting, etc
3. You'd like to use a MVC design pattern for the UI (or the UIPAB) but persistance via interfaces is difficult
Well, Release candidate 1 for version 2.0 of the Atoms Framework is about to be released (I was going to do it tomorrow). The new version features the ability to persist objects that do not inherit from the CPersistentObject, and you can also persist objects via interfaces instead of concrete classes.
If you don't want to wait that long, get the latest code from CVS, and also the unit tests as they will show you how to do non-ihnerited persistence.
You will still need to reference the AtomsFramework in you BL and UI for access to the persistence broker, but there will be no direct inheritance of the CPersistentObject.
- Richard.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
How do I use AtomsFramework?
Example in your frameworksamples.
1. I create a new project as name Common.
2. I move CCustomer,CProduct,COrderHeader and COrderLine to CommonProject.
3. I remove reference to AtomsFramework in UI (VBSample).
4. I add reference to AtomsFramework in CommonProject and add reference to CommonProject in UI (VBSample).
and then...
5 I create CPersistenseObjectAccess.vb on Common.
Imports AToMSFramework
Public Class CPersistenseObjectAccess
Private persbroker As CPersistenceBroker
Public Sub New()
persbroker = New CPersistenceBroker
persbroker.init("C:\AtomsFramework\FrameworkSamples\Common\AtomsFramework.XML")
End Sub
Public Sub find(ByRef obj As Object)
CType(obj, CPersistentObject).Find(obj)
End Sub
Public Sub save(ByRef obj As Object)
CType(obj, CPersistentObject).Save()
End Sub
Public Sub delete(ByRef obj As Object)
CType(obj, CPersistentObject).Delete()
End Sub
Public Function getAll(ByRef obj As Object) As IList
Return CType(obj, CPersistentObject).getAll
End Function
Public Function IsPersistent(ByRef obj As Object) As Boolean
Return CType(obj, CPersistentObject).Persistent()
End Function
End Class
5. I create class as CPersistenseObjectAccess.vb on CommonProject
Imports AToMSFramework
Public Class CPersistenseObjectAccess
Private persbroker As CPersistenceBroker
Public Sub New()
persbroker = New CPersistenceBroker
persbroker.init("C:\AtomsFramework\FrameworkSamples\Common\AtomsFramework.XML")
End Sub
Public Sub find(ByRef obj As Object)
CType(obj, CPersistentObject).Find(obj)
End Sub
Public Sub save(ByRef obj As Object)
CType(obj, CPersistentObject).Save()
End Sub
Public Sub delete(ByRef obj As Object)
CType(obj, CPersistentObject).Delete()
End Sub
Public Function getAll(ByRef obj As Object) As IList
Return CType(obj, CPersistentObject).getAll
End Function
Public Function IsPersistent(ByRef obj As Object) As Boolean
Return CType(obj, CPersistentObject).Persistent()
End Function
End Class
6. I create a class on CommonProject.
Option Strict Off
Option Explicit On
Imports AToMSFramework
Public Class CEntityCollection
Inherits CPersistentCollection
End Class
I modify a COrderHeader from CPersistentCollection to CEntityCollection.
8. I modify a frmOrders.
Public Class frmOrders
Inherits System.Windows.Forms.Form
Dim m_order As COrderHeader
Dim m_orderline As COrderLine
'Dim persbroker As CPersistenceBroker
Dim CPOAccess As CPersistenseObjectAccess
#Region " Windows Form Designer generated code "
.
.
#End Region
Private Sub frmOrders_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'persbroker = New CPersistenceBroker
'persbroker.init("..\AtomsFramework.XML")
CPOAccess = New CPersistenseObjectAccess
End Sub
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
m_order = New COrderHeader
If TextBox1.Text.Length > 0 Then
m_order.OrderNo = CInt(TextBox1.Text)
CPOAccess.find(m_order)
'm_order.find(m_order)
Else
TextBox1.Text = "New"
End If
ObjectToInterface()
End Sub
Private Sub ObjectToInterface()
If m_order.Customer Is Nothing Then
TextBox2.Text = Nothing
Label4.Text = Nothing
Else
TextBox2.Text = m_order.Customer.CustomerID
Label4.Text = m_order.Customer.CustomerName
End If
TextBox3.Text = m_order.OrderDate.ToShortDateString
If m_order.OrderLines.Count > 0 Then
m_orderline = CType(m_order.OrderLines, IList).Item(0)
Else
m_orderline = Nothing
End If
LineToInterface()
End Sub
Private Sub InterfaceToObject()
m_order.OrderDate = CDate(TextBox3.Text)
End Sub
Private Sub TextBox2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
Dim c As CCustomer
c = New CCustomer
c.CustomerID = TextBox2.Text
'c.Find(c)
CPOAccess.find(c)
If Not CPOAccess.IsPersistent(c) Then
e.Cancel = True
MsgBox("Customer does not exist")
Else
Label4.Text = c.CustomerName
m_order.Customer = c
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InterfaceToObject()
'm_order.Save()
CPOAccess.save(m_order)
MsgBox("Saved order number " & m_order.OrderNo.ToString)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'm_order.Delete()
CPOAccess.delete(m_order)
m_order = New COrderHeader
ObjectToInterface()
End Sub
Private Sub TextBox4_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox4.Validating
m_orderline = New COrderLine
If TextBox4.Text.Length = 0 Then
e.Cancel = True
MsgBox("You must enter a line number")
End If
m_orderline.LineNumber = CInt(TextBox4.Text)
'm_orderline.Find(m_orderline)
CPOAccess.find(m_orderline)
LineToInterface()
End Sub
Private Sub LineToInterface()
Dim m_line As COrderLine
Dim lvi As ListViewItem
If m_orderline Is Nothing Then
TextBox4.Text = Nothing
TextBox5.Text = Nothing
Label7.Text = Nothing
TextBox6.Text = Nothing
ListView1.Items.Clear()
Exit Sub
End If
TextBox4.Text = m_orderline.LineNumber.ToString
If m_orderline.Product Is Nothing Then
TextBox5.Text = Nothing
Label7.Text = Nothing
Else
TextBox5.Text = m_orderline.Product.ProductCode
Label7.Text = m_orderline.Product.Name
End If
TextBox6.Text = m_orderline.Quantity.ToString
ListView1.Items.Clear()
For Each m_line In m_order.OrderLines
lvi = New ListViewItem
lvi.Text = m_line.LineNumber.ToString
lvi.SubItems.Add(m_line.Product.ProductCode)
lvi.SubItems.Add(m_line.Quantity)
ListView1.Items.Add(lvi)
Next
End Sub
Private Sub InterfaceToLine()
m_orderline.Quantity = CInt(TextBox6.Text)
End Sub
Private Sub TextBox5_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox5.Validating
Dim p As New CProduct
p.ProductCode = TextBox5.Text
'p.Find(p)
CPOAccess.find(p)
If Not CPOAccess.IsPersistent(p) Then
e.Cancel = True
MsgBox("Product does not exist")
Else
Label7.Text = p.Name
m_orderline.Product = p
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
InterfaceToLine()
If Not CPOAccess.IsPersistent(m_orderline) Then
m_orderline.Order = m_order
'm_order.OrderLines.Add(m_orderline)
Dim x As IList = m_order.OrderLines
x.Add(m_orderline)
End If
m_orderline = New COrderLine
LineToInterface()
End Sub
End Class
Q1: How do I use AtomsFramework for n-tier app.
Q2: Your suggestion,please?
Thank you.
That's a lot of posts :-)
Just to make sure I understand your situation
1. You are doing a strict n-tiered application where there is no inheritance between the DAL and the business objects.
2. (I assume here) Inheriting from CPersistentObject is not desired as you then have to include extra data for .NET remoting, etc
3. You'd like to use a MVC design pattern for the UI (or the UIPAB) but persistance via interfaces is difficult
Well, Release candidate 1 for version 2.0 of the Atoms Framework is about to be released (I was going to do it tomorrow). The new version features the ability to persist objects that do not inherit from the CPersistentObject, and you can also persist objects via interfaces instead of concrete classes.
If you don't want to wait that long, get the latest code from CVS, and also the unit tests as they will show you how to do non-ihnerited persistence.
You will still need to reference the AtomsFramework in you BL and UI for access to the persistence broker, but there will be no direct inheritance of the CPersistentObject.
- Richard.
Thank you very much.
- Anyakit