Im using asp.net mvc and I'm implementing a grid that displays cards. Each card could be of a different type and display different information. Something that doesn't seem to be uncommon these days. From the grid perspective it gets a List and just iterates over the list calling the correct view.
foreach(item in gridList)
{
@Html.Partial(item.GetCard(),item,...)
}
public abstract class GridViewItem
{
public abstract string GetCard()
}
public class CardAViewModel : GridViewItem
{
public override string GetCard()
{
return "card path for A"
}
}
public class CardBViewModel : GridViewItem
{
public override string GetCard()
{
return "card path for B"
}
}...
So in the controller it will take its data source and map it to the appropriate view model.
My questions is in the controller Im ending up with this.
...Do some logic
List<GridViewItem> grid....
var List<objectA> listA =_serviceA.GetStuff();
...Do some logic
grid.AddRange(MapToCardAViewModel(listA));
var List<objectB> listB =_serviceB.GetStuff();
...Do some logic
grid.AddRange(MapToCardBViewModel(listB));
...Do some logic
var List<objectC> listC =_serviceC.GetStuff();
grid.AddRange(MapToCardCViewModel(listC));
Making all these calls and aggregating each list gets blown out real quick. I ultimately want the controller to just make one call and then handle the mapping, but I don't want to just end up with the same problem in another layer. What would be a good pattern to re-factor out this logic so that as more data sources are added I can simply extend whatever class is putting this list together. Instead of having to modify the class every time a new source is entered.
Aucun commentaire:
Enregistrer un commentaire