Wednesday, April 09, 2008

Javascript generator

Many of us have written JavaScripts and have always wished, if it could be written better like programming in C#. Interestingly the other day I saw a plug for VS.NET called Script# that actually generates the Javascript from a code written in C#.

Tuesday, July 17, 2007

Predicates in .NET 2.0

I am not sure how many developers have used Predicates in .NET 2.0. Its a new generic type. When I first heard of it I tried finding it in some of the books and hardly any of the books mentioned about it and mostly it's been ignored. So I wrote some code to figure it out.
The task is I have a list of integers. I want to find all the numbers greater than 5 and copy it into another list without writing any looping code.
// declaring the predicate.
Predicate isGreater = new Predicate(pr.checkGreaterThanFive);
// passing the delegate to the findall method of the list
lst = anotherList.FindAll(isGreater);
that's it and your list is populated.
well, you need this method
public bool checkGreaterThanFive(int value)
{
if (value > 5)
return true;
else
return false;
}
Writing this method is boring so you can avoid it by creating anonymous method which makes your code smaller since you dont have to declare the Predicate nor write the method to call. Everything done in only one line of code.
lst = anotherList.FindAll(delegate(int value) { return value > 5; });

Here is the complete code
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace PredConsole
{
class Program
{
static void Main(string[] args)
{
Program pr = new Program();
// Two empty list
List lst = new List();
List anotherList = new List();
anotherList.Add(1);
anotherList.Add(2);
anotherList.Add(3);
anotherList.Add(4);
anotherList.Add(5);
anotherList.Add(6);
anotherList.Add(7);
anotherList.Add(8);
anotherList.Add(9);
anotherList.Add(10);
anotherList.Add(11);
Predicate isGreater = new Predicate(pr.checkGreaterThanFive);
// With the created predicate
lst = anotherList.FindAll(isGreater);
pr.printResponse(lst);
lst = null;
// Using Anonymous methods is easier to write as well as understand.
lst = anotherList.FindAll(delegate(int value) { return value > 5; });
pr.printResponse(lst);
}
public bool checkGreaterThanFive(int value)
{
if (value > 5)
return true;
else
return false;
}
public void printResponse(List lst)
{
foreach (int iCount in lst)
{
Console.WriteLine(iCount);
}
Console.ReadLine();
}
}
}

Thursday, December 29, 2005

Batch Config file

Batch Config fileThe new <location> element in .NET 2.0 lets you define the settings for specific location. That includes subdirectories in web applications.

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">;<system.web><!-- Regularconfiguration settings go here. --></system.web><location path="/MySubDirectory"><system.web><!-- Configuration settings for MySubDirectory go here. --></system.web></location></configuration>

Would you really need it ? My preferred method is have it separated into individual config file.

But is has an interesting attribute allowOverride="false".

<location allowOverride="false" >

This would prevent overrides in the config files at lower level and this is useful for system administrators when they would like to retain certain policies throughout and not allow it to be overridden by some application.

Wednesday, December 28, 2005

Validating in Groups

In ASP.NET 2.0 has a new feature called Validation Groups.

If supposing you have two different sections and each of the section needs to be validated separately then Validation Groups is an interesting concept.
Each control can be tied to a specific group by using the ValidationGroup property in the controls.

Here is a simple example in ASP.NET 2.0.
Copy it to an aspx page and run it.

<form id="form1" runat="server"><div><asp:Panel ID="Panel1" runat="server"><asp:TextBox ID="TextBox1" ValidationGroup="FirstGroup" runat="server" /><asp:RequiredFieldValidator ID="RequiredFieldValidator1"ErrorMessage="First Group Validated" ValidationGroup="FirstGroup"runat="server" ControlToValidate="TextBox1" /><asp:Button ID="Button1" Text="Validate Group1"ValidationGroup="FirstGroup" runat="server" /></asp:Panel><br /><asp:Panel Height="94px" ID="Panel2" runat="server" Width="125px"><asp:TextBox ID="TextBox2" ValidationGroup="SecondGroup"runat="server" /><asp:RequiredFieldValidator ID="RequiredFieldValidator2"ErrorMessage="Second Group Validated" ValidationGroup="SecondGroup"ControlToValidate="TextBox2" runat="server" /><asp:Button ID="Button2" Text="Validate Second Group"ValidationGroup="SecondGroup" runat="server" /></asp:Panel></div></form>


Wednesday, December 07, 2005

Exception -al issues in .NET

Quite often I have seen "Object reference not set" error in .net applications. The error is not a developer intended error its just the basic understanding of exceptions not being clear. Whenever an exception is thrown deep in the code the developer intends to highlight this exception and hence re-throws the exception. In .net when you re-throw an exception it creates a new exception on the stack and hence this is the "Object reference not set" a very misleading error.
Therefore instead of doing this

throw new exception("Message")

do only this

throw

Hence the right exception info is obtained when the exception is handled at the interface level. Most large projects should develop their own exception handling mechanism and not depend on this.

Tuesday, October 25, 2005

Technology AND Process in Software Development.

Most people think technology solves all issues not realizing importance of process. I am often a believer that the process should be in place before adopting the technology. Sometimes software teams try to fit new technologies with older process. This further adds to complex and error prone process and hence wasted efforts. Therefore it’s of utmost important to define a new process when a new technology is planned. Unfortunately this would mean more time needed from the Architects to establish such a mechanism since they cannot use an older process. But more than not utilizing the older process it also is an issue of convincing the developers and management more convincingly. This is like saying hurricane Wilma would be a two or a three either way you need to be prepared for it.

Monday, October 10, 2005

Custom Session state in .NET

In .NET 1.1 session state could be inProc, on a state server or in sql server. What if we want to store the session in Oracle DB or any other storage location? We have to extend the session state functionality. The session state was handled by an httpModule. Unfortunately in 1.1 the session state class was not extendable unless a wrapper was created around it that accessed the methods within the session state class via using reflections. This is a complicated task and can be done. Good news with 2.0 is that there is an interface called SessionStateProviderBase that can be used to create the custom session state.

Saturday, October 01, 2005

Extracting contents from MSI ( Windows Installers )

I needed to extract contents of the MSI without installing it. Interestingly ORCA would show the contents but its not easy to extract the binaries from the MSI. The solution was to use WIX (To download WIX click here) . After you have copied WIX on to your machine. Run the following of the commandline

Dark.exe MyNewInstaller.MSI SomeFileName.XML /x c:\MyFolder

The SomeFileName.xml can be ignored. It contains the complete XML mapping of your msi. /x is used to indicate the path where the content needs to be extracted.

Wednesday, August 31, 2005

X - Factor in SOA

Read my new article on the X - Factor in SOA

Thursday, August 25, 2005

Software Patent

I never liked much of the software patenting. Software patenting on a product that can be created with the existing tools or simply even clever ideas does not augur well for the software community and future of unchallenged growth in software technologies. Here is an interesting article on it.