Search code examples
c#design-patternsstrategy-pattern

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


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.


Solution

  • 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;