using GasStation.Repositories; using Unity; namespace GasStation.Forms { public partial class FormSupplies : Form { private readonly IUnityContainer _container; private readonly ISupplyRepository _supplyRepository; public FormSupplies(IUnityContainer container, ISupplyRepository supplyRepository) { InitializeComponent(); _container = container ?? throw new ArgumentNullException(nameof(container)); _supplyRepository = supplyRepository ?? throw new ArgumentNullException(nameof(supplyRepository)); } 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 void ButtonDel_Click(object sender, EventArgs e) { if (!TryGetIDFromSelectedRow(out var findID)) { return; } if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } try { _supplyRepository.DeleteSupply(findID); LoadList(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка удаления", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void LoadList() => dataGridViewData.DataSource = _supplyRepository.ReadSupply(); private void FormSupplies_Load(object sender, EventArgs e) { try { LoadList(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private bool TryGetIDFromSelectedRow(out int id) { id = 0; if (dataGridViewData.SelectedRows.Count < 1) { MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } id = Convert.ToInt32(dataGridViewData.SelectedRows[0].Cells["ID"].Value); return true; } } }