Category: C#

More >

Behavior-Driven Development (BDD) – The What, Why, and How

The standard development method they teach you in Software Engineering 101 is

  1. Get your requirements
  2. Come up with a top-bottom architectural design
  3. Code up modules one by one
  4. Test (Unit/System/Regression)
  5. Delivery/Deployment

It’s a great approach and the solution you deliver can be built without compilation errors and work fine… or so you think.  You deliver the solution and all of the sudden, the client points out that there were requirements missed and/or incomplete.  My favorite taught-myself-in-24-hours-developer reaction to this is:

“But… it… compiles… therefore… it works…  it has to!”

With Behavior-Driven Development (BDD), and one of the reasons this approach is favored by QA teams all over the world is this approach ensures and, actually, requires that you work against the user requirements when working on the design.
Continue reading

Category: C#

More >

Tutorial: Refresher on Sorting Generics in C# – IComparable & IComparer

I thought I’d post a quick refresher for sorting Lists<> and generics since it will be used in the post about working with Sitecore Fast Query.

There are a lot of ways of sorting items you get back from Sitecore, including even implementing a bubble sort in the code-behind aspx.cs file.  That may work for a quick prototyping exercise; however, when working with enterprise class systems, why not leverage the tools C# gave you to create elegant and robust code by creating line-of-business model objects and using a C# List<T> that already comes with sort functionality (provided you define the rules on how to compare your objects)?

IComparable

If you want to your objects to be sortable, you have to make sure they implement the IComparable method, otherwise when you try to do a List<T>.Sort() on them, you will get an exception.  To implement the IComparable class, you will need to implement the CompareTo() method, which essentially describes how to compare two objects.  If you compare your objects alphabetically, you can take advantage of the String.CompareTo() method here.  Take a look at the code example below using a Movie object.

public class Movie : IComparable
{
public int YearReleased { get; set; }
public string MovieTitle { get; set; }

//need to implement this as a result
//of declaring the IComparable interface
int CompareTo(Movie otherMovie)
{
return MovieTitle.CompareTo(otherMovie.Title);
}
}

IComparer

When you implement the IComparable interface and the CompareTo method, that is the default sort method.  When you want to create additional properties on which to sort, this is where the IComparer interface comes in.   For every method on which you will want to sort, you will have to create a class that implements the IComparer interface.  So, if you wanted to create two sorts – ascending and descending – on the year the movie was released, you will need two classes that implement the IComparer interface.  With this particular example, we can create these classes as nested.

public class Movie : IComparable //main class
{
public int YearReleased { get; set; }
public string MovieTitle { get; set; }

public class SortByYearReleasedAscending : IComparer
{
int Compare(Movie a, Movie b)
{
var compareResult = a.YearReleased - b.YearReleased;

return ( (compareResult == 0) ? 0 : compareResult / Math.Abs(compareResult) );

}
}

public class SortByYearReleasedDescending : IComparer
{
}
}

In case I lost you in the Compare() method, I am all about C# shortcuts (thanks to ReSharper) and didn’t feel like writing this out:

if (a.year > b.year)
return 1;
if (a.year < b.year)
return -1;
else
return 0;

Since the year is an integer, we can just take the difference. If the value on the left is bigger, the result will be positive, otherwise negative. At that point, we will need to return 1, -1, or if it’s the same, the result is 0. To get our result down to a 1 (positive or negative), we simply divide by the absolute value of the result, but not before we check if it’s 0 to bypass the divide by zero error.

And that’s all there is to it. To piece it all together, if you have a List<movieList>, you can do a sort on it using movieList.Sort() which will use the default IComparable.CompareTo method, or you can specify how you want it sorted by passing in the IComparer component, like movieList.Sort(new movieList.SortByYearReleasedAscending())

Questions or comments – drop me a line.

Category: Sharepoint

More >

Category: OpenXML

More >

OpenXML & Microsoft Word document manipulation using data Sharepoint

I have been involved in a lot more Sharepoint development at work lately – moreso than .Net, so I’ve been kind of busy reading up SDKs and playing with APIs.

On that note, I got a chance to get my hands dirty with some OpenXML 2.0. The cool thing about that was that it was @ Microsoft. Not only did I get to play with it, but met the guys that developed parts of it and even wrote 2,000 of the 6,000 pages of documentation – Tristan Davis and Zeyad Rajabi – who helped me a great deal.

The story that I worked on involved gathering data from Sharepoint lists (some of it fed from the BDC(business data catalog)) and using OpenXML to populate a Word template. The real world scenario behind it is at my company, like I’m sure at a lot of other companies, we have administrative assistants who end up generating the documents for meetings. They just sit there copying and pasting data from various data sources and reports into this document. And there are as many documents generated as there are assistants multiplied by weeks in the year. Seriously – these people end up working 60 hour workweeks.
Continue reading