Take the 2-minute tour ×

I wrote this method that takes in a generic parameter object. This method accepts different parameter objects with different properties. The idea is that based on the value of the "MethodName" property, I can construct a URI base on properties of that particular parameter object

I use reflection to get the value of the property named "MethodName." It's not a method, just a property name.

I use the value of the MethodName property in a case statement. This seems like an inferior way to accomplish my task. Especially as the number of MethodNames grow.

Below is the source with the case statement implementation. Any help would be appreciated.

    public string ConstructBBAPIUri<T>(T parameters)
        var methodName = "";
        var uri = "";
        var userId = "";
        long timeStamp = 0;
        var signature = "";
        var sessionKey = "";
        var newUserId = "";

        if (parameters != null)
            methodName = parameters.GetType().GetProperty("MethodName").GetValue(parameters, null).ToString();

            switch (methodName)
                case "user.login":
                    userId = parameters.GetType().GetProperty("UserId").GetValue(parameters, null).ToString();
                    timeStamp = Convert.ToInt64(parameters.GetType().GetProperty("TimeStamp").GetValue(parameters, null));
                    signature = GenerateBunchBallSignature(userId);

                    uri = "method=" + methodName + "&apiKey=" + apiKey + "&userid=" + userId + "&ts=" +
                          timeStamp + "&sig=" + signature;

                case "user.modifyUserId":

                    //We shouldn't need the session key if user.login is being called first
                    newUserId = parameters.GetType().GetProperty("NewUserId").GetValue(parameters, null).ToString();

                    uri = "method=" + methodName + "&sessionKey=" + sessionKey + "&oldUserId=" + userId +
                          "&newUserId=" + newUserId;

        return uri;
share|improve this question
Switching on a string is pretty bad in my opinion. Why don't you validate that your property is of type enum ??? and switch on the enum value? –  emartel Nov 15 '12 at 2:20
That T objects, do they have any common interface or a base class? Or are they just completely different? And btw, do you have control over them, can you change their code? –  Max Shmelev Nov 15 '12 at 2:22
Max, yes the T objects all have an interface in common. And I do have control over the code. –  Derek Kenney Nov 15 '12 at 4:43
add comment

2 Answers

up vote 4 down vote accepted

A better design would be to delegate constructing of URI on the classes.

  1. Create an interface with method ConstructBBAPIUri();
  2. Implement the interface for new or existing classes you have code access
  3. For existing classes you don't have access to code, inherit from those classes and implement interface. Hopefully all the properties you need for those classes are accessible, otherwise use reflection.
share|improve this answer
Thanks much. Will look at this in the morning. –  Derek Kenney Nov 15 '12 at 4:40
add comment

An option would be to add functions to a dictionary but I don't see how that would be much better than the current method, and it would be more confusing and error-prone.

I think what Max Shmelev is hinting at would be the best solution. If you can, add an interface to each class which will be passed, and add a generic constraint to this method. The interface will define a method which, when implemented in the objects, will handle the serialization for that object.

If the list isn't too much longer, you could add methods for each different type to be handled, and just have a single method call within each case.

share|improve this answer
Thanks for the feedback from everyone. This has been very helpful. –  Derek Kenney Nov 15 '12 at 4:44
add comment

Your Answer


