How do I reorganize this code so that I can centralize the projection?
public IEnumerable<ItemDto> GetItemsByType(int itemId, ItemType itemType) { IEnumerable<ItemDto> items = null; try { var tempItems= _Items.Get(i => i.ItemId == itemId && o.Active == true); switch (itemType) { case ItemType.Normal: items = from item in tempItems select new ItemDto { // many fields here }; break; case ItemType.Damaged: items = from item in tempItems join itemDetail in _ItemDetails.Get() on item.ID equals itemDetail.ItemID select new ItemDto { // many fields here }; break; case ItemType.Fixed: items = from item in tempItems join itemDetail in _ItemDetails.Get() on item.ID equals itemDetail.ItemID where item.Status.ToLower() == "fixed" select new ItemDto { // many fields here }; break; // more case statements here... default: break; } } catch { ... } }
Basically, I have many case arguments and a long projection onto each case statement. I am worried that as soon as the DTO needs to be changed, say, add a new field, the projection of other cases may not correspond to each other (forgot or missed the update). How can I centralize this?
source share