StrataFrame Forum

Create a collection from business object

http://forum.strataframe.net/Topic12497.aspx

By Jason Seidell - 11/6/2007

I cannot figure out how to extract a set of information from a SF BO to a collection.  I want seperate objects that I can pass around and change the properties of without having to be tied to one BO, and without any concern of the BO's being written back to the database, or one subroutine navigating the BO and breaking the caller sub b/c it didn't realize the recordset was changed, or having to manually reset the BO back to whatever record I was looking at before.

The closest thing I could find would be to serialize and deserialize all the results into new objects, which I think would work??  But seems to complex, there should be a simpler solution.

By Robin J Giltner - 11/6/2007

What about copying the underlying datatable out to a local datatable and manipulating that ?

myBusinessObject.CurrentDataTable or myBusinessObject.CurrentView.toTable

If you need to respect any filters/sorting etc.  Then you could reference columns in the datatable with

loDT.CurrentRow.Item(ColumnIndex) or loDT.CurrentRow.Item("fieldname")

You could change, update whatever you need, and if you wanted to put the data back into the database, simply fire up a CopyDataFrom method on the originating Business Object.

<sorry if the code isn't 100%, trying from memory>

Robin Giltner

By Jason Seidell - 11/6/2007

That would solve sorta half my problem. Let me elaborate.



Let's says I have an object myBO, it has pulled 10 records from a database. I want to be able to create another object BOentry and set it to an instance of the first record in myBO. Then I may play around with myBO moving back and forth looking at other records but leaving BOentry still pointing to the first record. So here's some imaginary code



' Create a business object

myBO = New SampleBO

myBO.Fill10()



' Now get an instance to the first record

Dim BOentry as SampleBO

BOentry = myBO



Call FindNextEntry(myBO) ' Some other function that may navigate the myBO



BOentry = ??? ' At this point I have no clue what BOentry is pointing to since BOentry is simply a shallow copy (only pointing to myBO, and not a specific instance (or row) of information within myBO) if FindNextEntry called a MoveLast then BOentry is now pointing to the last record in myBO not the first. The simpliest solution is to be able to create a new SampleBO object that truly contains an indepedent instance of the object. For example, lets see it with a collection instead



myColl = New Collection()

FillCollection(myColl) ' Add the rows to my collection



' Create a new object

Dim CollEntry as SampleObject()

CollEntry = myColl.Item(1)



Call FindNextEntry(myColl) ' Some other function that may play around with the collection



CollEntry = myColl.Item(1) ' CollEntry is still pointing to the same instance of the object
By Robin J Giltner - 11/6/2007

Would simply copying the data from myBO to your BOEntry suffice ?

Dim loBusinessObject as new BusinessObjectType

loBusinessObject.CopyDataFrom(myBO, Microfour.Strataframe.Business.BusinessCloneDataType.ClearandFillfromDefaulView)

would get you a complete copy of the original business object's data as a completely different instance of the BusinessObject type.

I might be misunderstanding what you are needing here. (also complete guess on the ClearandFillfromDefaulValue enum, but I think that is close)

Robin Giltner

By Peter Jones - 11/6/2007

Hi Jason,

This may help (or not). I recently wanted various versions on the same BO but populated differently and all versions to be available simultaneously so I built a class that contained various infor (including the BO's data table) and stuck these classes into a sorted list. In summary:

In the main form's global area define a sorted list:
   
Dim slALU As New SortedList

----------

Add a new ALULookup class to the sorted list as needed and passing in the a BO that contains the data I have at this point in time:

slALU.Add(Me.BoGRS1.CurrentRowIndex, New clsALULookup(BO, CType(Me.BoGRS1.GRS_ATTType, Integer), Me.BoGRS1.GRSID.Value))

----------

The ALULookup class has the following properties. Note that one is the Data Table from the BO that is passed in:

Public Class clsALULookup

    ' A class to provide ALU lookup data for a specific Score cell in a grading screen.

    Private _dv As Data.DataView = New System.Data.DataView
    Private _dt As Data.DataTable = New System.Data.DataTable
    Private _LongestCode As Integer = 0
    Private _ShortestCode As Integer = 0
    Private _DefGUID As String = ""
    Private _DefCode As String = ""
    Private _DefName As String = ""

-----------

Then I can extract the specific class I want from the sorted list as and when I need it and use its properties:

Dim clsALU As clsALULookup = CType(slALU.Item(View.FocusedRowHandle), clsALULookup)

If Not clsALU Is Nothing AndAlso clsALU.dv.Count >= 1 Then

 clsALU.dv.RowFilter = "PK = '" & CType(View.GetRowCellValue(View.FocusedRowHandle, "GRS_Score"), String) & "'"

 If CType(clsALU.dv.Item(0)("ALUReason"), Boolean) Then....

  DefaultName = clsALU.DefName .....
-----------

Not strongly typed of course but, in my application, clsALU can contain different 'types' of BO.

Cheers, Peter

By Jason Seidell - 11/6/2007

BigGrinThanks for the replies, definetly different ideas than I orginally had, but great nonetheless. I will look further into each of these ideas and see where they take me.BigGrin
By StrataFrame Team - 11/7/2007

You could place the business object within a BusinessBindingSource, which will return separate instances of the business object, each positioned to the appropriate record (the BBS acts like an IBindingListView, which is itself an IList, so you can use it like a collection).  This would allow you to access the business object without worrying about the record changing each time you access a different object.  The added benefit is that changes to these objects can still be propagated back to the database.
By Jason Seidell - 11/8/2007

Sweet!!! That is exactly what I needed!!! I knew there had to be a simpler solution.



Your awesomeWink