我正在尝试为共享点创建一个库(类库),它将使所有共享点dll与共享点服务器进行交互以上传文件,文档,并创建文档库和文档集。
现在,此客户端(例如Web应用程序(asp.net webform或mvc)或控制台应用程序或Web api/wcf服务或Windows形式的)可以使用此库。
因此,在这里我有点困惑于创建存储库patterna和工作单元层以管理客户端上下文对象。
我不确定是否允许为每个操作创建客户端上下文,还是一次创建客户端上下文并重用,还是每次都创建clientcontext。
我已经进行了大量搜索,但是无法找到任何引用资料或文章来创建存储库层,就像在Entity framework DbContext
的情况下创建存储层一样。
我正在使用客户端对象模型(CSOM库),而我的库全部是关于内容管理系统来管理文件和元数据的。
我将不胜感激:)
更新:用于在sharepoint域上上传文件的示例代码
ClientContext context = new ClientContext("http://SiteUrl"); // Starting with ClientContext, the constructor requires a URL to the server running SharePoint.
context.Credentials = new SharePointOnlineCredentials(username, password);
// The SharePoint web at the URL.
Web myWeb = context.Web;
List myLibrary = myWeb.Lists.GetByTitle("MyProject"); //This is called document library so in sharepoint document Library is the root where we can create
//Document Set(Advance version of folder) or folder(Simple Folder) or upload files directly inside document library
FileCreationInformation info = new FileCreationInformation();
info.Url = "Sample.txt";
info.Overwrite = true;
info.Content = System.IO.File.ReadAllBytes("C://sample.txt"); //This will be user uploaded file that will be dynamic
myLibrary.RootFolder.Files.Add(info);
context.ExecuteQuery(); // Execute the query to the server.This is like EF SaveChanges method
一些引用:
https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff649690(v=pandp.10)
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code
https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee538685%28v%3doffice.14%29
https://sharepoint.stackexchange.com/questions/96180/sharepoint-2013-csom-is-it-better-to-pass-a-context-object-around-or-a-url
最佳答案
关于您的第一个问题:
为什么不让使用您的库的开发人员选择呢?即,您提供了上下文,他们可以对其进行初始化并将其保持在所需的范围内。
例如,如果您的库在网站中使用,则他们可能希望在每次请求时对其进行初始化,但是如果您的库在桌面应用程序中使用,则他们可能仅希望对每个窗口初始化一次。
关于第二个问题:
存储库和工作单元模式只是可以应用于任何上下文的抽象层。存储库将为给定对象或实体实现一组相关操作,而工作单元将为给定上下文内的一个或多个实体实现一组相关操作(数据库事务等)。
恕我直言,存储库对您要执行的操作没有多大意义,但是您可以实现包装ClientContext
实例的工作单元。
首先,从一个接口(interface)开始,定义您将公开的方法,例如:
public interface IContentManagerUnitOfWork
{
IEnumerable<List> GetLists();
List CreateList(ListCreationInformation listCreationInformation);
List GetListByTitle(string title);
[...]
}
然后实现它,这是一个主意:
public class ContentManagerUnitOfWork : IContentManagerUnitOfWork, IDisposable
{
private ClientContext clientContext;
private Web web;
public ContentManagerUnitOfWork(string url, username, password)
{
clientContext = new ClientContext(url);
clientContext .Credentials = new SharePointOnlineCredentials(username, password);
web = context.Web;
}
public IEnumerable<List> GetLists()
{
clientContext.Load(web.Lists);
clientContext.ExecuteQuery();
return web.Lists;
}
List CreateList(ListCreationInformation listCreationInformation)
{
List list = web.Lists.Add(listCreationInformation);
list.Update();
clientContext.ExecuteQuery();
return list;
}
List GetListByTitle(string title)
{
return web.Lists.GetByTitle("Announcements");
}
public void Dispose()
{
clientContext.Dispose();
}
}
然后,任何使用您的库的开发人员都可以简单地将工作单元与您提供的方法一起使用:
using (var unitOfWork = new ContentManagerUnitOfWork("http://SiteUrl", username, password))
{
var lists = unitOfWork.GetLists();
}
当然,这只是一个基本示例,您应根据需要对其进行调整,并确保它是与Sharepoint进行交互的正确方法。不过,我希望它能帮助您前进。
关于c# - 如何在单独的类库中管理客户端上下文对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53893774/