Wednesday 24 December 2008

Update: AutoComplete using YUI

Previously i had written about how i had made a wrapper control for .net from YUI. In that control i still had to write some javascripts and thus needed some hard work before the control was in a working condition. Now, I have made it into a class library and doesn't require any javascript to be written. Add the dll as a reference and you can use this control like a normal control.

Here is the download link http://abhishekshrestha.posterous.com/autocomplete-yui-net-source

Wednesday 17 December 2008

Some useful .net specific links

I have few links that may be helpful to you guys:

1. Turning an .ascx User Control into a Redistributable Custom Control
http://msdn.microsoft.com/en-us/library/aa479318.aspx
if you don't want to go through the msdn article, here is a links to a shorter article on creating custom control from user control

2. Ten must have tools for .net developer

3. Visual Studio keyboard shortcuts

4. Static Code Analysis (FxCop is a great tool to go with)

5. Guidelines for Names in .net (Naming conventions)

6. jQuery Intellisense in VS 2008 (Scott Gu)

Monday 15 December 2008

Timer application on .net c#

In some projects i have worked, there have been requiremts of console application which runs on the background and does(requests) some processing at a certain interval of time. For example, if you have a newsletter subscription system in you site, you may want to send news at 8:00 AM every monday. For this, a console application will run as a schedule task or windows service and if set as schedule task, it will runt at that particular scheduled time.

Now, say for example, you newletter subscription for 3 types of newsletters, one is sent every monday 8 A.M, the second one is sent every Monday-Wednesday and the third one is sent everyday. Since, its newsletter and has the same database of subscribers but only differs in the subscripttion type (which definesd which trpe of newsletter to sent to the subscriber). And most importantly you do not want to write 3 different console applications (or timers) with almost same logic.Extending the newsletters, now say, you want to process(or request) a database call every 10 mins. So, we have 4 different implementation at different time intervals.


Requirements:
1. Application running in background like a timer
2. More than one process request with independent time intervals

High level Logic:
1. Get the list of process requests
2. Check if it is time to make that request
3. If it is time then make a request

1. Getting the list of process requests:
Create an XML file with the following settings,

<?xml version="1.0" encoding="utf-8" ?>
<ProcessList>
<Process Name="NewsletterMonday">
<Times>
<Time Value="8:30 AM" ClockType="_12Hr" />
</Times>
<Days Type="Selected">
<Day Value="Monday" />
</Days>
</Process>
<Process Name="NewsletterMondayToWednesday">
<Times>
<Time Value="10:30 PM" ClockType="_12Hr"/>
</Times>
<Days Type="Range">
<Day Value="Monday" />
<Day Value="Wednesday" />
</Days>
</Process>
<Process Name="NewsletterAllDays">
<Times>
<Time Value="15:00" ClockType="_24Hr"/>
</Times>
<Days Type="All" />
</Process>
<Process Name="DatabaseCallEveryTenMinsAllDays">
<Times>
<Time Value="10" ClockType="Min"/>
</Times>
<Days Type="All" />
</Process>
</ProcessList>
This seetings xml defines what type of process are there and the time to make the
process request.

2. Check if it is time to make that request:
private bool isProcessRequestDay()
{
bool isProcessRequestDay= false;
switch (_Days.Type)
{
case DayType.All:
isRefreshDay = true;
break;
case DayType.Range:
isProcessRequestDay = (DateTime.Today.DayOfWeek >= _Days[0].Value && DateTime.Today.DayOfWeek <= _Days[_Days.Count - 1].Value);
break;
case DayType.Selected:
foreach (Day day in _Days)
{
if (day.Value == DateTime.Today.DayOfWeek)
{
isProcessRequestDay= true;
break;
}
}
break;
}
return isProcessRequestDay;
}



_Days is a Generic List class with type as a property which contains
the value from < type="----"> from the settings xml. [Note: You will have
to load the settings to the its corresponding classes. ]

Now, get the date and time at which the process request should be made



    private DateTime getMinProcessRequestDateTime(DateTime currentDateTime)
    {
        int min = 0, hr = 0, sec = 0;
        int year = currentDateTime.Year;
        int month = currentDateTime.Month;
        int day = currentDateTime.Day;
        DateTime minProcessRequestDateTime = DateTime.MinValue;

        foreach (ProcessRequestTime ProcessRequestTime in _ProcessRequestTimes)
        {
            DateTime tempProcessRequestDateTime = DateTime.MaxValue;
            switch (ProcessRequestTime.ClockType)
            {
                case ClockType._24Hr:
                    string[] time24Hr = ProcessRequestTime.Value.Split(Constants.SeperatorColon);
                    hr = Convert.ToInt32(time24Hr[0]);
                    min = Convert.ToInt32(time24Hr[1]);

                    tempProcessRequestDateTime = new DateTime(year, month, day, hr, min, 0);
                    if (!(currentDateTime > tempProcessRequestDateTime))
                    {
                        tempProcessRequestDateTime = tempProcessRequestDateTime.AddDays(-1);
                    }
                    break;
                case ClockType._12Hr:
                    string[] time12Hr = ProcessRequestTime.Value.Split(Constants.SeperatorSingleSpace);
                    if (time12Hr[1].Equals(Constants.DateTimeMeridianPM, StringComparison.InvariantCultureIgnoreCase))
                    {
                        hr = Convert.ToInt32(time12Hr[0].Split(Constants.SeperatorColon)[0]);
                    }
                    else if (time12Hr[1].Equals(Constants.DateTimeMeridianAM, StringComparison.InvariantCultureIgnoreCase))
                    {
                        hr = Convert.ToInt32(time12Hr[0].Split(Constants.SeperatorColon)[0]);
                        if (hr >= 12) { hr -= 12; }
                    }
                    min = Convert.ToInt32(time12Hr[0].Split(Constants.SeperatorColon)[1]);

                    tempProcessRequestDateTime = new DateTime(year, month, day, hr, min, 0);
                    if (!(currentDateTime > tempProcessRequestDateTime))
                    {
                        tempProcessRequestDateTime = tempProcessRequestDateTime.AddDays(-1);
                    }
                    break;
                case ClockType.Hr:
                    hr = Convert.ToInt32(ProcessRequestTime.Value);
                    tempProcessRequestDateTime = new DateTime(year, month, day);
                    tempProcessRequestDateTime = tempProcessRequestDateTime.AddHours(currentDateTime.Hour - hr).AddMinutes(currentDateTime.Minute);
                    break;
                case ClockType.Min:
                    min = Convert.ToInt32(ProcessRequestTime.Value);
                    tempProcessRequestDateTime = new DateTime(year, month, day);
                    tempProcessRequestDateTime = tempProcessRequestDateTime.AddHours(currentDateTime.Hour).AddMinutes(currentDateTime.Minute - min);
                    break;
                case ClockType.Sec:
                    sec = Convert.ToInt32(ProcessRequestTime.Value);
                    tempProcessRequestDateTime = new DateTime(year, month, day);
                    tempProcessRequestDateTime = tempProcessRequestDateTime.AddHours(currentDateTime.Hour).AddMinutes(currentDateTime.Minute).AddSeconds(currentDateTime.Second - sec);
                    break;
            }
            //Assigning the maximum datetime less than the current date
            if (tempProcessRequestDateTime > minProcessRequestDateTime)
            { minProcessRequestDateTime = tempProcessRequestDateTime; _DataSource = ProcessRequestTime.DataSource; }
        }
        return minProcessRequestDateTime;
    }

Now, get the date and time when the last process request was made,
DateTime lastProcessRequestDateTime = {from database or file or memory};



Now, check if last request made before the time calculated for next request,
bool isProcessRequestTime = (minProcessRequestDateTime >= lastProcessRequestDateTime);

If the last request made was before the current calculated time for request, then it means that now is the time to make a new request. Otherwise, if the last request made is after the current calulated time for next request, then the schedule time for process request has already been made.

3. If it is time then make a request
if(isProcessRequestTime)
{
//Call the process
// database call or web service call or http request call
}

Hope this give some logic into how you can write a single application with process request settings that runs on background to make different process request for different time intervals.

If you require code for this example, please mail me or give your mail id in the comment section. I'll will send you the whole code for this application.

Friday 12 December 2008

How to be a good development team lead?

Working in quite a few companies in my professional life time and being part of a few more projects, I was lucky to have a good team lead in my starting days. With the number of projects and teams, I have liked few and not-so-much some. And if you are a lead (or aspiring to be one) , read through what a team member feels a lead should at least have in them.

1. Know your development platform
I believe a team lead is an integral part of the development group (a group of people with development skills). He/She is involved in coding and has a greater responsibility of taking critical decisions, apart from project planning and documentation work. For a developer to respect his lead, just taking is not enough. He/She has to be in line or ahead of the other members of the team. If you love talking and don't love coding, you better change you career. Those are part of marketing and sales important qualities. At our desk, we build products and not sell them. Without sales and marketing, we as a developer wouldn't have any job but if you are a talking person and not the doing person, being a lead will be a difficult task.

2. Be a part of the team, a team member
Being part of a team means more than just few words and lines, but most importantly it means putting your desk in the team space and working from that desk, :). I think the team member feel and attitude starts from that. Communication, understanding, discussions, help are few words that is part of being a team. Having your own room and working isolated from your team, tells a lot about how insecure and uninvolved you are as a lead.

3. Better working environment
A company does its best to create a good environment for all its employees to work at, but in the end its up to the the lead that make it better or worse. A recent scientific research has found that a bad boss leads to bad health and greater chance of heart attacks to its subordinates. You should always try to create a good environment for your team members to be in a good mood and concentrate on doing their assigned job better.

4. You are the lead
Sunny days and beautiful flowers are not every season. There will be times when you'll have to be hard and times when you'll have to take tuff decisions, so, you should be prepared to show that you are the lead and its for them to respect your decision. You cannot be polite every time and no way be pushed around by your members, and you must be prepared to show them that you are the lead and you are there to lead others to reach a goal.