EDIT 1: Forgot to add a ball to the curve of the attached property.
UPDATE: I chose @mtazva's answer as this was the preferred solution for my particular case. Looking back, I asked a general question with a very concrete example, and I believe that in the end everyone was confused (or maybe just me) as regards the question. I believe that a general question was also answered (see Answers and links to the strategy template). Thanks everyone!
The operators of a large operator are obviously smell , and I saw some links to how you could do this using a dictionary that maps to functions . But I wonder if there is a better (or smarter way) to do this? In a way, this is a question that I always somehow rolled down the back of my head, but really did not have a good solution.
This question arose from another question that I asked earlier: How to select all values โโof an object property in a list of typed objects in .Net with C #
Here is an example of the class I'm working with (from an external source):
public class NestedGameInfoObject { public string NestedName { get; set; } public int NestedIntValue { get; set; } public decimal NestedDecimalValue { get; set; } } public class GameInfo { public int UserId { get; set; } public int MatchesWon { get; set; } public long BulletsFired { get; set; } public string LastLevelVisited { get; set; } public NestedGameInfoObject SuperCoolNestedGameInfo { get; set; }
Unfortunately, this is due to an external source ... imagine that HUGE dump data from Grand Theft Auto or something like that.
And I want to get only a small cross section of the list of these objects. Imagine that we want to compare you with a bunch of objects of information about the game of your friends. An individual result for one user will look like this:
public class MyResult { public int UserId { get; set; }
And an example of what I want to replace with something more manageable (believe me, I DO NOT want to support this monster switch statement):
const int MATCHES_WON = 1; const int BULLETS_FIRED = 2; const int NESTED_INT = 3; public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input) { var output = new List<MyResult>(); switch(input) { case MATCHES_WON: output = gameInfos.Select(x => new MyResult() { UserId = x.UserId, ResultValue = x.MatchesWon.ToString() }).ToList<MyResult>(); break; case BULLETS_FIRED: output = gameInfos.Select(x => new MyResult() { UserId = x.UserId, ResultValue = x.BulletsFired.ToString() }).ToList<MyResult>(); break; case NESTED_INT: output = gameInfos.Select(x => new MyResult() { UserId = x.UserId, ResultValue = x.SuperCoolNestedGameInfo.NestedIntValue.ToString() }).ToList<MyResult>(); break;
So the question is, are there any reasonable ways to control this beast? I would really like the dynamic way to get this information in case the initial changes to the object (for example, additional properties of the game information are added). Is there a better way to architect this so that it is less awkward?