Using the Strategy Design Pattern(C#) to sort based on different columns of data

2k views Asked by At

I am currently trying to get my head around all of the different Design Patterns, I have been set the task of sorting an IQueryable based on different columns, this is how it is currently implemented:

if (choice == 1)
{ 
    return from Animals in ctx.Animals orderby Animals.AnimalID descending select Animals; 
}
else if (choice == 2)
{ 
    return from Animals in ctx.Animals orderby Animals.Name descending select Animals; 
}
else if (choice == 3)
{ 
    return from Animals in ctx.Animals orderby Animals.Age descending select Animals; 
}

This however seems like a bad code smell to me, it does not have the ability to add different fields or sorting options easily, my tutor has advised me that it would be better to implement the strategy pattern and use a Dictionary to select the strategy implementation we want, However I'm not sure how the Strategy Pattern would be applied to this situation, any helpful tips would be appreciated greatly, if any more information is required, just ask.

2

There are 2 answers

4
dcastro On BEST ANSWER

Applying the strategy pattern, you'd have an ISortStrategy interface, and then several implementations like SortById, SortByName and SortByAge. The interface, and its implementations, would have a method like object Sort(Animal animal); that returns one of the animal's properties.

You'd then just select the proper strategy at runtime, and use it like this:

return from animal in ctx.Animals
       orderby sortStrategy.Sort(animal) descending
       select animal;
0
SWeko On

Continuing from @dcastro's answer, regarding dictionaries.

You can create the concrete strategy via a factory class (and get bonus points for using a factory as well):

public static class SortStrategyFactory()
{
    private static Dictionary<string, ISortStrategy> strategyRepository;

    static SortStrategyFactory()
    {
      strategyRepository = new Dictionary<string, ISortStrategy>();
      strategyRepository.Add("ID", new SortById());
      strategyRepository.Add("Name", new SortByName());
      strategyRepository.Add("Age", new SortByAge());
    }

    public static ISortStrategy GetStrategy(string key)
    {
      //todo: add error checking
      return strategyRepository[key];
    }

}

Then your initial code becomes:

ISortStrategy sortStrategy= SortStrategyFactory.GetStrategy(choice);

return from animal in ctx.Animals
       orderby sortStrategy.Sort(animal)
       descending select animal;