Detecting DataRow Changes from a Partial Class
Let's say I have an interface that represents a domain object:
public interface IFoo
{
Bar Bar { get; }
}
A type Bar
is a family of objects, each with a slightly different schema. In the database, this is represented as XML (this is a mobile app, so just nvarchar
, not ) a true XML column).
I have a generated DataSet
one that contains FooDataTable
and the relevant FooRow
objects:
// AcmeDataSet.designer.cs
public partial class FooRow : DataRow
{
public string BarXml
{
// Generated get and set
}
}
I would like to implement IFoo
and cache a deserialized instance Bar
:
// FooRow.cs
public partial class FooRow : IFoo
{
private Bar _bar;
Bar IFoo.Bar
{
get { return _bar; }
}
}
How can I tell from within a partial class that the value BarXml
has changed?
I understand what FooDataTable
contains events ColumnChanging
and ColumnChanged
, but I don't know how to subscribe to them. There is no Linq to Sql equivalent of a OnCreated
partial method, and I don't know of any other way to bind to the generated constructor.
source to share
If you understand correctly, then your FooRow object (instance) is an instance of some type that is inferred from the DataRow type ... If so, then since the DataRow has a property that refers to the DataTable, just pass the IFoo variable to the DataRow.
Inside FooRow
public partial class FooRow
{
private void myColumnChanging Handler(object sender, EventArgs e)
{
// Implementation
}
private void myColumnChanged Handler(object sender, EventArgs e)
{
// Implementation
}
public void RegisterEvents()
{
((DataRow)this).Table.ColumnChanging += myColumnChanging;
((DataRow)this).Table.ColumnChanged += myColumnChanged;
}
}
Then in the FooDataTable class add a new factory method, MyNewFooRow ()
public class FooDataTable
{
// -- Other implementation
public FooRow MyNewFooRow()
{
FooRow fr = this.NewFooRow(); // call the original factory
fr.RegisterEvents();
return fr;
}
}
and use this new factory wherever you used the old ...
source to share