在WEBAPI方法中处理通用对象的正确方法是什么?

在WEBAPI方法中处理通用对象的正确方法是什么?

问题描述:

这种情况是这样的:

我有这个可处理各种支付网关的网络API,并且我希望所有这些都具有相同的端点.

I have this web-api which will handle a variety of payment gateways, and I want to have the same endpoint for all of those.

所以,我要记住的是获取一些像这样的json数据:

So, what I have in mind is to get some json data like this:

{
    "OperationId":"N0004",
    "Generic_Object_That_Will_Change_According_ToThe_GateWay":
    {
        "Sale_id":1000,
        "CodUser":"1000040",
        "Email":"teste@teste.com"
    }
}

或者这,对于其他一些支付网关

Or this, for some other payment gateway

{
    "OperationId":"N044444",

    "Generic_Object_That_Will_Change_According_ToThe_GateWay":
    {
        "Token":1000,
        "UserSettings":{
            id: "4563345",
            name: "Average Joe"
        }        
    }
}

我想做的是在每个付款网关(贝宝或其他)的特定对象中转换此"Generic_Object_That_Will_Change_According_ToThe_GateWay",以防每个付款网关完全不同,但我不希望这影响付款方式客户端将调用此API-我希望它尽可能灵活,您只需要传递此Generic_Object_That_Will_Change_According_ToThe_GateWay中的所有数据,然后将其强制转换为适当的对象,然后调用另一个终结点(例如aggragate)微服务设计)传递此新创建的对象.

What I want to do is to transform this "Generic_Object_That_Will_Change_According_ToThe_GateWay" in the specific object for each payment gateway (paypal, or some other), becase each one is completely different, but I don't want that to affect the way the client will call this API - I want it to be as flexible as possible, in a way that you just have to pass whatever data in this Generic_Object_That_Will_Change_According_ToThe_GateWay, and I will then cast it to the proper object and then call another endpoint(like an aggragate microservice design) passing this newly created object.

到目前为止,我的想法是创建具有此类通用属性的类

My idea so far, was creating some class with a generic property like this

 public class Payment<Gateway>
    {
        public int OperationId{ get; set; }
        public Gateway paymentGateWay{ get; set; }
    }

此财产的payGateWay可以根据可用的付款网关输入.

And this property paymentGateWay could be typed according the available payment Gateways.

然后我可以在API方法中以Object的形式获取此数据,并进行必要的强制转换

And then maybe I could get this data in the API method as Object, and do the necessary casts

[Route("api/payment")]
[HttpPost]
public string Compra(Object payment) {

但是,老实说,我不知道自己是否做对了.

But, to be honest, I don't know if I'm in the right way.

我已经知道我在Web-api端点中没有通用方法-因此,考虑到此json数据的一部分是灵活的/通用的,并且可能在我的端点中获取此数据的正确方法是什么?被投射到一些不同的对象上.

I already know that I can't have a generic method in a web-api endpoint - so what would be the correct way to get this data in my endpoint considering that a part of this json data is flexible/generic and may be cast to a few different objects.

总而言之,我想处理可反序列化为几个不同的已知对象的json数据,但我不想在我的API中使用其他方法来处理每个可能的数据场景./strong>

To summarize, I want to handle json data that can be deserialized to a few different known objects, but I don't want to have a different method in my API to handle each one this possible data scenarios.

如果要在webapi中使用通用方法,则必须使用JObject类似于以下内容

if you want a generic method in webapi you have to use JObject something like the following

 public void Post([FromBody] JObject testJObject)
        {
            //here you have to do some additional work in order to parse and get it working for generic entity 
        }

除此之外,您可以针对任何收到的请求使用Schema验证器,并使用工厂模式来创建正确的对象

in addition to this, you can use the Schema validator against any received request and use the factory pattern in order to create the correct object

这里是一个示例

var json =
                " {\"OperationId\":\"N0004\",\"Generic_Object_That_Will_Change_According_ToThe_GateWay\":{\"Sale_id\":1000,\"CodUser\":\"1000040\"}}";

            JsonSchema paypalschema = new JsonSchema();
            paypalschema.Type = JsonSchemaType.Object;
            paypalschema.Properties = new Dictionary<string, JsonSchema>
            {
                {"OperationId", new JsonSchema {Type = JsonSchemaType.String}},
                {
                    "Generic_Object_That_Will_Change_According_ToThe_GateWay",
                    new JsonSchema {Type = JsonSchemaType.Object,Properties = new Dictionary<string, JsonSchema>
                    {
                        {"Sale_id", new JsonSchema {Type = JsonSchemaType.Integer}},
                        {"CodUser", new JsonSchema {Type = JsonSchemaType.String}},
                                                }}                   
                }
            };

        JObject requestObject = JObject.Parse( json);
            bool valid = requestObject.IsValid(paypalschema);
            if (valid)
            {
                //create your GatewayObject here 
            }
            //else check another gateway object