using ProjectLibrary.Repositories; using Unity; namespace ProjectLibrary.Forms { public partial class FOrders : Form { private readonly IUnityContainer _container; private readonly IOrderRepository _orderRepository; public FOrders(IUnityContainer container, IOrderRepository orderRepository) { InitializeComponent(); _container = container ?? throw new ArgumentNullException(nameof(container)); _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); } private void LoadList() { dataGridViewOrders.DataSource = _orderRepository.ReadOrders(); } private void buttonAdd_Click(object sender, EventArgs e) { try { _container.Resolve().ShowDialog(); LoadList(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private bool TryGetIdentifierFromSelectedRow(out int id) { id = 0; if (dataGridViewOrders.SelectedRows.Count < 1) { MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } id = Convert.ToInt32(dataGridViewOrders.SelectedRows[0].Cells["Id"].Value); return true; } private void buttonUpdate_Click(object sender, EventArgs e) { if (!TryGetIdentifierFromSelectedRow(out var findId)) { return; } try { var form = _container.Resolve(); form.Id = findId; form.ShowDialog(); LoadList(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonRemove_Click(object sender, EventArgs e) { if (!TryGetIdentifierFromSelectedRow(out var findId)) { return; } if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } try { _orderRepository.DeleteOrder(findId); LoadList(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void FOrders_Load(object sender, EventArgs e) { try { LoadList(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }