seasar netseasar.netファミリ を使った開発ファ …...•...

30
Seasar Conference Seasar Conference 2008 2008 Autumn Autumn 2008 2008 Autumn Autumn Seasar NET Seasar NETファミリ を使った開発 ファミリ を使った開発 Seasar.NET Seasar.NETファミリを使った開発 ファミリを使った開発 S2C ti NETコミ S2Container.NETコミ藤井宏明 1 Seasar Conference Seasar Conference 2008 2008 Autumn Autumn © The Seasar Foundation and the others 2008. all rights reserved.

Upload: others

Post on 27-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Seasar ConferenceSeasar Conference2008 2008 AutumnAutumn2008 2008 AutumnAutumn

Seasar NETSeasar NETファミリ を使った開発ファミリ を使った開発Seasar.NETSeasar.NETファミリーを使った開発ファミリーを使った開発

S2C t i NETコミ タS2Container.NETコミッタ

藤井宏明

1Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 2: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

自己紹介自己紹介

• S2Container NET S2Dao NETコミッタ• S2Container.NET、S2Dao.NETコミッタ

• 某SIerでSEをしてます。

• 昼間は要件定義書、仕様書、議事録書き。夜はプログラマの生活

2Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 3: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

SEASARSEASAR.NET.NETの紹介の紹介

3Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 4: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Dependency InjectionDependency Injectionとはとは

• 一般的に、各部分の結合を最小限にするように設計しますように設計します。⇒ セパレート・インターフェイス・パターン

class 論理モデル

<<interface>> HogeImplIHoge

+ FindData(s tring) : int

HogeImpl

+ FindData(string) : int

< コードでの例 >ドでの例IHoge hoge = new HogeImpl();hoge.FindData(“1”); ハードコーディングなので変更

できない。依存関係の条件に合わせてif文で対応

if 文、フラグだらけのコード完成

4Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 5: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Dependency InjectionDependency Injectionとはとは

• 依存関係を外部から設定することをD d I j ti と呼ぶDependency Injectionと呼ぶ。

S2Container container = S2ContainerFactory.GetInstance(“app.dicon”);IHoge hoge = (IHoge) container GetComponent(typeof(IHoge));

IHoge hoge = (IHoge) container.GetComponent(typeof(IHoge));hoge.FindData(“1”);

• 依存関係を外部から設定するモノが、Dependency Injectionコンテナ(DIコンテナ)p y j– S2Container.NET

Spring NET– Spring.NET– Unity

5Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 6: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Dependency InjectionDependency Injectionとはとは

• 主な使われ方:

– フレームワークのベース

• フレームワークが実装を用意して、Injectionする。⇒ユーティリティ的な使い方

– アプリケーション・アーキテクチャーのベース

• DIコンテナに登録した実装クラスをInjectionする。j⇒依存関係の処理に使う

– ユニットテスト用

6Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 7: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Seasar.NETSeasar.NETファミリーファミリー (一部)(一部)

• S2Container NET• S2Container.NET– S2Container.NETとQuill

• S2Dao.NET• S2Unit.NET• S2Windows NETS2Windows.NET

7Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 8: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

余談1:余談1:ReSharperReSharperのススメのススメ

• VisualStudioのリファクタリング用プラグイン

商用

• VisualStudioのリファクタリング用プラグイン

8Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 9: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Container.NETS2Container.NET

• Dependency Injection(DI)コンテナ• Dependency Injection(DI)コンテナ

– 使い方はJava版に近い。

– 基本的に設定はdiconファイルという設定ファイルで指定する。ファイルで指定する。

• 登録するクラス、実装クラスの対応。

指向プ グ ム( O ) 指定• Aspect指向プログラム(AOP)の指定

• データソースの指定

ポ 自動登録 定• コンポーネントの自動登録設定

• 色々な種類でオブジェクトを作成できる。

9Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 10: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

QuillQuill

• 簡易版DIコンテナ• 簡易版DIコンテナ

– Java版にはない機能。diconファイルを使わない。

– コンテナへの登録・指定はコードで行う。

基本はSingletonのみ– 基本はSingletonのみ。

– AOPの指定もコードで行う。

デ 指定– データソースの指定はapp.configで行う

– プロバイダは基本は固定。

10Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 11: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Container.NETS2Container.NETととQuillQuillの違いの違い

• インスタンス生成のタイミングの違い

– S2Container.NETは起動時にすべて初期化

– Quillは初めて呼ばれたときに初期化

• コードへの埋め込みと柔軟性コ ドへの埋め込みと柔軟性

– S2Container.NETはdiconファイルの変更でよい

は再ビ ドを必要– Quillは再ビルドを必要

– パッケージのような柔軟な仕様の場合にはS2Container.NET

– 請負開発のような場合にはQuill

11Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

請負開発のような場合にはQuill

Page 12: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Dao.NETS2Dao.NET

• O/Rマッパー

– 毎度決まった処理を楽にするために。

• DbConnection作成 → DbCommand生成→ DataSet(or DbDataReader)生成→ 画面や帳票にセット⇒ 自動化します⇒ 自動化します。

• 2Way仕様

– SQL文の自動生成を行う– SQL文の自動生成を行う。

– SQL文を記述したテキストファイルを使用する。

12Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 13: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Dao.NET S2Dao.NET –– PONO(Value Object)PONO(Value Object)

[Table("T_EMP")]public class EmployeeDto{

取得先テーブルを指定する属性

{private string _code;private DepartmentDto _department;

[Col mn("s code")][Column("s_code")]public string Code{

get { return _code; }t { d l }

取得先フィールドを指定する属性

set { _code = value; }}

[Relno(0), Relkeys("n_dept_id:n_id")]public DepartmentDto Department{

get { return _department; }set { _department = value; }

結合を設定する属性

}} Value Object は「エンタープライズ

アプリケーションアーキテクチャーパターン」から

13Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 14: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

QuillQuillを使った場合のコードの例を使った場合のコードの例

< interface >[Implementation(typeof (EmployeeEditServiceImpl))]public interface IEmployeeEditServicepublic interface IEmployeeEditService{

EmployeeEditPage GetData(int id);}

実装クラスの指定を示す属性

< 実装クラス > 実装クラスpublic class EmployeeEditServiceImpl : IEmployeeEditService{

protected IEmployeeDao dao; public、protectedなら自動的にinjection

public EmployeeEditPage GetData(int id){

…}}

}

14Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 15: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

QuillQuillを使ったを使ったS2Dao.NET S2Dao.NET –– DaoDaoインターフェイスインターフェイス

• interfaceを作成するだけ

[S2Dao][Implementation]

S2Daoを示す属性とAOPを示す属性(Quill用)

自動生成するPONOクラスを示す属性[Implementation][Bean(typeof(EmployeeDto))]public interface IEmployeeDao{

[Query("order by t emp n id")]

自動生成するPONOクラスを示す属性

自動生成するSQL文のWhere句を示す属性

[Query("order by t_emp.n_id")]IList<EmployeeDto> GetAll();

[Sql("select n_id from t_emp where s_code = /*code*/'000001'")]i t G tId( t i d )int GetId(string code);

IList<EmployeeCsvDto> GetAll();}

実行するSQL文を示す属性

この場合、IEmployeeDao_GetAllというファイルのSQL文を実行。

注意:このSQL文のファイルを埋め込みリソースにする必要あり。

15Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 16: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Linq To SQLLinq To SQL

• NET Framework3 5より導入• .NET Framework3.5より導入

// DataContext で接続文字列を取得します。DataContext db = new DataContext("c:¥¥projects¥¥Sample¥¥sample mdf");DataContext db = new DataContext( c:¥¥projects¥¥Sample¥¥sample.mdf );

// クエリを実行するために、型指定されたテーブルを取得します。Table< EmployeeDto> employees = db.GetTable<EmployeeDto>();

// クエリを指定var q =

from c in EmployeeDto h C d “001“where c.Code == “001“

select c; foreach (var cust in q)

Console.WriteLine(“Code = {0}, Name = {1}", cust.Code cust.Name);

16Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 17: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Seasar.NETSeasar.NETを使うメリットを使うメリット

• 仕様書との親和性が高い• 仕様書との親和性が高い

– Seasar.NET で設定することは、普通のソースの中に記述することになることが多いに記述することになることが多い。

– デザイナーやツールを必要としない。

– 複雑なXMLファイルをほとんど必要としない。

仕様書からソースの自動生成ができる。

※もちろん すべ 生成 きる訳 はな※もちろん、すべて生成できる訳ではない。

17Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 18: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

Linq To SQLLinq To SQL他との違い他との違い

• Linq To SQL 他• Linq To SQL、他

– デザイナを使ったり、コード中に記述する。

• S2Daoの場合

属性ベ よる 文 自動作成– 属性ベースによるSQL文の自動作成

– 他のDB管理ツールで作成したSQL文をそのまま使える。

業務用アプリを開発し、保守する上ではS2Daoのメリットは大きい

18Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

メリットは大きい。

Page 19: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Unit.NETS2Unit.NET

• Unitテスト用フレームワーク

– S2Unit.NETが提供するクラスを継承して使う。

• S2Container.NET用がS2TestCase。

• Quill用がQuillTestCase。

– MbUnitを使用する。を使用する。

• NUnitではない。

– 使用するときにはReSharperと、MbUnitを連携させるMbUnit ReSharperを使うと便利させるMbUnit-ReSharperを使うと便利

19Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 20: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Unit.NETS2Unit.NET((QuillQuillの場合)の場合)

[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");

}}}

20Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 21: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

S2Windows.NETS2Windows.NET

• PONOを自動的にバインディングする• PONOを自動的にバインディングする

[ControlModifier("txt", "")]バインドするコントロールの接頭語を示す属性

public partial class FrmDepartmentEdit : S2Form{

… …このクラスを継承する

DepartmentEditPage data = service.GetData(_id.Value);if (data != null){

this.DataSource = data;;}

}PONOをバインドするプロパティ

21Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 22: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

余談2:余談2:ReSharperReSharperとテンプレートとテンプレート

• そのReSharperの便利な機能にテンプレートが• そのReSharperの便利な機能にテンプレートがあります。

22Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 23: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

設計について設計について

23Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 24: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

インターフェイスとトランザクションインターフェイスとトランザクション

• S2Container NET(Quill)では 対象となる• S2Container.NET(Quill)では、対象となるオブジェクトのメソッドに設定します。

– S2Container.NET:diconファイル

– Quill:[Transaction]属性[ ]

[Transaction]public virtual int ExecUpdate(EmployeeEditPage data)public virtual int ExecUpdate(EmployeeEditPage data){

if (data == null)throw new ArgumentNullException("data");

… …}

24Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 25: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

サービスサービスinterfaceinterfaceと実装クラスについてと実装クラスについて

• レイヤー化• レイヤー化(「エンタープライズアプリケーションアーキテクチャーパターン」より)

・・・ 画面、帳票など、ユーザの入力部分

・・・ トランザクションはここで設定

Presentaion層

Domain層

aspx、Formなど

Serviceクラス トランザクションはここで設定

・・・ S2DaoのメソッドはSQL文単位

Domain層

Data Source層S2Dao.NET,Accessクラス

Serviceクラス

Domain層以下をカプセル化し、ト ザクシ を制御する とを

DBトランザクションを制御することを

サービスレイヤーという

25Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 26: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

実際のレイヤー化した設計実際のレイヤー化した設計

class 論理モデル

FrmDepartment

Presentation層

FrmDepartment

+ service: IDepartmentService

<<interface>> DepartmentServ iceImp

Service層

IDepartmentServ ice

+ ExecUpdate(DepartmentDto) : int+ GetAll() : IList

DepartmentServ iceImp

+ dao: IDepartmentDao

+ ExecUpdate(DepartmentDto) : int+ GetAll() : IList

<<interface>>

Data Source層

IDepartmentDao

+ Delete(DepartmentDto) : void+ GetAll() : IList+ Insert(DepartmentDto) : int+ Update(DepartmentDto) : int

26Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 27: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

レイヤー化の一例レイヤー化の一例

<Presentation層 → Domain層>private void btnUpdate_Click(object sender, EventArgs e){

DepartmentEditPage data = (DepartmentEditPage) DataSource;

層へ渡すデータ(Value Object)を取得する

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 != null)( p )

return (dao.UpdateData(data));else

return (dao.InsertData(data));}

}

ビジネスロジック

}}

DataSource層へアクセス

27Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 28: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

WindowsFormWindowsFormについてについて

• Formのデザイナ画面でQuillは使えます• Formのデザイナ画面でQuillは使えます。

S i クラスを bli か t t dな• Serviceクラスをpublicかprotectedなフィールドにすると自動バインドします。

28Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 29: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

余談3:余談3:OracleOracleでの注意での注意

• 件数が多い表のメタ情報を取得するとき• 件数が多い表のメタ情報を取得するとき時間がかかる。

– 全件取得してからメタ情報を取得してる?

– Oracle Clientの仕様?

対応策– 対応策

• Table属性で指定するテーブルを注意する。

テ ブル定義情報のキャッシ を作成する• テーブル定義情報のキャッシュを作成する。(詳細は、ドキュメント参照)

29Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

Page 30: Seasar NETSeasar.NETファミリ を使った開発ファ …...• Unitテスト用フレームワーク – S2Unit.NETが提供するクラスを継承して使う。• S2Container.NET用がS2TestCase。•

参考サイト参考サイト

• S2Container NETサイト• S2Container.NETサイト

http://s2container.net.seasar.org/ja/

• S2DAO NETサイト• S2DAO.NETサイト

http://s2dao.net.seasar.org/ja/

S NETサンプル• Seasar.NETサンプル

http://s2container.net.seasar.org/ja/download.html

30Seasar ConferenceSeasar Conference

2008 2008 AutumnAutumn © The Seasar Foundation and the others 2008. all rights reserved.

p g j