S2Dao.NET を使ってみる(2)

これの続き。
昨日の状態だと全データが見れるだけなので、もう少しアプリケーションらしくしてみる。
追加機能は以下の3つ。

  • データの追加(社員番号の最大値+1のデータを追加)
  • データの修正(テーブル上で選択しているレコードを修正)
  • データの削除(テーブル上で選択しているレコードを削除)

そのソース一式をここに置いておく。

Dao インターフェイスの修正

まず、社員番号の最大値を取得するメソッドを追加する。さすがに自動生成は無理なので Query 属性を使う。WHEREより後のSQLを書けばいいわけだが、今回特に条件がないので ORDER BY から書き始める。具体的には「Query("ORDER BY EMPNO DESC")」とする。
次に INSERT文、UPDATE文、DELETE文を実行するメソッドを追加する。これは規約に従って、メソッド名をそれぞれInsert、Update、Deleteで始めればいい。
最終的なソースはこんな感じ。

using System;
using System.Collections.Generic;
using System.Text;
using Seasar.Dao.Attrs;

namespace S2DaoTestApp {
    [Bean(typeof(Emp))]
    public interface IEmpDao {
        Emp[] GetAllList();
        [Query("ORDER BY EMPNO DESC")]
        Emp GetEmpHavingMaxEmpno();
        int Insert(Emp emp);
        int Update(Emp emp);
        int Delete(Emp emp);
    }
}

Form の修正

メインフォームをこんな感じに修正する。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Seasar.Framework.Container;
using Seasar.Framework.Container.Factory;

namespace S2DaoTestApp {
    public partial class Form1 : Form {
        private IS2Container _container;

        public Form1() {
            InitializeComponent();
        }

        private IEmpDao EmpDao {
            get {
                if (_container == null) {
                    _container = SingletonS2ContainerFactory.Container;
                }
                return (IEmpDao)_container.GetComponent(typeof(IEmpDao));
            }
        }

        private Emp Current {
            get {
                if (empBindingSource.Current == null) {
                    return null;
                } else {
                    return (Emp)empBindingSource.Current;
                }
            }
        }

        private void buttonGetAll_Click(object sender, EventArgs e) {
            try {
                this.Enabled = false;
                empBindingSource.DataSource = EmpDao.GetAllList();
            } finally {
                this.Enabled = true;
            }
        }

        private void buttonAppend_Click(object sender, EventArgs e) {
            try {
                this.Enabled = false;
                Emp emp = EmpDao.GetEmpHavingMaxEmpno();
                using (AppendForm f = new AppendForm(emp.EmpNo + 1)) {
                    DialogResult r = f.ShowDialog(this);
                    if (r == DialogResult.OK) {
                        empBindingSource.DataSource = EmpDao.GetAllList();
                    }
                }
            } finally {
                this.Enabled = true;
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e) {
            try {
                this.Enabled = false;
                if (this.Current == null) {
                    MessageBox.Show("編集できません",
                        "",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                    return;
                }

                Emp emp = this.Current;
                using (EditForm f = new EditForm(emp)) {
                    DialogResult r = f.ShowDialog(this);
                    if (r == DialogResult.OK) {
                        empBindingSource.DataSource = EmpDao.GetAllList();
                    }
                }
            } finally {
                this.Enabled = true;
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e) {
            try {
                this.Enabled = false;
                if (this.Current == null) {
                    MessageBox.Show("削除できません",
                        "",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                    return;
                }

                if (MessageBox.Show("削除しますか?",
                    "",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) == DialogResult.No) {
                    return;
                }

                Emp emp = this.Current;
                if (EmpDao.Delete(emp) == 1) {
                    empBindingSource.Remove(emp);
                }
            } finally {
                this.Enabled = true;
            }
        }
    }
}

コンテナは Program.cs で初期化するように変更した。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Seasar.Framework.Container.Factory;

namespace S2DaoTestApp {
    static class Program {
        [STAThread]
        static void Main() {
            SingletonS2ContainerFactory.Init();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

データを追加する AppendForm はこんな感じ。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Seasar.Framework.Container;
using Seasar.Framework.Container.Factory;

namespace S2DaoTestApp {
    public partial class AppendForm : Form {
        private IS2Container _container;

        public AppendForm(int newnumber) {
            InitializeComponent();
            textEmpno.Text = newnumber.ToString();
        }

        public IS2Container S2Container {
            get {
                if (_container == null) {
                    _container = SingletonS2ContainerFactory.Container;
                }
                return _container;
            }
        }


        private bool IsValid {
            get {
                if (textEname.Text.Trim().Length == 0) {
                    return false;
                }

                if (textDeptnum.Text.Trim().Length == 0) {
                    return true;
                }

                int tmp;
                if (!int.TryParse(textDeptnum.Text, out tmp)) {
                    return false;
                }

                return true;
            }
        }

        private void buttonAppend_Click(object sender, EventArgs e) {
            this.Enabled = false;
 
            Emp emp = new Emp();
            emp.EmpNo = int.Parse(textEmpno.Text);
            emp.Ename = textEname.Text;
            string dept = textDeptnum.Text.Trim();
            if ((dept != null) && (dept.Length > 0)) {
                emp.DeptNum = int.Parse(dept);
            }

            IEmpDao dao = (IEmpDao)this.S2Container.GetComponent(typeof(IEmpDao));
            int r = dao.Insert(emp);
            if (r == 1) {
                this.DialogResult = DialogResult.OK;
            }
        }

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

        private void textDeptnum_TextChanged(object sender, EventArgs e) {
            buttonAppend.Enabled = this.IsValid;
        }

        private void textEname_TextChanged(object sender, EventArgs e) {
            buttonAppend.Enabled = this.IsValid;
        }
    }
}

データを修正する EditForm はこんな感じ。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Seasar.Framework.Container;
using Seasar.Framework.Container.Factory;

namespace S2DaoTestApp {
    public partial class EditForm : Form {
        private IS2Container _container;
        private Emp _emp;

        public EditForm(Emp emp) {
            InitializeComponent();
            _emp = emp;
            textEmpno.Text = _emp.EmpNo.ToString();
            textEname.Text = _emp.Ename;
            if (!_emp.DeptNum.IsNull) {
                textDeptnum.Text = _emp.DeptNum.ToString();
            }
        }

        public IS2Container S2Container {
            get {
                if (_container == null) {
                    _container = SingletonS2ContainerFactory.Container;
                }
                return _container;
            }
        }


        private bool IsValid {
            get {
                if (textEname.Text.Trim().Length == 0) {
                    return false;
                }

                if (textDeptnum.Text.Trim().Length == 0) {
                    return true;
                }

                int tmp;
                if (!int.TryParse(textDeptnum.Text, out tmp)) {
                    return false;
                }

                return true;
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e) {
            this.Enabled = false;
 
            Emp emp = new Emp();
            emp.EmpNo = int.Parse(textEmpno.Text);
            emp.Ename = textEname.Text;
            string dept = textDeptnum.Text.Trim();
            if ((dept != null) && (dept.Length > 0)) {
                emp.DeptNum = int.Parse(dept);
            }

            IEmpDao dao = (IEmpDao)this.S2Container.GetComponent(typeof(IEmpDao));
            int r = dao.Update(emp);
            if (r == 1) {
                this.DialogResult = DialogResult.OK;
            }
        }

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

        private void textDeptnum_TextChanged(object sender, EventArgs e) {
            buttonEdit.Enabled = this.IsValid;
        }

        private void textEname_TextChanged(object sender, EventArgs e) {
            buttonEdit.Enabled = this.IsValid;
        }
    }
}

データの削除はメッセージボックスで済ませるようにした。


ここまで来てもSQLはほとんど書いてない。やっぱりやばいですな。