StrataFrame Forum

runtime business object creation

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

By Fabian R Silva, - - 9/24/2007

Hi guys, I try to make a generic ABM, on it I dropped a "copy" button and it must copy the current record of the primary business object on a private business object



example: I'm retrieving on the ABM a product's table, my currentrowindex is 23 and I like to copy this on a temporary business object with only this record, I not sure how to declare the private variable



private _boCopy as XXXXXX



XXXXX mean that I known what is the primary object (the form property say it) but I don't known how to say something like

dim _boCopy as (the type of Myform.PrimaryBusinessObject)



Thanks and sorry for me newbie questions w00t
By Fabian R Silva, - - 9/24/2007

I not sure if I clear on the last post, I try to make something like the VFP "scat memo" / appe blank / "gath memo" to copy a record into another new record, I not sure if a temporal 1-row business object is the correct stuff to do
By Trent L. Taylor - 9/24/2007

Well, a business object has a CopyDataFrom method that allows data from one BO to be copied to another.  This also has some overloads and parms that allow you to specify whether you want to copy the filtered BO (DefaultView) or the CurrentDataTable which ignores the filter.  So if you want to copy a single record, apply a filter to the source BO then call the CopyDataFrom on the destination BO using a DefaultView option.

SourceBO.Filter = "MyPK = " & SourceBO.MyPk.ToString()
DestBO.CopyDataFrom(SourceBO,ClearAndFillFromDefaultView)
By Fabian R Silva, - - 9/25/2007

thanks for reply, my question I think is more a .net question about object types, I don't sure how to define the destination object in run time without known what type of BO it is



Example:I have a me.PrimaryBusinessObject, when I press a "Copy" button I handle it....



Sub Copy_Click

' I like to copy the current record on the me.Primarybusinessobject to a variable

' called _CopyBO (it can be testbo1, otherBo1 or another BO)

dim _CopyBO as XXXXXXX '<--- how I declare the type of the primarybusinessobject here?

'Copy the row from XXXBO1 to _CopyNO....

' some code here....

end sub



--------------------

Thanks Smile
By Trent L. Taylor - 9/25/2007

You will want to use the base class which would be BusinessLayer:

Dim _CopyBO As MicroFour.StrataFrame.Business.BusinessLayer

This will allow any BO to be passed into that variable while giving you the ability to reference all of the base classes.  If you want to have the type of BO passed in then you will want to take a more technical approach using Generics.  This is a more length explanation as Generics are a wonderful tool but they require more explanation.

By Fabian R Silva, - - 9/25/2007

Trent L. Taylor (09/24/2007)
Well, a business object has a CopyDataFrom method that allows data from one BO to be copied to another. This also has some overloads and parms that allow you to specify whether you want to copy the filtered BO (DefaultView) or the CurrentDataTable which ignores the filter. So if you want to copy a single record, apply a filter to the source BO then call the CopyDataFrom on the destination BO using a DefaultView option.



SourceBO.Filter = "MyPK = " & SourceBO.MyPk.ToString()

DestBO.CopyDataFrom(SourceBO,ClearAndFillFromDefaultView)




how I can say something like "MyPK = " & SourceBO.MyPK.Tostring() in a general manner to filter the current record? I say something like:



me.primarybusinessObject.filter = me.primarybusinessObject.PrimaryKeyField.Tostring() & "=" & me.primarybusinessobject.(me.PrimaryBusinessObject.PrimaryKeyField.tostring())



Thanks and sorry for all the questions w00t
By StrataFrame Team - 9/26/2007

Yes, Fabian, that will work, but you'll need to make sure that you remove the filter when you're done or you won't be able to Navigate or Move to any new records.
By Fabian R Silva, - - 9/26/2007

How To obtain the current record from the PrimaryBusinessObject of a Form



in the last post I try to obtain the current record but I don't known how to set the filter



I try with



me.primarybusinessObject.filter = me.primarybusinessObject.PrimaryKeyField.Tostring() & "=" & me.primarybusinessobject.(me.PrimaryBusinessObject.PrimaryKeyField.tostring())



but this doesn't work, after some googling I see that I can use:





Me.PrimaryBusinessObject.Filter = Me.PrimaryBusinessObject.PrimaryKeyField & " = " & Me.PrimaryBusinessObject(Me.PrimaryBusinessObject.PrimaryKeyField)





Sorry for all those questions and thanks for all Smile
By Fabian R Silva, - - 9/26/2007

I say "Victory" too fast



when I set

Private _CopyBO As MicroFour.StrataFrame.Business.BusinessLayer



when I try to use _CopyBO, VB.net say that I have to instantiate the class



then, I set

Private _CopyBO As New MicroFour.StrataFrame.Business.BusinessLayer



When I try to use

_CopyBO.CopyDataFrom(Me.PrimaryBusinessObject, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable)



a message "The class "Microfour...BussinesLayer" must override the property "Tablename" since it derives from Microf.....Businesslayer





What Can I do?



My "full" code:

--------------

Private _RegistroEnMemoriaBO As New MicroFour.StrataFrame.Business.BusinessLayer

Dim sFiltroAnterior As String = Me.PrimaryBusinessObject.Filter

Dim _CurrentRowIndex As Integer = Me.PrimaryBusinessObject.CurrentRowIndex



Me.PrimaryBusinessObject.Filter = Me.PrimaryBusinessObject.PrimaryKeyField & " = " & Me.PrimaryBusinessObject(Me.PrimaryBusinessObject.PrimaryKeyField)



'-- at this line the "tablename" error ocurrs

_RegistroEnMemoriaBO.CopyDataFrom(Me.PrimaryBusinessObject, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable)



Me.PrimaryBusinessObject.Filter = sFiltroAnterior

Me.NavigateToPrimaryKey(_RegistroEnMemoriaBO(_RegistroEnMemoriaBO.PrimaryKeyField))



Thanks!
By Trent L. Taylor - 9/27/2007

Have you built the partial class for the BO you are working with?
By Fabian R Silva, - - 9/28/2007

Yes, I build all the project to test it, I tested with some BOs and same error....

I have an CRUD working, when I press the "copy" button with the last post code, the message appears:



NotImplementedException

The class [MicroFour.StrataFrame.Business.BusinessLayer] must override the property 'TableName' since it derives from MicroFour.StrataFrame.Business.BusinessLayer.



Source : MicroFour StrataFrame Business



Stack Trace:

en MicroFour.StrataFrame.Business.BusinessLayer.get_TableName()

en MicroFour.StrataFrame.Business.BusinessLayer.CreateInternalTable()

en MicroFour.StrataFrame.Business.BusinessLayer.get__CurrentDataTable(Boolean IsSharedTable)

en MicroFour.StrataFrame.Business.BusinessLayer.ChangeCurrentDataTable(DataTable NewTable, Boolean AcceptChanges, Boolean IsSharedTable)

en MicroFour.StrataFrame.Business.BusinessLayer.CopyDataFrom(BusinessLayerBase BusinessObject, BusinessCloneDataType CopyType)

en Integra4.SFStandardFormBaseABM.CopiarRegistro() en C:et\SISTEMA\integra4\Clases Base\formularios\SFStandardFormBaseABM.vb:línea 157

en Integra4.SFStandardFormBaseABM.tsbCopiar_Click(Object sender, EventArgs e) en C:et\SISTEMA\integra4\Clases Base\formularios\SFStandardFormBaseABM.vb:línea 107

en System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)

en System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)

en System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)

en System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)

en System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)

en System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)

en System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)

en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

en System.Windows.Forms.Control.WndProc(Message& m)

en System.Windows.Forms.ScrollableControl.WndProc(Message& m)

en System.Windows.Forms.ToolStrip.WndProc(Message& m)

en System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)

en System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)

en System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)





I tested to declare _CopyBO as BusinessObject (ej: as TESTBO1) instead of "as bussinesslayer" and It worked partially...it copy the datatable..... but I have the properties of testBO1, not the actual me.PrimaryBusinessObject BusinessObject, the unique thing that I see that I can do is to declare _copyBO as [theCorrectTypeBO here], not other solution to copy the current row to call it back later like "me.primarybusinessobject.someFieldOnSqlProperty = _copyBO.someFieldOnSqlProperty"



I think that the best I can do to copy the actual record from some BO is to copy the current dataRow and restore it later... Any best idea? :/



Thanks for all
By Trent L. Taylor - 9/28/2007

Yes, this message is because the partial class has not been built.  When we create the partial class for the BO we automatically overwrite the TableName property, which on the BO you are referring does not have it overwritten so thus you get the error.  If you HAve built your partial class then something else is going on.  Can you post your partial class file here.  It will be the MyBo.Designer.vb file.
By Fabian R Silva, - - 10/1/2007

Trent L. Taylor (09/28/2007)
Yes, this message is because the partial class has not been built. When we create the partial class for the BO we automatically overwrite the TableName property, which on the BO you are referring does not have it overwritten so thus you get the error. If you HAve built your partial class then something else is going on. Can you post your partial class file here. It will be the MyBo.Designer.vb file.




I think that the problem is that I like to create the copy of a (unknown yet on design) new business object (I trying to create a generic CRUD) and can´t use the businesslayer to define the type of _CopyBO" because it don't override the tablename property :S I not sure how to avoid this problem and copy a single record from the primarybusinessobject into a temporal 1-row business object copy, I don´t known how I can declare the _copyBO as same type that

myform.primarybusinessObject, sometype like:



dim _copybo as gettype(me.primarybusinessobject) '<---- something like this exist on VB?



Thanks and sorry for my bad english and all my newbie questions



--------------------------------------------

I repeat the code that I posted before



My "full" code:

--------------

Private _CopyBO As New MicroFour.StrataFrame.Business.BusinessLayer

Dim sFiltroAnterior As String = Me.PrimaryBusinessObject.Filter

Dim _CurrentRowIndex As Integer = Me.PrimaryBusinessObject.CurrentRowIndex



Me.PrimaryBusinessObject.Filter = Me.PrimaryBusinessObject.PrimaryKeyField & " = " & Me.PrimaryBusinessObject(Me.PrimaryBusinessObject.PrimaryKeyField)



'-- at this line the "tablename" error ocurrs

_CopyBO.CopyDataFrom(Me.PrimaryBusinessObject, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable)



Me.PrimaryBusinessObject.Filter = sFiltroAnterior

Me.NavigateToPrimaryKey(_RegistroEnMemoriaBO(_RegistroEnMemoriaBO.PrimaryKeyField))
By Fabian R Silva, - - 10/1/2007

Solution to this topic (I think :crazySmile



dim _copybo as gettype(me.primarybusinessobject) '<---- something like this exist on VB?




I found the solution w00t I have to take some class on .net Blush



My "full" working code:

--------------

Private _CopyBO As MicroFour.StrataFrame.Business.BusinessLayer

Dim sOldFilter As String = Me.PrimaryBusinessObject.Filter

Dim _CurrentRowIndex As Integer = Me.PrimaryBusinessObject.CurrentRowIndex



Me.PrimaryBusinessObject.Filter = Me.PrimaryBusinessObject.PrimaryKeyField & " = " & Me.PrimaryBusinessObject(Me.PrimaryBusinessObject.PrimaryKeyField)



'Instantiate a object from the same type as the primaryBusinessObject

_CopyBO = Activator.CreateInstance(Me.PrimaryBusinessObject.GetType)



_CopyBO.CopyDataFrom(Me.PrimaryBusinessObject, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable)



Me.PrimaryBusinessObject.Filter = sOldFilter



'Me.NavigateToPrimaryKey(_RegistroEnMemoriaBO(_RegistroEnMemoriaBO.PrimaryKeyField))[/quote]



Now I have a _CopyBO with 1-row copy of my actual PrimaryBusinessObject Smile



Thanks and sorry for all the posts that I write for a simple topic (because my dificulties to write more understand posts)
By Greg McGuffey - 10/1/2007

Fabian,



I think that the code posted won't have just one record (unless the primary BO only has one record). When you do the CopyDataFrom, you need to supply the ClearAndFillFromDefaultView option to get just the filtered records.



Of course, if what you do have works, I'm not understanding what's happening BigGrin
By StrataFrame Team - 10/2/2007

Yep, Greg is correct... if you filter out all but 1 row, and you want to copy just that one row, then you will need to use the ClearAndFillFromDefaultView.  The other option (ClearAndFillFromCompleteTable) will ignore the filter you set on the business object.
By Fabian R Silva, - - 10/2/2007

Hello guys, that's right, some cut & paste to show the example and I make another mistake w00t I learn the clearandfillfrom... stuff thanks for all the help and support Smile