Monday, 27 July 2009

How to end the session when the browser is closed?

Approach 1: using window object
1. <body onunload="window.location.href='abandon.asp';">
2. Add abondon.asp page and put only this there: <% Session.Abondon %>

There is a problem with this method that if the user changes the url, it navigates back to the abondon.asp page.

Approach 2: using AJAX
1. <body onunload="bodyUnload();">
2.

function bodyUnload()
{
var xhr = GetXHR();
xhr.open("GET","Abondon.aspx",true);
xhr.send();
}
function GetXHR()
{
var xhr = null;
if(window.XMLHttpRequest)
{
//this object is standard in IE7, FF, Opera, Safari
xhr=new XMLHttpRequest();
}
else {
//IE 6.x and IE 5.x
xhr=new ActiveXObject('MSXML2.XMLHTTP.3.0');
}
return xhr;
}

3. Add abondon.asp page and put only this there: <% Session.Abondon %>

Approach 3: using Page Methods

1. <asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />

2. <body onunload="PageMethods.AbandonSession();">

3.

[WebMethod]
public static void AbandonSession()
{
HttpContext.Current.Session.Abandon();
}

Business Objects Web Services


Business Objects Web Services is a component of Business Objects Enterprise XI R2. Built for developers, it is composed of an implementation of web services that can be deployed with BusinessObjects Enterprise XI R2. This web service implementation provides an API/WSDL which simplifies the process of developing applications.

From your .NET application you can call your business object engine and retrieve report contents.

3 main assemblies you need to work with are:
  • BusinessObjects.DSWS.dll
  • BusinessObjects.DSWS.ReportEngine.dll
  • BusinessObjects.DSWS.Session.dll
For more info and code examples click here and here.

Saturday, 25 July 2009

Repository Factory

Looking for a tool which generates the data access layer for you? Download Repository Factory Guidance Package from here. Previously it was part of Web Service Software Factory but now it’s separated to be independent.

I used this tool for about 1.5 years for one of my applications which was developed by WSSF.

What can I generate with it?

Using this tool you can generate:

  • CRUD stored procedure for your Database Tables
  • Business Entity classes for your Tables
  • Data access layer (Repository classes) for your Business Entities: Repository Interfaces, Repository Classes and RepositoryFactory

If you need to create any new custom methods for your repositories, you can easily do so using partial classes in which way you can always benefit from generated codes by Repository Factory.

Repository Factory Pattern - Code Example:

// Repository Interface
public interface ICustomerRepository
{
Customer GetCustomersByCustomerId(System.Int32 customerId);
List<Customer> GetAllFromCustomers();
void Add(Customer customer);
void Remove(System.Int32 customerId);
void Save(Customer customer);
}

// Repository
public partial class CustomerRepository : Repository<Customer>, ICustomerRepository

{
public CustomerRepository(string databaseName) : base(databaseName) { }

public CustomerRepository() : base() { }

public List<City> GetAllFromCustomer() { }

public void Add(Customer customer) { }

public void Remove(System.Int32 customerId) { }

public void Save(Customer customer) { }

}

// Repository Factory
public class RepositoryFactory<T>
{
public T Create() { }
}

// How to use it
class Program
{
static void Main(string[] args)
{
ICustomerRepository repository =
RepositoryFactory.Create<ICustomerRepository>();

  Customer customer = repository.GetCustomersByCustomerId(1);
}
}



Screencasts:

Thursday, 23 July 2009

WebPage Performance Tracker

MySpace’s Performance Tracker is a browser plugin that help developers to improve their code performance by capturing and measuring possible bottlenecks on their web pages. It currently supports Internet Explorer 6 and up.

Monday, 20 July 2009

Windows Live Writer

Thanks to Sandeep who introduced Windows Live Writer to me to be able to add complex posts including code and html on blogspot and other blogging engines.

I think Google has another blog publishing tool as well.

Extracting the Extension of a Url Host

var host = this.Request.Url.Host;
private string ExtractExtension(string host)
{
string result = null;
var pattern = @"[\w-]+\.[\w-]+(?<extension>(\.[\w-]+)+)";
var match = Regex.Match(host , pattern);
if (match.Groups["extension"].Success)
{
result = match.Groups["extension"].Value;
}
return result;
}
Note: this method is easily testable.

Friday, 17 July 2009

The Art of Pair Programming

What is Pair Programming?

  • Pair Programming is a software development technique which is popularized by Extreme Programing (XP) methodology.
  • Pair Programming is an art which must be learnt.
How it works?

Two programmers sit beside each other and work on a single task. One writes the code (driver) and the other reviews the code written (observer) and they switch their role frequently e.g. every 40 min.

Observer's main responsibilities are:
  • Strategic direction of the overall code e.g. patterns, design quality, how methods fit into the class
  • Come up with improvement ideas such as refactoring
  • Continually observe the work of the driver and identify tactical defects such as syntactic, spelling, etc.
Coder's main responsibilities are:
  • Create the implemenation; focus on completing the tasks technically such as writing a method
  • Use the Observer's comments as a safety guide
Observer and Coder main responsibilities are:
  • Respect and listen to each other; empower and encourage each other.
  • If necessary, organize 1-2 meetings to discuss how they can work together and based on what principles
  • Participate in brainstorming
  • Refactor the codes frequently e.g. 15 minutes in every 1 hour of development
  • Prefer simplicity; avoid complexity or break it up. "Do The Simplest Thing That Could Possibly Work"
  • Avoid waste
Step By Step
  1. 2 programmers sit beside each other and explain why they do want to do pair programming and its benefits.
  2. Explain or review the responsibilities of each role (observer and coder)
  3. Obviously the first 2 steps are not required each time!
  4. Pick up a task which has a high priority
  5. Quickly both roles explain and clarify the task and problem. What is the problem? Why we want to resolve it? What is the root? Where has it happened?
  6. Quickly both roles come up with ideas how to resolve the problem. Brainstorming. Agree on the overall strategy in up to one page.
  7. Make sure that you write the overall strategy on a paper or on notepad. Don't start coding without strategy!
  8. The Coder starts coding and the Observer starts observing and both consider their responsibilities as explained.
  9. Rotate your roles every, say, 40 minutes
  10. When the task is done, be happy, enjoy the work and go back to step 4
Benefits?
1) Better Design and Quality
Since 2 programmers think together design becomes better, simpler and more maintainable in comparison with solo programming.

2) Fewer Bugs and Reduced Development Costs

3) Learning and Training
Knowledge is shared and passed between the programmers.

4) Reduced Management Risks
If one programmer leaves the team, there is less risk to management.

5) Improved Focus, Concentration, Discipline and Efficiency
Every one can easily loose focus, check personal emails, surfing the web, etc. With Pair Programming the focus is much better acheived, time wasting is reduced and as a result efficiency will be improved.

6) Difficult Tasks Can be Solved Easier and Quicker
Because 2 minds work obviously.

7) Developers Become More Socially Adjusted Creatures!
Yes, they have to come out of their comfort zone and think loudly!

Challenges?
1) Work preference
Some developers are not interested in working in pairs.

Solutions:
  • The benefits of pair programming should be discussed between the pairs
  • Pairs should come up with ideas how they can work together.
  • Gradually increasing the time of pair programming if it's felt that it works; otherwise changing the strategy or changing the pair
2) Intimidation
A less experienced developer may feel intimidated when working with a more experienced developer.

Solutions:
  • Experts might be better to be paired with experts and less-experienced with less-experienced; however
  • Quickly write the overall strategy of what the task is, how it should be implemented should be discussed, agreed and written before coding the task.
3) Tutoring costs
An experienced developer may find it tedious to tutor a less experienced one.

Solutions:
  • Individuals might be better to be paired with ones from their own level.
  • At the beginning Pair Programming may take time but as you learn how to do it, the performance will be increased as well.
4) Potential Conflicts
Developers at times may feel uncomfortable developing with a pair. The coding styles and habits are often different.
Solutions:
  • Use Company guidelines, standard and conding conventions; suggest improvement. Feel free to justify why you think what you think but at the same time be humble enough to accept a better idea if available.
  • Less egos
5) Development Cost
Some managers think the development costs are increased.
Solution:
  • Studies and tests have shown that the development costs are decreased and efficiency and productivity is improved from 15%-55%.
Where Not to Do Pair Programming?
  • Avoid Pair Programming for very simple tasks; now we should define what "very simple" mean?
More info:

Wednesday, 15 July 2009

Send a Dictionary through QueryString

Send the data:

../MyPage.aspx?filters={filtername,selectedValue},{filtername2,selectedvalue2}

Retrieve the data:

var filters = Request.QueryString["filters"];
var filtersDictionary = GetDictionary(filters);

private Dictionary GetDictionary(string filters)
{
var result = new Dictionary();

var matches = Regex.Matches(filters, "{(?.*?)}");

foreach (Match match in matches)
{
var keyValueArray = match.Groups["pair"].Value.Split(',');
result.Add(keyValueArray[0], keyValueArray[1]);
}

return result;
}

DOCTYPE

All about browser compatibility and html standards:

Markup Validation Service



Monday, 13 July 2009

Next Learning Path

My future learning paths:

  • .NET 4.0, C# 4.0
  • F#
  • More of Refactoring techniques, Design Patterns and Unti-Patterns
  • More of AJAX and asynchronous programming
  • More of ASP.NET WebParts
  • More of WCF and distributed systems
  • More of WPF & Silverlight
  • More of TDD; specially writing integration tests without using mocking tools
  • SQL Server 2008
  • More use of ready tools such as Telerik, ASP.NET Code Gallery, AJAX Control Toolkit, etc.

Concepts to read further:

  • Lean Principles
  • SOLID Principles

Wednesday, 8 July 2009

TFS Web Access

There are 2 main tools which enables web access in TFS: TFS WIWA and TFSWA

1) TFS WIWA:

TFS Work Item Web Access (WIWA) is a light version of TSWA which enables you to access TFS Work Items using a web access.

More details:

This tool is mainly for business analysts and project managers, whereas Team Explorer is a better option for developers and testers (it is installed in Visual Studio).

Limitations:


Costs:
WIWA is free to use and you don't need a CAL license.

2) TSWA:
Team System Web Access can be installed on your existing Team Foundation Server deployments at no additional charge.

Costs:
Each user who accesses the TeamPlain web portal must have purchased a TFS Client Access License (CAL) prior to use.

Page Transition in IE

Use the following meta tags to have smooth page transitions in IE:

<head id="Head1" runat="server">
<meta http-equiv="Page-Exit" content="blendTrans(Duration=0.1)" />
<meta http-equiv="Page-Enter" content="blendTrans(Duration=0.1)" />
<meta http-equiv="Site-Enter" content="blendTrans(Duration=0.1)" />
<meta http-equiv="Site-Exit" content="blendTrans(Duration=0.1)" />
</head>

Saturday, 4 July 2009

Sending Complex JavaScript Objects to WCF and Web Services

Example 1 - Sending dictionary to a WebMethod or PageMethod:

[WebMethod]
public static string SendDictionary(Dictionary&lt;string,string&gt; dictionary)
{
return "Success";
}

Sending objects using AJAX:

function SendDictionary() {
var dictionary = CreateDictionary();
PageMethods.SendDictionary(dictionary, sendDictionarySucceededHandler, sendDictionaryFailedHandler);
}

function CreateDictionary() {
var dictionary = new Object();
dictionary["Filter1"]= "SelectedValue1";
dictionary["Filter2"]= "SelectedValue2";
return dictionary;
}

Example2-Sending list of objects to a WebMethod or PageMethod:

[WebMethod]
public static string SendPeople(List<string> people)
{
return "Success";
}

Sending objects using AJAX:

function SendPeople() {
var people = CreatePeople();
PageMethods.SendPeople(people, sendPeopleSucceededHandler, sendPeopleFailedHandler);
}

function CreatePeople() {
var result = new Array();
var object1 = new Object();
object1.FirstName = "FirstName1";
object1.LastName = "LastName1";
result[0] = object1;

var object2 = new Object();
object2.FirstName = "FirstName2";
object2.LastName = "LastName2";
result[1] = object2;

return result;
}
Example3 - Sending Dictionary to a WCF service
You must use JSON to do so.
function SendDictionary()
{var dictionary = CreateDictionaryForWCF();
PageMethods.SendDictionary(dictionary, sendDictionarySucceededHandler, sendDictionaryFailedHandler);}
function CreateDictionaryForWCF()
{
var dictionary = new Array();
dictionary[0]= AddJSONItem("Filter1", "SelectedValue1");
dictionary[1]= AddJSONItem("Filter2", "SelectedValue2");
return dictionary;}
function AddJSONItem(key, value){ return {"Key": key, "Value": value};}

More details:

Thursday, 2 July 2009

SVN

  • AnkhSVN is a Subversion SourceControl Provider for Visual Studio
  • TortoiseSVN is a really easy to use Revision control / version control / source control software for Windows.
  • VisualSVN
  • SvnSpam: produces a nicely formatted HTML e-mail message containing the (universal) diff, log message and versions of files involved in an svn commit.
Quickest Way to Create a Fresh Branch Locally:

When needed to create a new tag or branch from the latest version of trunk (rather than retrieving one of the old ones), it doesn't have to download everything and quickest way would be like (perhaps in 3mins):

1) making sure trunk has no file which is in a conflict or checked out status

2) making sure trunk is up-to-date with svn

3) Use svn Branch command to create a new branch/tag on svn

4) Make a local copy of the trunk (which is up-to-date) to a local branch folder

5) Use svn Switch command to map the newly created local branch folder to point to svn branch folder

6) get a quick update
Tutorials:
Glossaries:
  • Check out
  • Export
  • Import
  • Revision
  • Trunk: this is where the latest version of your main code is located
  • Branch: this is where you bug fix the released versions; then you will have to merge back into trunk
  • Tag: this is where you put your release versions of your application
  • Repository
  • Merge
  • Switch: make a local folder to point to a svn url

Windows Commands
Get Latest

CD C:\Program Files\TortoiseSVN\bin\
START TortoiseProc.exe /command:update /path:"C:\Svn\MyTeamProject\trunk\" /closeonend:1