StrataFrame Forum

Custom Fields on Business Object

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

By Tim Dol - 10/19/2007

Is it possilbe to create a custom field in a business object to store temporary data for each row without creating a actual field in the database.

I tried creating a custom field in the business object and a Private variable to store and retrieve the value but it appears to only keep the last value.

I'm open to suggestions if this isn't the correct approach.  Basically I want to end up with a place to store / retrieve information in the business object for each row without having to create an extra field in the table.

Thanks

Tim

By Edhy Rijo - 10/19/2007

Hi Tim,

Please take a look at the following topic in the SF help file "Custom Field Properties, Adding Custom Field Properties" it will show you how to do what you are looking for.

Good luck!

By Tim Dol - 10/19/2007

I've used custom field properties many times as described in the help, but these are read-only. I need the ability update the custom field as well, just like a normal field. Perhaps I'm missing something here but I can't seem to achieve the desired results with the read-only property.

Thanks

Tim

By Greg McGuffey - 10/19/2007

Tim,



Instead using a private variable, use a private dictionary, with the key being the PK and the value being whatever you want to store...so....



' Holds data for custom temp property.

' In this case, the PK is an integer and MyProp will hold a string.

Private _myProp As Dictionary(Of Integer, String)



' Of course, this would need the attributes to make a bindable property

Public Property MyProp As String

Get

' If dictionary hasn't been set yet, return a ZLS

If _myProp Is Nothing Then Return String.Empty



' Where PKField is the strong typed property that accesses the PK of the BO

Return _myProp(Me.PKField)

End Get

Set(value As String)

' check that the dictionary is initialized

If _myProp Is Nothing Then

_myProp = New Dictionary(Of Integer, String)

End If

_myProp.Add(Me.PKField,value)

End Set

End Property

By Tim Dol - 10/19/2007

Greg, this is perfect. Thanks a lot! Smile
By Greg McGuffey - 10/19/2007

Any time BigGrin
By Larry Caylor - 10/20/2007

Another method that I use to store the temporary custom fields in the BOs data table. I usually add the custom colums in the select statement that fills the BO. Once the colums have been added I use the custom field properties to maintian them.

<Browsable(False), _
BusinessFieldDisplayInEditor(), _|
Description(
"Custom field"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Property cust_Field() As String
  
Get
       
Return Me.CurrentRow.Item("CustomField").ToString
  
End Get
  
Set(ByVal value As String)
      
Me.CurrentRow.Item("CustomField") = value
  
End Set
End Property

Since the BO uses the AllFieldsNames property when saving or updating the object, the added fields are simply ignored.

-Larry

 

By Tim Dol - 11/1/2007

I'm trying to use these properties on grids so I added the custom bindable property descriptions but when I try to update I get an error indicating that the field isn't part of the business object.

Are these properties supposed to be bindable?

Thanks

TIm

By Greg McGuffey - 11/2/2007

I'm wondering if the problem is that you are using a grid. I'm thinking out loud here...normal SF data binding is controlled by the BO but binding to a grid is through a BusinessBindingSource to the DataTable, so that might be the problem.



If the extra data is temporary and you don't need to persist it, then the dictionary method would likely work better. If you do need to persist it, then it looks like it could get complicated. Sorry this isn't a more definitive answer Unsure I hope this helps you keep moving on the problem though Smile


By Paul Chase - 11/2/2007

Gregg is probably on the right track.

I had quite a bit of trouble using the BBS and ended up making my own class for that. 

Basically what happens with a BBS is for every row in the business object that you are binding to, a new instance of the business object is created and represent that row and its datatable is shared with the first main bo.

The problem is that if you have any properties , events etc set on the main bo the lose they're values in the newly created instance. hope that makes sense

However I would think because the datatable is shared you should be able to make it work somehow.