Great new feature in .NET 2.0 - Partial class, can make huge problems sometimes.
You are able to add partial class to typed dataset and to set your own methods, event hamdlers, etc. Particulary problem I work on is that for some reason event handlers are not passed over web services. Here is a scenario:
In typed dataset I want to update one field (Field2) in datatable when other field(Field1) is changed. I also want this to happend when user changes something, not in a moment when typed dataset is filled with data.
So, at the end of FillData method, which fills data into typed dataset I write AddHandler (VB.NET) to attach event handler on event raising in a moment when Field1 is changed. This approach works fine on Front End and value in Field2 is updated every time user changed Field1.
Similar scenario should happend on back end when system (in this case COM+ component) changes value in Field1. Field2 value is not changed. Why??
I started with investigation, write some log messages in event log and realase for some reason that event handlers are not triggered becase they don't exists. How is this possbile? In a moment when web service proxy class calls web service method event handlers are in typed data set. A second later they don't exists in back end. ??
After some time I realise what is a problem. Event handlers are not passed to back end system because they are added programmatically in a run time, not in design time. Yes, it is a true. It is hard to belive but it is a true.
When you add event hadnler programmatically (with AddHandler instruction) it is added to your class (entity, business object, whatever) but it is placed into separate place in local memory. This memory location is not contacted when you pass typed dataset to back end.
If you add event handler in design time (with Handles instruction) event handler is placed into some memory location but type dataset has a reference to it during its lifecycle. Strange, but it is true.
Solution could be found with some local variable of boolean type. You set it to false as a first line of your fill typed dataset method and in event handlers you check is this variable true or not. At the end of fill method set variable to tru and everything is fine. Your code works.
The same scenario applies to system dataset since they also have partial class feature.
Example (some sort of pseudo code) - won't work on back end:
Private sub FillData()
'get data from back end
'fill data
Addhandler typedDataSet.Tables("SomeTable").RowChanged, AddressOf SomeEventHandler
End Sub
Private Sub SomeEventHandler(sender as Object, e as RowChangedEventArgs)
End Sub
How it should be implemented:
private loading as Boolean = false
Private sub FillData()
loading = false
'get data from back end
'fill data
loading = true
End Sub
Private Sub SomeEventHandler(sender as object, e as RowChangedEventArgs) Handles typedDataSet.Tables("SomeTable").RowChanged
If loading
'do something
End If
End Sub
Cheers, mates