共通ドキュメント

Form

概要

ユーザーインターフェース(フォーム)を実装する。

クラス名
(システムプロジェクト名称).(サブシステム名称).(機能名称).(エンティティ名称 + "Form")
エンティティに対していくつかのフォームがある場合、機能により名称を分ける。
namespace
(システムプロジェクト名称).(サブシステム名称).(機能名称)
継承元
System.Windows.Forms.Form
インターフェース
N/A
クラス属性
N/A

フィールド

以下のフィールドを定義する

  • 入力対象のエンティティ
  • 対象DataFormAdapter

プロパティ

必要なプロパティを定義する。

コンストラクタ

必要に応じてコンストラクタを定義する。

  • デフォルトコンストラクタは必ず定義する。

メソッド

データ入力のためのメソッドを定義する。

InitializeDataFormWrapper()DataFormWrapper初期化メソッド。Formを初期化する時に呼び出す。DataFormWrapperの各要素に適切なコントロール、フィルター、検証を設定する。
Get[Entity]()エンティティ取得メソッド。DataAccessから必要なエンティティを取得して返す。
Save[Entity]()エンティティ保存メソッド。DataAccessを呼び出すしエンティティを保存する。
Update[Entity]()エンティティ保存メソッド(特に「更新のみ」の場合)。DataAccessを呼び出すしエンティティを保存する。
Insert[Entity]()エンティティ保存メソッド(特に「挿入のみ」の場合)。DataAccessを呼び出すしエンティティを保存する。
Validate[Entity]()エンティティ検証メソッド。現在のエンティティの検証を実行する。

注意
  • 上記「[Entity]を適宜実際のエンティティクラス名称に置き換える。
  • データアクセスコードは例外をキャッチし、然るべき例外処理を行う。

イベント処理

イベントに反応するためのメソッドを定義する。

(保存トリガーイベント)
  • DataFormWrapper#Bindを呼び出す。
  • DataFormAdapter#IsValidがfalseの場合後続処理は実行しない。
  • エンティティ保存メソッドを呼び出す。
(取得トリガーイベント)エンティティ取得メソッドを呼び出す。

注意
  • エンティティの内容によって挿入か更新かが決定される場合、イベント処理内で挿入か更新かを分けない。それを分けるのはエンティティ保存メソッドか、データアクセス内

注意

  • データ接続は直接使用しない。
  • 可能な限りログを採取する。
  • フォームの入力コントロールから直接データを取得しない。また、直接データを設定しない。DataFormAdapterに任せる。
  • TODO:出力専用コントロールの扱い()

サンプル

FXトラベル/マスタ保守/都市、地域/都市編集フォーム(C#)


namespace FxTravel.Maintainance.Area.Form
{
    public partial class CityEditForm : Form
    {
        /// 
        /// ログ出力
        /// 
        static Logger logger = LoggerFactory.GetLogger("default", MethodBase.GetCurrentMethod().ReflectedType.Name);
        /// 
        /// 編集都市
        /// 
        City city;
        MstCityTableProperties cityProperties;
        CityEditFormAdapter cityEditFormAdapter;


        public CityEditForm()
        {
            InitializeComponent();

            this.InOutType1.Tag = 1;
            this.InOutType2.Tag = 2;
            this.InOutType3.Tag = 3;

            this.cityProperties = new MstCityTableProperties();
            this.cityEditFormAdapter = new CityEditFormAdapter();
            this.CreateFormAdapter();
            //comboのデータソース
            TraTypeSource traSource = new TraTypeSource();
            List traTypes = traSource.GetSource();
            this.TraCityType.DataSource = traTypes;
            this.TraCityType.ValueMember = "Id";
            this.TraCityType.DisplayMember = "Name";


        }
        void CreateInputForm()
        {
            this.cityEditFormAdapter.CityCode = new TextBoxElement(
                this.cityCode,
                new StringFilter(this.cityProperties.CityCode),
                FieldValidator.GetInstance(this.cityProperties.CityCode)
                );
            this.cityEditFormAdapter.JpnName = new TextBoxElement(
                this.JpnName,
                new StringFilter(this.cityProperties.JpnName),
                FieldValidator.GetInstance(this.cityProperties.JpnName)
                ); ;
            this.cityEditFormAdapter.JpnShortName = new TextBoxElement(
                this.JpnShortName,
                new StringFilter(this.cityProperties.JpnShortName),
                FieldValidator.GetInstance(this.cityProperties.JpnShortName)
                );
            this.cityEditFormAdapter.AnkName = new TextBoxElement(
                this.AnkName,
                new StringFilter(this.cityProperties.AnkName),
                FieldValidator.GetInstance(this.cityProperties.AnkName)
                );
            this.cityEditFormAdapter.EngName = new TextBoxElement(
                this.EngName,
                new StringFilter(this.cityProperties.EngName),
                FieldValidator.GetInstance(this.cityProperties.EngName)
                );
            this.cityEditFormAdapter.EngShortName = new TextBoxElement(
                this.EngShortName,
                new StringFilter(this.cityProperties.EngShortName),
                FieldValidator.GetInstance(this.cityProperties.EngShortName)
                );
            this.cityEditFormAdapter.KeyName = new TextBoxElement(
                this.KeyName,
                new StringFilter(this.cityProperties.KeyName),
                FieldValidator.GetInstance(this.cityProperties.KeyName)
                );
            this.cityEditFormAdapter.CountryCode = new TextBoxElement(
                this.CountryCode,
                new StringFilter(this.cityProperties.CountryCode),
                FieldValidator.GetInstance(this.cityProperties.CountryCode)
                );
            this.cityEditFormAdapter.StateCode = new TextBoxElement(
                this.StateCode,
                new StringFilter(this.cityProperties.StateCode),
                FieldValidator.GetInstance(this.cityProperties.StateCode)
                );
            this.cityEditFormAdapter.AreasCode = new TextBoxElement(
                this.AreasCode,
                new StringFilter(this.cityProperties.AreasCode),
                FieldValidator.GetInstance(this.cityProperties.AreasCode)
                );
            this.cityEditFormAdapter.Gmt = new TextBoxElement(
                this.Gmt,
                new NumberFilter(this.cityProperties.Gmt),
                FieldValidator.GetInstance(this.cityProperties.Gmt)
                );
            this.cityEditFormAdapter.SummerGmt = new TextBoxElement(
                this.SummerGmt,
                new NumberFilter(this.cityProperties.SummerGmt),
                FieldValidator.GetInstance(this.cityProperties.SummerGmt)
                );
            this.cityEditFormAdapter.SummerFrom = new TextBoxElement(
                this.SummerFrom,
                new DateTimeFilter(CFW.DateTimeInputFormat.DATE_LONG),
                FieldValidator.GetInstance(this.cityProperties.SummerFrom),
                CFW.DateTimeOutputFormat.LONG_DATE
                );
            this.cityEditFormAdapter.SummerTo = new TextBoxElement(
                this.SummerTo,
                new DateTimeFilter(CFW.DateTimeInputFormat.DATE_LONG),
                FieldValidator.GetInstance(this.cityProperties.SummerTo),
                CFW.DateTimeOutputFormat.LONG_DATE
                );
            this.cityEditFormAdapter.InOutType = new RadioButtonGroupElement(
                new RadioButton[] { this.InOutType1, this.InOutType2, this.InOutType3 },
                new NumberFilter(this.cityProperties.InOutType),
                FieldValidator.GetInstance(this.cityProperties.InOutType)
                );
            this.cityEditFormAdapter.TraCityType = new DropDownListElement(
                this.TraCityType,
                new NumberFilter(this.cityProperties.TraCityType),
                FieldValidator.GetInstance(this.cityProperties.TraCityType)
                );
        }
        public DialogResult  NewCity(Form owner)
        {
            city = new City();
            city.IsModified = false;
            this.inputForm.SetDataSource(city);
            this.inputForm.RenderView();
            return ShowDialog(owner);
        }
        public DialogResult EditCity(string cityCode, Form owner)
        {
            LoadCity(cityCode);
            city.IsModified = false;
            this.inputForm.SetDataSource(city);
            this.inputForm.RenderView();

            return ShowDialog(owner);
        }
        void LoadCity(string cityCode)
        {
            CityAccess access = new CityAccess();
            this.city = access.GetCity(cityCode);
        }
        void SaveCity()
        {
            CityAccess access = new CityAccess();
            access.Save(city);
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!city.IsModified)
            {
                MessageBox.Show("何も変更していません。", "確認", MessageBoxButtons.OK);
            }
            this.cityEditFormAdapter.Bind();
            if (!this.inputForm.IsValid)
            {
                MessageBox.Show("エラーがあります.");
                this.DialogResult = DialogResult.None;
                return;
            }
            this.cityEditFormAdapter.UpdateDataSource();
            this.city = (City)this.inputForm.GetDataSource();
            SaveCity();

            this.Close();
        }

        private void CityEditForm_Load(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
        }

        private void city_TextChanged(object sender, EventArgs e)
        {
            if (this.city != null)
            {
                this.city.IsModified = true;
                this.button1.Enabled = true;
            }

        }

        private void CityEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (city != null)
            {
                if (city.IsModified)
                {
                    DialogResult result =  MessageBox.Show("変更されています。閉じていいですか?","確認",MessageBoxButtons.YesNo,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1);
                    if(result != DialogResult.Yes)
                    {
                        e.Cancel = true;
                    }
                }
            }
        }
    }
}