Thursday, September 13, 2012

Start of Week

Needed to find the first day of the next week for a given date. I thought there might be an in-built method to do this, but I didn't see anything.

So I wrote these extension methods - one that uses the CurrentCulture and one that can work on a predefined day of the week.
public static class DateTimeExtensions
{
    public static DateTime GetNextWeekStartDate(this DateTime dt, 
                                                DayOfWeek weekStartDay)
    {
        var difference = weekStartDay - dt.DayOfWeek;
        if (difference < 0)
            difference += 7;
        return dt.AddDays(weekStartDay - dt.DayOfWeek + 7);
    }

    public static DateTime GetNextWeekStartDate(this DateTime dt)
    {
        CultureInfo ci = Thread.CurrentThread.CurrentCulture;
        DayOfWeek weekStartDay = ci.DateTimeFormat.FirstDayOfWeek;
        return GetNextWeekStartDate(dt, weekStartDay);
    }
}

But I hate adding the 7 there. I am sure this can be done in  a more elegant way. Another time perhaps...

Monday, September 10, 2012

Design Patterns


What are Design Patterns?
Design Patterns are general and reusable templates or solutions to solve common problems. It is not an algorithm but a usual pattern which can be used to design a solution to the problem.

History
The term is taken from a book by Christopher Alexander – a regular "buildings" architect. 

In software development these ideas were initially published in the book - Design Patterns: Elements of Reusable Object-Oriented Software. The authors of the book - Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides are together often called the Gang of Four (GoF).

Why do they matter?
Design patterns are well accepted solutions to existing software development problems and provide templates to define systems with good OO design qualities. The design patterns provide a common language between developers to explain complex scenarios.

Pattern Organization
Patterns are organized in an ever increasing list (as new patterns are discovered).
  1. Creational
    1.   Factory
    2. Builder
    3. Etc...
  2. Structural
    1. Decorator
    2. Etc.
  3. Behavioral
  4. Security
  5. Etc.
Further reading: Design Patterns