将 class 方法转换为带有支持字段的 属性

Converting a class method to a property with a backing field

我有一个简单的 class 从 SharePoint 检索详细信息以供 WPF 应用程序使用。

我的 class 看起来像这样:

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;
        }
    }
}

class 的用法如下所示:

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;
}

从此 class 加载的数据将用于整个应用程序的各个领域,而不仅仅是这里。为了避免每次引用它时都必须为它点击 SharePoint,我相信我可以(应该?)make

public static ListItemCollection Load()
{

进入带有支持字段的 属性。我将如何着手解决这个问题?

你可以这样做..

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;
    }
}