Search code examples
c#asp.netasp.net-mvccode-duplication

Avoid duplicate code in SortedDictionary C#


So I have two if stament to handle dictionary value.If the method is POST I just need to add the status params to the dictionary.All the other params are the same.

var stringBuilder = new StringBuilder();                    
var dictionary = new SortedDictionary<string, string>();
if (method == "GET")
{
    stringBuilder.Append("GET&");
    stringBuilder.Append(Uri.EscapeDataString(url));
    stringBuilder.Append("&");
    dictionary = new SortedDictionary<string, string>
    {
        { "oauth_version" , oauthVersion },
        { "oauth_consumer_key", consumerKey },
        { "oauth_nonce" , oauthNonce },
        { "oauth_signature_method" , oauthSignatureMethod },
        { "oauth_timestamp" , oauthTimeStamp },
        { "oauth_token" , accessToken },
    };
}

if (method == "POST")
{
    stringBuilder.AppendFormat("POST&{0}&", Uri.EscapeDataString(url));
    dictionary = new SortedDictionary<string, string>
    {
        { "oauth_version" , oauthVersion },
        { "oauth_consumer_key", consumerKey },
        { "oauth_nonce" , oauthNonce },
        { "oauth_signature_method" , oauthSignatureMethod },
        { "oauth_timestamp" , oauthTimeStamp },
        { "oauth_token" , accessToken },
        { "status" , status }
    };
}

Is there anyways to avoid duplicate in my code.Any suggestion would be handy.

Kindly regard.


Solution

  • I don't much see any relation between your StringBuilder instance variable and the dictionary that you are constructing, so assuming the duplication of the dictionary code, it can be taken out of those if conditions and then you could deal with the remaining StringBuilder instance like that:

    var stringBuilder = new StringBuilder();
    stringBuilder.AppendFormat("{0}&{1}&", method, Uri.EscapeDataString(url));
    
    var dictionary = new SortedDictionary<string, string>
    {
        { "oauth_version", oauthVersion },
        { "oauth_consumer_key", consumerKey },
        { "oauth_nonce", oauthNonce },
        { "oauth_signature_method", oauthSignatureMethod },
        { "oauth_timestamp", oauthTimeStamp },
        { "oauth_token", accessToken },
    };
    
    if (method == "POST")
    {
        // Only add the status parameter if the method is POST
        dictionary["status"] = status;
    }
    

    So basically after this code executes both the stringBuilder and dictionary instance variables as shown in your question will have the expected values.

    This being said, your dictionary looks like as if it contains query string parameter values. If this is the case then you more than definitely will want to properly url encode those values before ever consider sending them through the HTTP protocol:

    var dictionary = new SortedDictionary<string, string>
    {
        { "oauth_version", Uri.EscapeDataString(oauthVersion) },
        { "oauth_consumer_key", Uri.EscapeDataString(consumerKey) },
        { "oauth_nonce", Uri.EscapeDataString(oauthNonce) },
        { "oauth_signature_method", Uri.EscapeDataString(oauthSignatureMethod) },
        { "oauth_timestamp", Uri.EscapeDataString(oauthTimeStamp) },
        { "oauth_token", Uri.EscapeDataString(accessToken) },
    };
    
    if (method == "POST")
    {
        // Only add the status parameter if the method is POST
        dictionary["status"] = Uri.EscapeDataString(status);
    }