将类方法转换为具有支持字段的属性

时间:2015-08-18 23:38:09

标签: c# sharepoint properties

我有一个简单的类,它从SharePoint中检索要由WPF应用程序使用的详细信息。

我的班级看起来像这样:

using Microsoft.SharePoint.Client;

/// <summary>
/// Class for loading orders from SharePoint and interacting with them
/// </summary>
public static class Orders
{
    /// <summary>
    /// Loads orders from SharePoint and puts them into a ListItemCollection
    /// </summary>
    /// <returns>
    /// The <see cref="ListItemCollection"/>.
    /// </returns>
    public static ListItemCollection Load()
    {
        using (var ctx = new ClientContext("http://sharepoint/resources"))
        {
            var list = ctx.Web.Lists.GetByTitle("Resource Orders");

            var query = new CamlQuery
                            {
                                ViewXml =
                                    @"<Where><Or><Eq><FieldRef Name=""Status""></FieldRef><Value Type=""Text"">Approved</Value></Eq><IsNotNull><FieldRef Name=""Status"" /></FieldRef></IsNotNull></Or></Where>"
                            };

            ListItemCollection collListItem = list.GetItems(query);

            ctx.Load(
                collListItem,
                items =>
                items.Include(
                    item => item.Id,
                    item => item.DisplayName,
                    item => item.HasUniqueRoleAssignments,
                    item => item["Persona"],
                    item => item["Quantity_x0020_Ordered"],
                    item => item["Resource_x0020_Name"],
                    item => item["Title"],
                    item => item["Customer_x0020_E_x002d_mail"],
                    item => item["Customer_x0020_Phone_x0020_Numbe"],
                    item => item["Customer_x0020_Street"],
                    item => item["Customer_x0020_Suburb"],
                    item => item["Customer_x0020_Postcode"],
                    item => item["Organization"]));

            ctx.ExecuteQuery();

            return collListItem;
        }
    }
}

该类的用法如下所示:

private void buttonRefreshOrders_Click(object sender, RoutedEventArgs e)
{
    var collListItem = Orders.Load();

    if (!collListItem.Any())
    {
        MessageBox.Show(
            "No resource orders are currently within the queue.",
            "Order Center",
            MessageBoxButton.OK,
            MessageBoxImage.Information);

        return;
    }

    var customers = new ObservableCollection<Customer>();

    foreach (var customer in
        collListItem.Select(
            item =>
            new
                {
                    Persona = item["Persona"].ToString(),
                    CustomerName = item["Title"].ToString(),
                    Email = item["Customer_x0020_E_x002d_mail"].ToString(),
                    Organization = item["Organization"].ToString(),
                    PhoneNumber = item["Customer_x0020_Phone_x0020_Numbe"].ToString(),
                    Street = item["Customer_x0020_Street"].ToString(),
                    Suburb = item["Customer_x0020_Suburb"].ToString(),
                    Postcode = item["Customer_x0020_Postcode"].ToString()
                })
            .Distinct()
            .Select(
                r =>
                new Customer
                    {
                        Persona = r.Persona,
                        CustomerName = r.CustomerName,
                        Email = r.Email,
                        Organization = r.Organization,
                        PhoneNumber = r.PhoneNumber,
                        Street = r.Street,
                        Suburb = r.Suburb,
                        Postcode = r.Postcode
                    }))
    {
        customers.Add(customer);
    }

    dataGridOutstandingOrders.ItemsSource = customers;
}

从这个类加载的数据将在整个应用程序的各个领域中使用,而不仅仅是在这里。为了避免每次引用它都必须为它命中,我相信我可以(应该?)制作

public static ListItemCollection Load()
{

进入具有支持字段的属性。我将如何接近这个?

1 个答案:

答案 0 :(得分:1)

你可以这样做..

public static class Orders
{
    private static ListItemCollection _cache;
    public static ListItemCollection Cache{ get{return _cache??(_cache = LoadListItemCollection());}

    public static ListItemCollection LoadListItemCollection()
    //your code
}

// One more apporach
public static class Orders
{
    private static ListItemCollection _cache;
    public static ListItemCollection Cache{ get{return _cache??LoadListItemCollection(true);}

    public static ListItemCollection LoadListItemCollection(bool refreshList=false)
    { 
          if(!refreshList && _cache!=null)
          {
               return Cache;
          }
          //your code
          ListItemCollection collListItem = list.GetItems(query);
          //your code
          _cache = collListItem;
          return collListItem;
    }
}