将参数作为Xml传递到存储过程 [英] Passing Parameters as Xml to a Stored Procedure

查看:78
本文介绍了将参数作为Xml传递到存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将参数作为Xml传递到我的存储过程.

I've got a requirement to pass parameters as Xml to my stored procedures.

我在中间层有一个WCF服务,该服务对我的数据层进行调用,然后依次将请求转发到适当的存储过程.

I have a WCF service in the middle tier that makes calls to my data layer which in turn forwards the request to the appropriate stored procedure.

设计是WCF服务负责构建Xml以传递到存储库.

The design is that the WCF service is responsible for building the Xml to pass to the Repository.

我只是想知道是要控制中间层Xml中包含哪些参数,还是要使用由客户端建立的Dictionary,然后将其转换为中间层Xml?

I'm just wondering whether to keep control of what parameters are contained within the Xml in the middle tier or use a Dictionary built up by the client which I then convert to Xml in the middle tier?

此刻,我已经选择了后者-例如:

At the moment I've gone for the latter - for example:

 public TestQueryResponseMessage TestQuery(TestQueryRequestMessage message)
 {
        var result = Repository.ExecuteQuery("TestQuery", ParamsToXml(message.Body.Params));

        return new TestQueryResponseMessage
        {
            Body = new TestQueryResponse
            {
                TopicItems = result;
            }
        }
    }


private string ParamsToXml(Dictionary<string, string> nvc)
{
        //TODO: Refactor
        StringBuilder sb = new StringBuilder();

        sb.Append("<params>");
        foreach (KeyValuePair<string, string> param in nvc)
        {
            sb.Append("<param>");
            sb.Append("<" + param.Key + ">");
            sb.Append(param.Value);
            sb.Append("</" + param.Key + ">");
            sb.Append("</param>");
        }
        sb.Append("</params>");

        return sb.ToString();
}

但是,我可能需要第一种方法.例如

However I might need to do it the first way. E.g.

public TestQueryResponseMessage TestQuery(TestQueryRequestMessage message)
{
       string xml = string.Format("<params><TestParameter>{0}</TestParameter></params>",message.Body.TestParameter)

       var result = Repository.ExecuteQuery("TestQuery", xml);

      return new TestQueryResponseMessage
      {
          Body = new TestQueryResponse
          {
                    TopicItems = result;
          }
      }
}

蜂巢推荐什么?

推荐答案

如果必须使用xml;然后,我不使用字典来代替,而是使用表示该数据的类,并使用 XmlSerializer 将其作为xml提取:

If you must use xml; then rather than passing around a dictionary, I'd use a class that represents that data, and use XmlSerializer to fetch it as xml:

[Serializable, XmlRoot("args")]
public class SomeArgs {
    [XmlElement("foo")] public string Foo { get; set; } 
    [XmlAttribute("bar")] public int Bar { get; set; }
}
...
SomeArgs args = new SomeArgs { Foo = "abc", Bar = 123 };
XmlSerializer ser = new XmlSerializer(typeof(SomeArgs));
StringWriter sw = new StringWriter();
ser.Serialize(sw, args);
string xml = sw.ToString();

这使得以面向对象的方式管理哪些参数应用于哪些查询变得更加容易.这也意味着您不必自己进行xml转义...

This makes it much easier to manage which arguments apply to which queries, in an object-oriented way. It also means you don't have to do your own xml escaping...

这篇关于将参数作为Xml传递到存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆