using System; using System.Windows.Forms; using YourNamespace.Entities; using YourNamespace.Repositories; namespace YourNamespace.Forms { public partial class FormPlane : Form { private readonly IPlaneRepository _planeRepository; private int? _planeId; public int Id { set { try { var plane = _planeRepository.ReadPlaneById(value); if (plane == null) { throw new InvalidDataException(nameof(plane)); } textBoxModel.Text = plane.Model; numericUpDownCapacity.Value = plane.Capacity; _planeId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormPlane(IPlaneRepository planeRepository) { InitializeComponent(); _planeRepository = planeRepository ?? throw new ArgumentNullException(nameof(planeRepository)); } private void ButtonSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(textBoxModel.Text)) { throw new Exception("Имеются незаполненные поля"); } if (_planeId.HasValue) { _planeRepository.UpdatePlane(CreatePlane(_planeId.Value)); } else { _planeRepository.CreatePlane(CreatePlane(0)); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private Plane CreatePlane(int id) => Plane.CreateEntity(id, textBoxModel.Text, (int)numericUpDownCapacity.Value); } }