Information Technology Reference
In-Depth Information
does not work for reference types—it works only for value types. To fix
this, you need to replace the data in the current reference object, and
ensure that you do it in such a way that it can never throw an exception.
That is difficult, because it is two different atomic operations: removing all
the existing objects in the collection and adding all the new objects. You
might consider that the risk is small of removing and adding the new
items:
private BindingList < PayrollData > data;
public IBindingList MyCollection
{
get
{
return data;
}
}
public void UpdateData()
{
// Unreliable operation might fail:
var temp = UnreliableOperation();
// These operations will only happen if
// UnreliableOperation does not throw an
// exception.
data.Clear();
foreach ( var item in temp)
data.Add(item);
}
That is a reasonable, but not a bulletproof, solution. I mention it because
“reasonable” is often the bar you need. However, when you do need bul-
letproof, you need to do more work. The envelope-letter pattern will hide
the internal swapping in an object that enables you to make the swap safely.
The envelope-letter pattern hides the implementation (letter) inside a
wrapper (envelope) that you share with public clients of your code. In this
example, you'll create a class that wraps the collection and implements the
IBindingList<PayrollData>. That class contains the BindingList<PayrollData>
and exposes all its methods to class clients.
 
Search WWH ::




Custom Search