Pregunta

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.

¿Fue útil?

Solución

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;

Otros consejos

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;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top