StrataFrame Forum

Custom field with storage

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

By Chan - 6/15/2007

Hi,

Anyway I can create custom BO field that able to store value without using sql server view?



Thank you
By Greg McGuffey - 6/15/2007

Chan,



If I understand you, you are asking if you can store something to the database, but not using the underlying DataTable that the BO uses within a custom property. If this is what you mean...



I've done this many times. In the set statement of the property, just use the ExecuteNonQuery() of the BO to execute any command on db that is needed. In the get statement of the property, use ExecuteScalar() (most likely) to get the value.



Here's a quick example (in VB):



Public Property ValueNotInBO() As String

Get

' if no data is returned from the db, return an empty string

Dim value As String = String.Empty

Using cmd As New SqlCommand()

' Here I'm assuming the PK of the BO, called bo_pk, is also the pk of TableNotUsedByBO

cmd.CommandText = "Select ValueNotInBO From TableNotUsedByBO Where pk = @pk"

cmd.Parameters.Add("@pk",Int)

cmd.Parameters("@pk").Value = Me.bo_pk

' It is possible that there is no match, in which case ret will be nothing

' It might also be possible that ret is NULL (based on db schema)

Dim ret As Object = Me.ExecuteScalar(cmd)

If ret IsNot Nothing AndAlso ret IsNot DBNull.Value Then

value = CType(ret,String)

End If

End Using

End Get

Set(value As String)

' In real code, you'd have to check if an insert or update was needed

' Here I'm just doing the insert.

Using cmd As New SqlCommand()

cmd.CommandText = "Insert Into TableNotUsedByBO (pk,ValueNotInBO) Value (@pk,@value)"

' Here I'm assuming the PK of the BO, called bo_pk, is also the pk of TableNotUsedByBO

cmd.Parameters.Add("@value",Int)

cmd.Parameters("@value").Value = value

cmd.Parameters.Add("@pk",Int)

cmd.Parameters("@pk").Value = Me.bo_pk

Dim ret As Object = Me.ExecuteScalar(cmd)

If ret IsNot Nothing AndAlso ret IsNot DBNull.Value Then

value = CType(ret,String)

End If

End Using

End Set

End Property




Hope this helps!
By Chan - 6/15/2007

Hi,

Thank you for reply.

Currently, i am using as you mentioned to retrieve custom field value. I found that, this property Get{} event always fired while refreshing. I am looking for anyway to prevent overload.



Any ideas?



Thank you
By StrataFrame Team - 6/18/2007

Unfortunately, there's no way to prevent the get {} from firing while refreshing.  .NET's data binding works in such a way that if any bound property on a data source raises its xChanged event, all controls bound to the same context refresh as well.  So, if any control's data changes, all bound controls will refresh.

My recommendation would be to add an "initialized" flag to your business object and only retrieve the value if the field has not been initialized.  Something like this:

private bool _propInitialized = false;
private string _customPropField;
public string MyCustomProp
{
    get
    {
        if (!this._propInitialized)
        {
            this._customPropField = this.GetCustomProp();
            this._propInitialized = true;
        }
    }
}