The correct way to edit a multi-level nested list of objects

I have 3 objects:

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, each person can have several orders, and each order can have several elements.

In my data, the applications that come from the database look like this:

 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 created a user control with two labels and a datagridview as follows: enter image description here 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 the main form, I add instances of this flowLayoutPanel control (for each order for a specific person):

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

With the above data, my application looks like this: enter image description here

I need to be able to add / edit each item of each order. Adding seems quite simple - I will create a new form, the user will enter the details, and then I will make a call to the database to add this element.

My questions:
How can I automatically update the view after adding / editing an item? Can I somehow attach my control to this list every time I update it to update my view. I need to be able to add items and orders to a person. What would be the easiest way to do this?

Is this view displayed correctly? Can i improve it? If so, how?

+4
source share
1 answer

One way to solve this problem is to use the polling mechanism. This can be done using System.Threading.Timer , which uses System.Threading.ThreadPool.

You will be asked again if your data is updated. You will need to save the modified date with your data, I would recommend storing it in UTC. Alternatively, you can simply use an integer tag that increments each time the data is updated, and then if the tag is out of date, you know that you need to update the displayed data.

The danger with polling coverage is how much your server will load, but if your concurrent user base is small, that won't be a problem. If you find that this causes performance problems, you can adjust the polling interval, enable caching methods, and possibly farm on your servers.

Your new OrderView class will probably look something like this:

 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(); } } } } 
+1
source

All Articles