0

有没有更好的方法来实现这个代码:

public Payment SendPaymentToSagePay(Payment paymentModel)
    {

        var webClient = new WebClient()
        {
            BaseAddress = "http://localhost:64317/api/PaymentStart"
        };

        string price = paymentModel.price.ToString();

        string productId = paymentModel.productId.ToString();

        string paymentMode = paymentModel.paymentMode.ToString();

        string context = paymentModel.context.ToString();

        var collection = new NameValueCollection();
        collection.Add("title", paymentModel.title);
        collection.Add("firstName", paymentModel.firstName);
        collection.Add("lastName", paymentModel.lastName);
        collection.Add("email", paymentModel.email);
        collection.Add("context", context);
        collection.Add("programmeName", paymentModel.programmeName);
        collection.Add("productId", productId);
        collection.Add("price", price);
        collection.Add("cardHolderTitle", paymentModel.cardHolderTitle);
        collection.Add("cardHolderLastName", paymentModel.cardHolderLastName);
        collection.Add("cardHolderFirstName,", paymentModel.cardHolderFirstName);
        collection.Add("cardHolderEmail", paymentModel.cardHolderEmail);
        collection.Add("selfAddress1", paymentModel.selfAddress1);
        collection.Add("selfAddress2", paymentModel.selfAddress2);
        collection.Add("selfCity", paymentModel.selfCity);
        collection.Add("selfCountry", paymentModel.selfCountry);
        collection.Add("selfState", paymentModel.selfState);
        collection.Add("selfPhone", paymentModel.selfPhone);
        collection.Add("selfPostCode", paymentModel.selfPostCode);
        collection.Add("otherAddress1", paymentModel.otherAddress1);
        collection.Add("otherAddress2", paymentModel.otherAddress2);
        collection.Add("otherCity", paymentModel.otherCity);
        collection.Add("otherCountry", paymentModel.otherCountry);
        collection.Add("otherState", paymentModel.otherState);
        collection.Add("otherPhone", paymentModel.otherPhone);
        collection.Add("otherPostCode", paymentModel.otherPostCode);
        collection.Add("ipAddress", paymentModel.ipAddress);
        collection.Add("additionalInfo", paymentModel.additionalInfo);
        collection.Add("paymentMode", paymentMode);



        byte[] responseBytes = webClient.UploadValues("", "POST", collection);
        string response = Encoding.UTF8.GetString(responseBytes);
        string decodedResponse = HttpUtility.UrlDecode(response);
        JavaScriptSerializer js = new JavaScriptSerializer();
        var payment = js.Deserialize<Payment>(decodedResponse);
        return payment;
    }

我正在使用这种方法来序列化我的支付模型,然后将其发送到外部 Web api,然后将这些数据添加到各种存储库中。

有没有更简洁的方法来做到这一点?

理想情况下,我想要一个对 MVC4 和依赖注入(我目前正在使用 Unity 容器)更友好的解决方案,我创建了这个序列化程序作为临时解决方案,同时我研究了其他方法,到目前为止,这是我唯一能够成功实现的方法.

任何建议将不胜感激。

4

1 回答 1

0

您可以发布 JSON 而不是表单集合吗?如果可以,您可以使用与反序列化响应相同的 JavaScriptSerializer 来序列您的Payment

string serializedPayment = js.Serialize(paymentModel);

于 2013-05-01T13:36:49.770 回答