...

SEASAR.NET - Seasarイベントサイト

by user

on
Category: Documents
21

views

Report

Comments

Transcript

SEASAR.NET - Seasarイベントサイト
Seasar Conference
2008 Autumn
Seasar
Seasar.NET
Seasar.NETファミリーを使った開発
NETファミリーを使った開発
NETファミリ
ファミリ を使った開発
S2Container.NETコミッタ
S2C
t i
NETコミ タ
藤井宏明
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
1
自己紹介
• S2Container.NET、S2Dao.NETコミッタ
S2Container NET S2Dao NETコミッタ
• 某SIerでSEをしてます。
• 昼間は要件定義書、仕様書、議事録書き。
夜はプログラマの生活
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
2
SEASAR.NET
SEASAR
.NETの紹介
の紹介
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
3
Dependency Injectionとは
Injectionとは
• 一般的に、各部分の結合を最小限にする
ように設計します。
ように設計します
⇒ セパレート・インターフェイス・パターン
c la s s 論理 モ デ ル
<<interface>>
Hog e Imp l
IHog e
+
FindData(s tring) : int
< コードでの例
ドでの例 >
IHoge hoge = new HogeImpl();
hoge.FindData(“1”);
Seasar Conference
2008 Autumn
+
FindData(string) : int
if 文、フラグだらけ
ハードコーディングなので変更
依存関係の条件に
のコード完成
合わせてif文で対応
できない。
© The Seasar Foundation and the others 2008. all rights reserved.
4
Dependency Injectionとは
Injectionとは
• 依存関係を外部から設定することを
D
Dependency
d
IInjectionと呼ぶ。
j ti と呼ぶ
S2Container container = S2ContainerFactory.GetInstance(“app.dicon”);
IHoge hoge = (IHoge) container.GetComponent(typeof(IHoge));
container GetComponent(typeof(IHoge));
hoge.FindData(“1”);
• 依存関係を外部から設定するモノが、
が
p
y Injectionコンテナ(DIコンテナ)
j
Dependency
– S2Container.NET
– Spring.NET
Spring NET
– Unity
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
5
Dependency Injectionとは
Injectionとは
• 主な使われ方:
– フレームワークのベース
• フレームワークが実装を用意して、Injectionする。
⇒ユーティリティ的な使い方
– アプリケーション・アーキテクチャーのベース
• DIコンテナに登録した実装クラスをInjectionする。
j
⇒依存関係の処理に使う
– ユニットテスト用
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
6
Seasar.NETファミリー
Seasar.NET
ファミリー (一部)
• S2Container.NET
S2Container NET
– S2Container.NETとQuill
• S2Dao.NET
• S2Unit.NET
• S2Windows.NET
S2Windows NET
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
7
余談1:ReSharper
余談1:
ReSharperのススメ
のススメ
商用
• VisualStudioのリファクタリング用プラグイン
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
8
S2Container.NET
• Dependency Injection(DI)コンテナ
– 使い方はJava版に近い。
– 基本的に設定はdiconファイルという設定
ファイルで指定する。
•
•
•
•
•
登録するクラス、実装クラスの対応。
Aspect指向プログラム(AOP)の指定
指向プ グ ム( O ) 指定
データソースの指定
コンポーネントの自動登録設定
ポ
自動登録 定
色々な種類でオブジェクトを作成できる。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
9
Quill
• 簡易版DIコンテナ
– Java版にはない機能。diconファイルを使わない。
– コンテナへの登録・指定はコードで行う。
– 基本はSingletonのみ。
基本はSingletonのみ
– AOPの指定もコードで行う。
– データソースの指定はapp.configで行う
デ
指定
– プロバイダは基本は固定。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
10
S2Container.NETと
S2Container.NET
とQuill
Quillの違い
の違い
• インスタンス生成のタイミングの違い
– S2Container.NETは起動時にすべて初期化
– Quillは初めて呼ばれたときに初期化
• コードへの埋め込みと柔軟性
コ ドへの埋め込みと柔軟性
– S2Container.NETはdiconファイルの変更でよい
– Quillは再ビルドを必要
は再ビ ドを必要
– パッケージのような柔軟な仕様の場合には
S2Container.NET
– 請負開発のような場合にはQuill
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
11
S2Dao.NET
• O/Rマッパー
– 毎度決まった処理を楽にするために。
• DbConnection作成 → DbCommand生成
→ DataSet(or DbDataReader)生成
→ 画面や帳票にセット
⇒ 自動化します。
自動化します
• 2Way仕様
– SQL文の自動生成を行う。
SQL文の自動生成を行う
– SQL文を記述したテキストファイルを使用する。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
12
S2Dao.NET – PONO(Value Object)
[Table("T_EMP")]
public class EmployeeDto
{
private string _code;
private DepartmentDto _department;
[Column("s_code")]
[Col
mn("s code")]
public string Code
{
get { return _code; }
sett { _code
d = value;
l
}
}
}
[Relno(0), Relkeys("n_dept_id:n_id")]
public DepartmentDto Department
{
get { return _department; }
set { _department = value; }
}
Seasar Conference
2008 Autumn
取得先テーブルを指定する属性
取得先フィールドを指定する属性
結合を設定する属性
Value Object は「エンタープライズ
アプリケーションアーキテクチャーパターン」から
© The Seasar Foundation and the others 2008. all rights reserved.
13
Quillを使った場合のコードの例
Quill
を使った場合のコードの例
< interface >
[Implementation(typeof (EmployeeEditServiceImpl))]
public interface IEmployeeEditService
{
EmployeeEditPage GetData(int id);
}
実装クラスの指定を示す属性
< 実装クラス >
public class EmployeeEditServiceImpl : IEmployeeEditService
{
protected IEmployeeDao dao;
public、protectedなら自動的にinjection
}
public EmployeeEditPage GetData(int id)
{
…
}
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
14
Quillを使ったS2Dao.NET
Quillを使った
S2Dao.NET
– Dao
Daoインターフェイス
インターフェイス
• interfaceを作成するだけ
S2Daoを示す属性とAOPを示す属性(Quill用)
[S2Dao]
[Implementation]
[Bean(typeof(EmployeeDto))]
public interface IEmployeeDao
{
[Query("order by tt_emp.n_id")]
emp n id")]
IList<EmployeeDto> GetAll();
自動生成するPONOクラスを示す属性
自動生成するSQL文のWhere句を示す属性
[Sql("select n_id from t_emp where s_code = /*code*/'000001'")]
i tG
int
GetId(string
tId( t i code);
d )
実行するSQL文を示す属性
}
IList<EmployeeCsvDto> GetAll();
この場合、IEmployeeDao_GetAllというファイルのSQL文を実行
。
注意:このSQL文のファイルを埋め込みリソースにする必要あり。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
15
Linq To SQL
• .NET
NET Framework3
Framework3.5より導入
5より導入
// DataContext で接続文字列を取得します。
DataContext db = new DataContext(
DataContext("c:¥¥projects¥¥Sample¥¥sample
c:¥¥projects¥¥Sample¥¥sample.mdf
mdf");
);
// クエリを実行するために、型指定されたテーブルを取得します。
Table< EmployeeDto> employees = db.GetTable<EmployeeDto>();
// クエリを指定
var q =
from c in EmployeeDto
where
h
c.Code
C d == “001“
select c;
foreach (var cust in q)
Console.WriteLine(“Code = {0}, Name = {1}", cust.Code cust.Name);
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
16
Seasar.NETを使うメリット
Seasar.NET
を使うメリット
• 仕様書との親和性が高い
– Seasar.NET で設定することは、普通のソースの中
に記述することになることが多い。
に記述することになることが多い
– デザイナーやツールを必要としない。
– 複雑なXMLファイルをほとんど必要としない。
仕様書からソースの自動生成ができる。
※もちろん すべ 生成 きる訳 はな
※もちろん、すべて生成できる訳ではない。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
17
Linq To SQL他との違い
SQL他との違い
• Linq To SQL、他
SQL 他
– デザイナを使ったり、コード中に記述する。
• S2Daoの場合
– 属性ベースによるSQL文の自動作成
属性ベ
よる
文 自動作成
– 他のDB管理ツールで作成したSQL文をそのまま使
える。
業務用アプリを開発し、保守する上ではS2Daoの
メリットは大きい
メリットは大きい。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
18
S2Unit.NET
• Unitテスト用フレームワーク
– S2Unit.NETが提供するクラスを継承して使う。
• S2Container.NET用がS2TestCase。
• Quill用がQuillTestCase。
– MbUnitを使用する。
を使用する。
• NUnitではない。
– 使用するときにはReSharperと、MbUnitを連携
させるMbUnit ReSharperを使うと便利
させるMbUnit-ReSharperを使うと便利
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
19
S2Unit.NET((Quill
S2Unit.NET
Quillの場合)
の場合)
[TestFixture]
public class TestGender : QuillTestCase
{
protected IGenderDao dao;
[SetUp]
public void Setup()
{
// 初期セットアップ
}
}
[Test, Quill(Tx.Rollback)]
public void TestDao()
{
IList<GenderDto> list = dao.GetAll();
Assert.AreEqual(2, list.Count, "Count");
}
Seasar Conference
2008 Autumn
テストクラスを示す属性
初期設定用メソッドを設定する属性
テストメソッドを示す属性
© The Seasar Foundation and the others 2008. all rights reserved.
20
S2Windows.NET
• PONOを自動的にバインディングする
バインドするコントロールの接頭語を示す属性
[ControlModifier("txt", "")]
public partial class FrmDepartmentEdit : S2Form
{
……
}
このクラスを継承する
DepartmentEditPage data = service.GetData(_id.Value);
if (data != null)
{
this.DataSource = data;;
}
PONOをバインドするプロパティ
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
21
余談2:ReSharper
余談2:
ReSharperとテンプレート
とテンプレート
• そのReSharperの便利な機能にテンプレートが
あります。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
22
設計について
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
23
インターフェイスとトランザクション
• S2Container
S2Container.NET(Quill)では、対象となる
NET(Quill)では 対象となる
オブジェクトのメソッドに設定します。
– S2Container.NET:diconファイル
– Quill:[Transaction]属性
[
]
[Transaction]
public virtual int ExecUpdate(EmployeeEditPage data)
{
if (data == null)
throw new ArgumentNullException("data");
}
Seasar Conference
2008 Autumn
……
© The Seasar Foundation and the others 2008. all rights reserved.
24
サービスinterface
サービス
interfaceと実装クラスについて
と実装クラスについて
• レイヤー化
(「エンタープライズアプリケーションアーキテクチャーパターン」より)
aspx、Form
Presentaion層
など
Service
クラス
Domain層
S2Dao.NET,
Data
Source層
Access
クラス
DB
Seasar Conference
2008 Autumn
・・・ 画面、帳票など、ユーザの入力部分
・・・ トランザクションはここで設定
・・・ S2DaoのメソッドはSQL文単位
Domain層以下をカプセル化し、
ト
トランザクションを制御することを
ザクシ を制御する とを
サービスレイヤーという
© The Seasar Foundation and the others 2008. all rights reserved.
25
実際のレイヤー化した設計
Presentation層
c la s s 論 理 モ デ ル
F rmDe p a rt me nt
+
service: IDepartmentService
Service層
<<interface>>
De p a rt me n t S e rv ice Imp
IDepa rt ment Serv ice
+
+
ExecUpdate(DepartmentDto) : int
GetAll() : IList
+
dao: IDepartmentDao
+
+
ExecUpdate(DepartmentDto) : int
GetAll() : IList
Data Source層
<<interface>>
IDepa rt ment Da o
+
+
+
+
Seasar Conference
2008 Autumn
Delete(DepartmentDto) : void
GetAll() : IList
Insert(DepartmentDto) : int
Update(DepartmentDto) : int
© The Seasar Foundation and the others 2008. all rights reserved.
26
レイヤー化の一例
層へ渡すデータ(Value Object)
<Presentation層 → Domain層>
を取得する
private void btnUpdate_Click(object sender, EventArgs e)
{
DepartmentEditPage data = (DepartmentEditPage) DataSource;
data.Id = _id;
if (service.ExecUpdate(data) > 0)
Domain層へアクセス
……
}
<Domain層→DataSource層>
トランザクションはメソッド全体にかける
[Transaction]
public virtual int ExecUpdate(DepartmentEditPage dto)
{
if (dto.Id.HasValue)
{
DepartmentDto departmentDto = dao.GetData(dto.Id.Value);
if ((departmentDto
p
!= null))
return (dao.UpdateData(data));
else
return (dao.InsertData(data));
}
}
}
ビジネスロジック
DataSource層へアクセス
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
27
WindowsFormについて
WindowsForm
について
• Formのデザイナ画面でQuillは使えます。
Formのデザイナ画面でQuillは使えます
• S
Serviceクラスをpublicかprotectedな
i クラスを bli か t t dな
フィールドにすると自動バインドします。
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
28
余談3:Oracle
余談3:
Oracleでの注意
での注意
• 件数が多い表のメタ情報を取得するとき
時間がかかる。
– 全件取得してからメタ情報を取得してる?
– Oracle Clientの仕様?
– 対応策
• Table属性で指定するテーブルを注意する。
• テーブル定義情報のキャッシュを作成する。
テ ブル定義情報のキャッシ を作成する
(詳細は、ドキュメント参照)
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
29
参考サイト
• S2Container.NETサイト
S2Container NETサイト
http://s2container.net.seasar.org/ja/
• S2DAO.NETサイト
S2DAO NETサイト
http://s2dao.net.seasar.org/ja/
• Seasar.NETサンプル
S
NETサンプル
http://s2container.net.seasar.org/ja/download.html
p
gj
Seasar Conference
2008 Autumn
© The Seasar Foundation and the others 2008. all rights reserved.
30
Fly UP