ILo*_*low 8 c# unit-of-work repository-pattern sharepoint-2013 csom
我正在尝试为sharepoint 创建一个库(类库),它将使所有sharepoint dll与sharepoint服务器交互,以上传文件,文档并创建文档库和文档集.
现在这个库可以被Web应用程序(asp.net webform或mvc)或控制台应用程序或web api/wcf服务或windows形成任何东西的客户端使用.
所以在这里我对创建存储库模式和工作单元层以便管理客户端上下文对象感到困惑.
我不确定是否允许为每个操作创建客户端上下文,或者是否创建客户端上下文一次并重新使用或是否每次都创建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
Run Code Online (Sandbox Code Playgroud)
一些参考文献:https: //docs.microsoft.com/en-us/previous-versions/msp-np/ff649690(v = pandp.10)
关于你的第一个问题:
我不确定是否允许为每个操作创建客户端上下文,或者是否创建一次客户端上下文并重用,或者是否每次都创建客户端上下文。
为什么不让使用你的库的开发人员选择呢?即,您提供上下文,他们可以初始化它并将其保留在他们需要的范围内。
例如,如果您的库在网站中使用,他们可能希望在每个请求时初始化它,但如果您的库在桌面应用程序中使用,他们可能只想每个窗口初始化一次。
关于你的第二个问题:
我进行了很多搜索,但无法找到任何参考或文章来创建存储库层,就像在实体框架 DbContext 中创建存储库层的方式一样。
存储库和工作单元模式只是可以应用于任何上下文的抽象层。存储库将为给定对象或实体实现一组相关操作,而工作单元将为给定上下文(数据库事务等)内的一个或多个实体实现一组相关操作。
恕我直言,存储库对于您想要做的事情没有多大意义,但是您可以实现包装实例的工作单元ClientContext。
首先,从接口开始,定义您将公开的方法,例如:
public interface IContentManagerUnitOfWork
{
IEnumerable<List> GetLists();
List CreateList(ListCreationInformation listCreationInformation);
List GetListByTitle(string title);
[...]
}
Run Code Online (Sandbox Code Playgroud)
然后你实现它,这是一个想法:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,任何使用您的库的开发人员都可以通过您提供的方法简单地使用工作单元:
using (var unitOfWork = new ContentManagerUnitOfWork("http://SiteUrl", username, password))
{
var lists = unitOfWork.GetLists();
}
Run Code Online (Sandbox Code Playgroud)
当然,这只是一个不同的基本示例,您应该根据您的需求进行调整,并确保它是与 Sharepoint 交互的正确方式。不过,我希望它能让你继续前进。
| 归档时间: |
|
| 查看次数: |
831 次 |
| 最近记录: |