前回は例外に例外区分を付けて,メッセージボックスなどで利用できるようにしました。しかしこのままでは,各画面や各イベントのcatchのすべてにこのエラー処理を入れていかないといけなくなり,コードの重複が発生するため,これを共通化する方法を見ていきたいと思います。
BaseFormでエラー処理をする
共通化するためのロジックは,すべてのViewで共通なので,BaseFormに記述します。次のようにExceptionProcメソッドを追加します。内容はLatestViewのcatchの中身をそのままコピーしてきて貼り付けてください。
using NDDD.Domain; using NDDD.Domain.Exceptions; using System; using System.Windows.Forms; namespace NDDD.WinForm.Views { public partial class BaseForm : Form { public BaseForm() { InitializeComponent(); toolStripStatusLabel1.Visible = false; #if DEBUG toolStripStatusLabel1.Visible = true; #endif if (Shared.LoginId.Length > 0) { UserIdLabel.Text = "UserId=" + Shared.LoginId; } else { UserIdLabel.Text = string.Empty; } } protected void ExceptionProc(Exception ex) { string caption = "エラー"; MessageBoxIcon icon = MessageBoxIcon.Error; var exceptionBase = ex as ExceptionBase; if (exceptionBase != null) { if (exceptionBase.Kind == ExceptionBase.ExceptionKind.Info) { icon = MessageBoxIcon.Information; caption = "情報"; } else if (exceptionBase.Kind == ExceptionBase.ExceptionKind.Warning) { icon = MessageBoxIcon.Warning; caption = "警告"; } else if (exceptionBase.Kind == ExceptionBase.ExceptionKind.Error) { icon = MessageBoxIcon.Error; caption = "エラー"; } } MessageBox.Show(ex.Message, caption, MessageBoxButtons.OK, icon); } } }
アクセス修飾子はサブクラスであるViewからしか呼び出す必要がないので,protectedとしています。名前はExceptionProcとして引数はExceptionにしています。ここに各Viewから発生したExceptionを投げさせます。ちなみにこのExceptionはExceptionBaseかもしれないし,ただのExceptionかもしれません。メソッドを作ったらLatestViewのcatchの中身をコピーして貼り付けます。
LatestViewの変更
using NDDD.Domain.Exceptions; using NDDD.WinForm.ViewModels; using System; using System.Windows.Forms; namespace NDDD.WinForm.Views { public partial class LatestView : BaseForm { private LatestViewModel _viewModel = new LatestViewModel(); public LatestView() { InitializeComponent(); AreaIdTextBox.DataBindings.Add("Text", _viewModel, nameof(_viewModel.AreaIdText)); MeasureDateTextBox.DataBindings.Add("Text", _viewModel, nameof(_viewModel.MeasureDateText)); MeasureValueTextBox.DataBindings.Add("Text", _viewModel, nameof(_viewModel.MeasureValueText)); } private void SearchButton_Click(object sender, System.EventArgs e) { try { _viewModel.Search(); } catch (Exception ex) { base.ExceptionProc(ex); } } } }
LatestViewのcatchではBaseFormのExceptionProcを呼び出すように変更します。 これで,どれだけイベントが増えてもExceptionProcを呼び出すだけでよいので,コードの重複はなくなります。