我希望能够使用两个不同的SharePoint列表(使用不同的Web服务调用):
例如,现在我有两个几乎相同的不同类:
using System;
using System.Net;
using System.Xml;
namespace POSAutomation.Common
{
internal class SpFunctions
{
private sp.canada.Lists _spService;
public sp.canada.Lists SpService
{
get { return _spService ?? (_spService = new sp.canada.Lists()); }
}
public virtual void UpdateSpListItems(string listname, string updateXml, string user, string pw,
string domain)
{
if (String.IsNullOrEmpty(updateXml))
{
return;
}
SpService.Credentials = new NetworkCredential(user, pw, domain);
var doc = new XmlDocument();
XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
batchElement.SetAttribute("ViewName", "");
batchElement.InnerXml = updateXml;
XmlNode rNode = _spService.UpdateListItems(listname, batchElement);
XmlNamespaceManager nsm = new XmlNamespaceManager(rNode.OwnerDocument.NameTable);
nsm.AddNamespace("sp", rNode.NamespaceURI);
XmlNodeList results = rNode.SelectNodes("//sp:ErrorCode", nsm);
if (results != null)
foreach (XmlNode result in results)
{
string errorCode = result.InnerText;
if (errorCode == "0x00000000") continue;
("Error Updating SharePoint List: " + errorCode).LogError();
("List Name: " + listname).LogError();
("Update XML: " + updateXml).LogError();
throw new Exception("Error Updating SharePoint List: " + errorCode);
}
}
public XmlNodeList RetrieveSpItems(string listname, string query, string user, string pw,
string domain)
{
XmlNodeList oNodes = null;
XmlNode ndListItems = null;
XmlNode ndQueryOptions = null;
XmlNode ndViewFields = null;
XmlNode ndQuery = null;
XmlDocument xmlDoc = null;
SpService.Credentials = new NetworkCredential(user, pw, domain);
xmlDoc = new XmlDocument();
ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml = "<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>";
ndViewFields.InnerXml = "";
ndQuery.InnerXml = query;
ndListItems = SpService.GetListItems(listname, null, ndQuery, ndViewFields, null, ndQueryOptions, null);
oNodes = ndListItems.ChildNodes;
return oNodes;
}
}
}
另一个类包含几乎完全相同的代码,但SpService属性除外,因为它在单独的列表上运行(通过单独的Web服务调用):
private sp.pr.Lists _spService;
public sp.pr.Lists SpService
{
get { return _spService ?? (_spService = new sp.pr.Lists()); }
}
注意区别:
private sp.canada.Lists _spService;
public sp.canada.Lists SpService
{
get { return _spService ?? (_spService = new sp.canada.Lists()); }
}
如何简化这两个类,以便我只有一个包含所有代码的主要类,以及两个较小的类(每个Web服务一个),它们告诉主要类要引用哪个Web服务?
基本上,我想创建一个internal abstract SpFunctions
类,其中包含UpdateSPListItems()
和RetrieveSpItems()
的代码,较小的类只包含SpService
属性。这可能吗?
答案 0 :(得分:1)
如果Lists类共享一个接口或者是从同一个类中进行子类化,那么您可以将它作为注入项用于基类中,您将从中对类列表特定的类进行子类化。否则,如果它们不共享公共库,但为您要调用的操作共享相同的方法签名,则可以使用动态类型来延迟绑定来自基类的调用。
选项#1:
班级名单 { 保护只读TList列表; 清单(TList清单) { this.list = list; }
void Foo() { this.list.Foo(); } }
class CanadaList:List { }
class PrList:List { }
选项#2
班级名单 { 受保护的只读动态列表; 列表(对象列表) { this.list = list; }
void Foo() { this.list.Foo(); } }
class CanadaList:List { }
class PrList:List { }
选项#3
班级名单 { 受保护的只读对象列表; 列表(对象列表) { this.list = list; }
void Foo() { 输入t = this.list.GetType();
t.GetMethod("Foo").Invoke(this.list, null);
} }