Wednesday, July 30, 2008

ASP.net Performance tuning

Excellent list of 25 Resources for Tuning Your .NET Application Performance ” :

http://effectize.com/23-resources-tuning-your-net-application-performance

One that I liked the most from the list is -

- Speed Up Your Site! 8 ASP.NET Performance Tips (By Jeff Atwood and Jon Galloway )

It covers many interesting topics like :

  • Tracing
  • Compressing the ViewState
  • Storing the ViewState on Server
  • HTTP Compression
  • OutputCache & SQL Cache Dependency etc

Learning is Inevitable . . . .

Sunday, July 27, 2008

using ValidatorHookupControlID() and ValidatorTrim() functions in asp.net Validators

Scenario
Lets imagine a scenario where we have 2 input boxes and we need to validate that atleast 1 of them should contain some value.

image

Solution
Asp.net CustomValidator comes handy here. Here is the code which would help us validate this simple scenario:

image

All works fine except that the validation occurs on the server side. What if we also want this validation from the client side ?

Its simple, create a client side function to validate the same scenario and specify the name of that function in the ‘ClientValidationFunction’ property of the CustomValidator.

image
<asp:CustomValidator ID="validateAddress" runat="server"
ErrorMessage="Please enter atleast 1 Address"
ClientValidationFunction="validateAtLeastOneAddressHasData"
OnServerValidate="validateAddress_ServerValidate1">
</asp:CustomValidator>

This enables the client side validation but there are still 2 issues with the above code :
1) There is no trimming happening in the client side validation code
2) The client side validation is fired only on the button click event.

For trimming there is a function provided by the asp.net validation scripts named ‘ValidatorTrim’ which is kind of hidden and only available once you start the page. This is how we can use it :

textBox1Value = ValidatorTrim(textBox1Value);
textBox2Value = ValidatorTrim(textBox2Value);

Now for the second issue, ideally the client side validation script should be fired on the onblur() event of any of the textboxes. To do that we would have to hookup the textboxes with the CustomValidator.

Hooking the first textbox is simple. Just specify it in the ControlToValidate property and set the ‘ValidateEmptyText’ property of the CustomValidator to true.

<asp:CustomValidator ID="validateAddress" runat="server"
ErrorMessage="Please enter atleast 1 Address"
ClientValidationFunction="validateAtLeastOneAddressHasData"
OnServerValidate="validateAddress_ServerValidate1"
ControlToValidate="txtAddress1"
ValidateEmptyText="True" >
</asp:CustomValidator>

For hooking the second textbox we would have to use a startup script function 'ValidatorHookupControlID as below :

<script type="text/javascript">

ValidatorHookupControlID ( '<% = txtAddress2.ClientID %>',
document.getElementById('<% =validateAddress.ClientID %>')
);
</script>

Wednesday, July 23, 2008

Excellent Whitepaper on : ‘Tools of Agility’

Recently I came across this brief but excellent whitepaper written by Kent Back on Tools of Agility.

I found these two diagrams from the whitepaper very interesting as they clearly explain,
- how the process flow differs in agile practise
- how the different tools are interwoven and support the agile practice


Waterfall

image

Agile

image


Tools to support Agility

image

Learning is Inevitable ! ! !

Tuesday, July 22, 2008

‘Foundations of Programming’ – Karl Seguin


Before few weeks Karl Seguin released an excellent free ebook titled 'Foundations of Programming' which covered many interesting topics focused more around design and coding fundamentals rather than API and framework details like :
- Design Principles
- Domain Driven Design
- Dependency Injection
- ORM's
- Why Unit Testing & Mocking ?
and some really interesting 'Back to Basics' stuff.
And now he also released the 'Foundations of Programming - Learning Application'.

Worth reading material for all . . . .

Thursday, July 17, 2008

Back to Basics - Page_Load & OnPreRender events with nested Web Controls

While working on some refactorings in my recent project I came across a piece of what I would like to call ‘ignorant code’*.

I have tried to get the gist of the scenario with the below diagram :


scenario


Its a pretty basic scenario where we have multiple web controls clubbed together in a single control. (Although I not a huge fan of nested web controls)

What’s important to consider here is the use of Page_Load and OnPreRender events in the Parent and Child web controls.

Probably we all know that Page_Load always occurs before the OnPreRender but the OnPreRender only occurs if the ‘Visible’ property of the web control is set to true.

So in the above scenario although the controls were made visible according to the page, the Page_Load event of all the 7 sub web controls was called everytime. The solution to this is to simply move the hefty code from Page_Load to the OnPreRender event. As the rendering would only occur if the control is visible it would only execute the hefty code on the relevant pages.

*ignorant Code : No code is crap code or bad code its just that we do not know a better way to do it at that point of time

Tuesday, July 15, 2008

Private & Public behaviours with Explicit interface member implementations


Have a look at the below code :

InterfaceCode

Here we are using the Explicit interface member implementation to differentiate the methods.

Now if we try to access these methods from an instance variable. . .

privateIntellense

We can’t !!! Because there is no access modifier specified in the implementation and hence the methods are treated as ‘private’.

So we try specifying the ‘public’ access modifier for these methods but that too generates a compiler error in visual studio !!!

So then how do we actually use any of those methods . . . .

privateIntellenseDone

So what’s confusing here is that although the methods were private they are behaving like public.

ilcode

Finally I managed to find the answer to this behaviour. . . .

Here is a good explanation of this and how the IL code for implicit & explicit Interface Contracts.

AddIn