简体   繁体   English

编辑多级嵌套对象列表的正确方法

[英]Proper way to edit multi-level nested list of objects

I have 3 objects: 我有3个对象:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public List<Order> Orders { get; set; }

    public Person()
    {
        Orders= new List<Order>();
    }
}

public class Order
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string Date { get; set; }
    public List<Item> Items { get; set; }

    public Order()
    {
        Items= new List<Item>();
    }
}

public class Item
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public int Number { get; set; }
}

As You can see every person can have multiple orders and every order can have multiple items. 正如您所看到的,每个人都可以拥有多个订单,每个订单都可以包含多个商品。

In my application data that comes from DB looks like so: 在我的应用程序中,来自DB的数据如下所示:

private List<Person> _persons;

_persons = new List<Person>
    {
     new Person
      {
       Id = 1,
       Name = "John",
       Surname = "Smith",
       Orders = new List<Order>
        {
         new Order
          {
           Id = 1,
           Description = "First Order",
           Date = "2013-03-07",
           Items =
            new List<Item>
             {
              new Item {Id = 1, Number = 2, ProductName = "Chair"},
              new Item {Id = 2, Number = 1, ProductName = "Bed"}
             }
          },
         new Order
          {
           Id = 2,
           Description = "Second",
           Date = "2013-03-07",
           Items =
            new List<Item>
             {
              new Item {Id = 1, Number = 2, ProductName = "Pen"},
              new Item {Id = 2, Number = 1, ProductName = "Pencil"}
             }
          }
        }
      },
      new Person
      {
       Id = 2,
       Name = "Adam",
       Surname = "West",
       Orders = new List<Order>
        {
         new Order
          {
           Id = 1,
           Description = "Adams order",
           Date = "2013-03-07",
           Items =
            new List<Item>
             {
              new Item {Id = 1, Number = 2, ProductName = "first"},
              new Item {Id = 2, Number = 1, ProductName = "second"}
             }
          },
         new Order
          {
           Id = 2,
           Description = "Adams second",
           Date = "2013-03-07",
           Items =
            new List<Item>
             {
              new Item {Id = 1, Number = 2, ProductName = "Pen"},
              new Item {Id = 2, Number = 1, ProductName = "Pencil"}
             }
          }
        }
      }
    };

I've created custom User control with 2 labels and datagridview like this: 我用2个标签和datagridview创建了自定义用户控件,如下所示: 在此输入图像描述 below is my code: 下面是我的代码:

using System.Windows.Forms;
using Demo.Model;

namespace Demo.Controls
{
    public partial class OrderView : UserControl
    {
        private Order _order;

        public Order Order
        {
            get { return _order; }
            set
            {
                _order = value;
                UpdateView();
            }
        }

        private void UpdateView()
        {
            if (_order == null) return;
            IdLBL.Text = string.Format("ID: {0}", _order.Id);
            DateLBL.Text = string.Format("Date: {0}", _order.Date);

            ItemsDGV.DataSource = _order.Items;
        }

        public OrderView()
        {
            InitializeComponent();
        }
    }
}

Then in main form I'm adding instances of that control to flowLayoutPanel (for every order for specific person): 然后在主窗体中我将该控件的实例添加到flowLayoutPanel(针对特定人员的每个订单):

private void RefreshView()
{
   flowLayoutPanel1.Controls.Clear();
   foreach (Order order in _persons[_currentPerson].Orders)
   {
      flowLayoutPanel1.Controls.Add(new OrderView {Order = order});
   }
}

With above data my application looks like so: 使用上面的数据,我的应用程序如下所示 在此输入图像描述

I need to be able to add/edit every item of every order. 我需要能够添加/编辑每个订单的每个项目。 Adding seems to be quite easy-I'll create new form, user will input details and then I'll do DB call to add that item. 添加似乎很容易 - 我将创建新表单,用户将输入详细信息,然后我将进行数据库调用以添加该项目。

My questions are: 我的问题是:
How can I auto refresh view after add/edit item? 添加/编辑项目后如何自动刷新视图? Can I somehow bind my control to that list to every time I update it my view updates. 我可以以某种方式将我的控件绑定到该列表,每次我更新它的视图更新。 I need to be able to add items and orders to person. 我需要能够向人们添加物品和订单。 What would be the easiest way of doing that? 最简单的方法是什么?

Is this kind of display correct? 这种显示器是否正确? Can I improve it? 我能改进吗? If yes then how? 如果是,那怎么样?

One way to solve this problem would be to use a polling mechanism. 解决此问题的一种方法是使用轮询机制。 This can be plumbed in using the System.Threading.Timer , which uses the System.Threading.ThreadPool. 这可以在使用System.Threading.Timer时使用System.Threading.ThreadPool进行检测。

You would repeatedly query to see if your data has been updated. 您将反复查询以查看您的数据是否已更新。 You'll need to store the modified date with your data, I'd recommend storing it in UTC. 您需要将修改日期与数据一起存储,我建议将其存储在UTC中。 Alternatively, you could just use an integer tag that is incremented every time data is updated, then if the tag is out of date, you know you need to update your displayed data. 或者,您可以使用每次更新数据时递增的整数标记,然后如果标记过期,您就知道需要更新显示的数据。

The dangerous thing about polling refreshes is how much load your server will encounter, but if your concurrent user base is small, this will not be a problem. 轮询刷新的危险之处在于服务器将遇到多少负载,但如果您的并发用户群很小,这将不会成为问题。 If you find it causes performance problems, you can adjust your polling interval, incorporate caching techniques, and possibly farm your server(s). 如果您发现它会导致性能问题,您可以调整轮询间隔,合并缓存技术,并可能对服务器进行托管。

Your new OrderView class will probably look similar to the following: 您的新OrderView类可能类似于以下内容:

using System.Windows.Forms;
using Demo.Model;
using System.Threading;

namespace Demo.Controls
{
    public partial class OrderView : UserControl, IDisposable
    {
        private Order _order;
        private Timer poller;

        public Order Order
        {
            get { return _order; }
            set
            {
                _order = value;
                UpdateView();
            }
        }

        private void UpdateView()
        {
            if (_order == null) return;
            IdLBL.Text = string.Format("ID: {0}", _order.Id);
            DateLBL.Text = string.Format("Date: {0}", _order.Date);

            ItemsDGV.DataSource = _order.Items;
        }

        public OrderView()
        {
            InitializeComponent();
            _poller = new Timer(CheckUpdate, null, timeSpan, timeSpan);
        }

        private void CheckUpdate(Object state)
        {
            //Do update check and update Order if it has changed
        }

        public void Dispose()
        {
            if (_poller != null)
            {
                _poller.Dispose();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM