mercredi 1 mars 2017

Invoking a method from a string method name

I have a two sets of Urls one for PreProd and one for Prod. Each Url has several API nodes. Instead of hard coding these API nodes, I maintain them in an enum

Something like this:

//PreProd
 public enum PreProd
        {
            precheckorder,
            submitresubmit,
            creditInquiry,
            createupdateorder,
            confirmorder,
            getorderstatus,
            cancelorder,
        }

        /// <summary>
        /// Gets the relative URL.
        /// </summary>
        /// <param name="u">The u.</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string GetRelativeUrl(PreProd u)
        {
            switch (u)
            {
                case PreProd.precheckorder:
                    return "http://ift.tt/2mGaFQY";
                case PreProd.submitresubmit:
                    return "http://ift.tt/2meUw7z";
                case PreProd.creditInquiry:
                    return "http://ift.tt/2mGdr90";
                case PreProd.createupdateorder:
                    return "http://ift.tt/2mf06Hc";
                case PreProd.confirmorder:
                    return "http://ift.tt/2mG0LPj";
                case PreProd.getorderstatus:
                    return "http://ift.tt/2mfc97e";
                case PreProd.cancelorder:
                    return "http://ift.tt/2mGefKN";
                default:
                    // Handle bad URL, possibly throw
                    throw new Exception();
            }
        }

//Prod
        private enum Prod
        {
            precheckorder,
            submitresubmit,
            creditInquiry,
            createupdateorder,
            confirmorder,
            getorderstatus,
            cancelorder,
        }

        /// <summary>
        /// Gets the relative URL.
        /// </summary>
        /// <param name="u">The u.</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        private static string GetRelativeUrl(Prod u)
        {
            switch (u)
            {
                case Prod.precheckorder:
                    return "http://ift.tt/2mf7xyh";
                case Prod.submitresubmit:
                    return "http://ift.tt/2mG5Bfy";
                case Prod.creditInquiry:
                    return "http://ift.tt/2mf9KtD";
                case Prod.createupdateorder:
                    return "http://ift.tt/2mGachR";
                case Prod.confirmorder:
                    return "http://ift.tt/2mf08Pk";
                case Prod.getorderstatus:
                    return "http://ift.tt/2mG8IUu";
                case Prod.cancelorder:
                    return "http://ift.tt/2mfdnzB";
                default:
                    // Handle bad URL, possibly throw
                    throw new Exception();
            }
        }

We use environment variables to store the Environment name and thats what dictates which API set to use.

Ideally, I would like to have a single method, I pass in my environment and api name and it will return the API Url.

Something like

GettexApiUrlBasedOnEnvironment("Dev", "precheckorder");

and response will be

"http://ift.tt/2mGaFQY"

Any ideas/suggestions will be much appreciated. TIA





Aucun commentaire:

Enregistrer un commentaire