如何在单独的类库中管理客户端上下文对象?

时间:2018-12-22 07:14:33

标签: c# sharepoint-2013 repository-pattern unit-of-work csom

我正在尝试为共享点创建库(类库),该共享点将具有所有共享点dll与共享点服务器进行交互以上载文件,文档并创建文档库和文档集< / strong>。

现在,客户端可以使用此库,例如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

2 个答案:

答案 0 :(得分:3)

关于第一个问题:

  

我不确定是否允许为每个创建客户端上下文   操作或是否一次创建客户端上下文并重用或   是否每次都创建clientcontext。

为什么不让使用您的库的开发人员选择?即,您提供了上下文,他们可以对其进行初始化并将其保持在所需的范围内。

例如,如果您的库在网站中使用,则他们可能希望在每次请求时对其进行初始化,但是,如果您的库在桌面应用程序中使用,则他们可能仅希望对每个窗口初始化一次。

关于第二个问题:

  

我搜索了很多,但是找不到任何参考或文章   创建存储库层,就像在实体的情况下创建存储库层一样   框架DbContext。

存储库和工作模式单元只是可以应用于任何上下文的抽象层。存储库将为给定的对象或实体实施一组相关操作,而工作单元将为给定上下文内的一个或多个实体实施一组相关操作(数据库事务等)。

恕我直言,存储库对于您要执行的操作没有多大意义,但是您可以实现一个包装ClientContext实例的工作单元。

首先,从一个接口开始,定义您将公开的方法,例如:

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进行交互的正确方法。我希望它能使您前进。

答案 1 :(得分:0)

我认为您需要在WCF服务中创建一个如下所示的身份验证服务,该服务将照顾所有登录的用户当有任何新请求进入时要获取该用户的详细信息(如果是真实用户),然后执行相应地

    [ServiceContract]
    public interface IAuthenticationService
    {
       [OperationContract]
       ClientInfo ChangeSessionUser(User user);
       [OperationContract]
       ClientInfo GetSessionUserInfo();
       [FaultContract(typeof(ServiceFault))]
       [OperationContract]
       ClientInfo Login(LoginCredentials credentials);
       [OperationContract]
       void Logout(string id);
       [FaultContract(typeof(ServiceFault))]
       [OperationContract]
       void RemoveInvalidUserLogins();
       [OperationContract]
       void RemoveSesseionFromCache(string id);
       [FaultContract(typeof(ServiceFault))]
       [OperationContract]
       bool ValidateUser(LoginCredentials credentials);
    }