Sunday, June 24, 2012

ISD Code Customization Tips, Part I: Post-Transaction Customization


There are two ways to extend ISD-generated base methods:
  1. Call the base method, and insert custom code before or after the call.
  2. Cut and paste base method's code, and modify the pasted code.
ISD makes it easier to use the second approach, because the cut-and-paste is done automatically. From code maintenance point of view, however, the first approach is preferred. This is because, 
  1. Custom code is not mixed with ISD-generated code, making it easier to understand and maintain.
  2. Any changes in the base method are automatically propagated to the override method in the first approach. While in the second approach, you have to redo the customization. It is VERY easy to forget to redo the customization, and result in bugs.
However, it is not always possible to go with the first approach. Sometimes you have to insert your custom code into the middle of ISD code. For some of those cases related to transactions, I have a few tips to offer.

Tip #1: Post-Transaction Customization

It is quite common to add post-transaction customization. For example, you want to send out email notifications only if data is saved into database successfully. You cannot insert your code after calling SaveButton_Click_Base like this, You may think that you have to cut-and-paste all code from SaveButton_Click_Base, and then insert your code right after this.CommitTransaction(sender). Here is the tip. You can avoid cut-and-paste by overriding CommitTransaction method.


Compare and see how the 2nd example greatly simplify the customization, making it much easier to maintain.