43.4. Advanced Techniques

Thus far, this chapter has covered how to display and visualize objects you are debugging. In earlier chapters you learned how to modify field and property values on the object being debugged via the datatip. The missing link is being able to edit more complex data objects. The final section in this chapter looks at how to extend your visualizer so you can save changes to the Customer object.

43.4.1. Saving Changes to Your Object

When you created the CustomerVisualizer, you had to retrieve the Customer object from the communication stream using the GetObject method. This essentially gave you a clone of the Customer object being debugged to use with the visualizer. To save any changes you make in the CustomerVisualizer, you need to send the new Customer object back to the process being debugged. You can do this using the ReplaceObject method on the ObjectProvider, which gives you a CustomerVisualizer.

Before you can call the ReplaceObject method you will need to make some changes to pass the modified Customer object back to the visualizer. This has been done by saving the Customer object to an internal variable when it is initially passed into the class, and exposing this variable via a read-only property. This is shown in the following listing:

[Serializable()]
public partial class CustomerForm : Form
{
    public CustomerForm(Customer c)
    {
        InitializeComponent();

        this.txtCustomerId.Text = c.CustomerId.ToString();
        this.txtCustomerName.Text = c.CustomerName;
        this.dgOrders.DataSource = c.Orders;

        m_ModifiedCustomer = c;
    }

    private Customer m_ModifiedCustomer;
    public Customer ModifiedCustomer
    {
        get
        {
            m_ModifiedCustomer.CustomerId = new Guid(txtCustomerId.Text);
            m_ModifiedCustomer.CustomerName = txtCustomerName.Text;

m_ModifiedCustomer.Orders = (List<Order>)dgOrders.DataSource;
            return m_ModifiedCustomer;
        }
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
}

You can now easily access the modified Customer object and save the changes back by calling the ReplaceObject method as shown here:

[Serializable()]
[DebuggerDisplay("Customer {CustomerName} has {Orders.Count} orders")]
[DebuggerTypeProxy(GetType(Customer.CustomerProxy))]
[DebuggerVisualizer(GetType(Customer.CustomerVisualizer))]
public class Customer
{
    ...

    public class CustomerVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(
                            IDialogVisualizerService windowService,
                            IVisualizerObjectProvider objectProvider)
        {
            Customer c = (Customer)objectProvider.GetObject();
            CustomerForm cf = new CustomerForm(c);
            if (windowService.ShowDialog(cf) ==
                                       System.Windows.Forms.DialogResult.OK)
                    objectProvider.ReplaceObject(cf.ModifiedCustomer);
        }
    }
}

An alternate method would be to use DataBinding for all of the Customer fields on the form with a BindingSource object. This BindingSource object could be exposed with a public modifier, thereby making it accessible from the visualizer class. All that is needed then is to set the Customer object as the data source of this BindingSource object by the visualizer class, and it will automatically synchronize changes back to the original Customer object

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.37.136