Upload
shinya-okano
View
1.566
Download
6
Embed Size (px)
Citation preview
Spring4D の紹介第 28 回 エンバカデロ・デベロッパーキャンプ
2014/04/22
株式会社ビープラウド 岡野真也
今日紹介するのは
Spring4D
→Spring Framework for Delphi
の主にDIコンテナ
“Spring Framework”
→Java のアプリケーション
フレームワーク
Spring4D の機能
Spring4D の機能
• 便利なクラスライブラリ• 一般的な型• インターフェースベースのコレクション
型• リフレクション拡張• など…
•DI コンテナ
開発元やライセンスなど
• オープンソースプロジェクト• https://bitbucket.org/sglienke/spring4d
• ライセンス• Apache License 2.0
• 対応 Delphi バージョン• 2010 ~ XE6
“DI”
→Dependency Injection( 依存性の注入 )
Dependency Injection ( 依存性の注入 )
• ソフトウェアデザインパターン• コンポーネント ( ユニットやクラス ) 間
の依存関係をソースコードから取り除く• 外部の設定などで依存関係を注入する
ユニットやクラス間の依存関係
例 . 計算機アプリ
TCalculatoruCalculator.p
as
TMathService
uMathService.pas
uses uMathService;service := TMathService.Create;
• TMathService は計算処理を行う• uMathService.pas
• TCalculator は値の入出力と橋渡し• uCalculator.pas
• TCalculator は TMathService を利用• →uCalculator.pas はuMathService.pas に依存
欠点
依存関係がある場合の欠点
• 再利用しにくい• 単体テストしにくい TCalculator
uCalculator.pas
TMathService
uMathService.pas
uses uMathService;service := TMathService.Create;
uCalculator だけをテストしたいがuMathService も必要となる
TMathService を別の処理に差し替えたいが、 uCalculator.pas を編集しないといけない
DI コンテナを利用して依存関係を除去する
Spring.Container.TContainer
Spring.Container.TContainer
• Spring4D の DI コンテナ• クラス登録
• TContainer.RegisterComponent<T>.Implements<I>
• クラス / インスタンス取得• TContainer.Resolve<I>
DI コンテナを利用して依存関係を除去する
// コンテナから計算処理クラスのインスタンスを取得service := Container.Resolve<IMathService>;
• インターフェースを用意
• DI コンテナを用意
uServiceLocator.pas
TContaineruServiceLocator.pas
uServiceLocator.pas
IMathService
uMathServiceIntf.pas
uServiceLocator.pas
TMathService
uMathService.pas
実装クラス登録 インターフェース利用
• インターフェースを実装• 実装クラスをコンテナに
登録 ( 依存性の注入 )
uServiceLocator.pas
TCalculatoruCalculator.pas
インスタンス取得
インターフェース利用
• コンテナから実装クラス ( のインスタンス )を取得
依存性を除去できました
依存度が低いと
実装の差し替えも簡単です
実装の差し替え例
• TMathService を RPC で処理するTRemoteMathService に差し替える
uServiceLocator.pas
TContaineruServiceLocator.pas
uServiceLocator.pas
IMathService
uMathServiceIntf.pas
uServiceLocator.pas
TCalculatoruCalculator.pas
インスタンス取得
インターフェース利用
uServiceLocator.pas
TMathService
uMathService.pas
実装クラス登録 インターフェース利用
uServiceLocator.pas
TRemoteMathService
uRemoteMathService.pas
実装クラス登録 インターフェース利用
ユニットを差し替えるだけ!
便利なので試してみてね!
サンプルコード
http://bit.ly/devcamp28-spring4d
時間があればデモ
まとめ
• Spring4D には DI コンテナが含まれている• DI コンテナを使うとクラス間の依存性を除去
できる• 依存性を除去することでクラスの再利用や実装
の差し替え、テストが容易になる
参考
• Getting Giddy with Dependency Injection and Delphi Spring #5 – Delphi Spring Basics
• https://bitbucket.org/sglienke/spring4d
詳しくは DelphiTalks で。
第 7 回 Delphi Talks @ 東京
• 日時 : 2014/04/26( 土 ) 13:30 ~ 17:30
• 場所 : 株式会社ビープラウド 会議室 ( 新宿 )
• 参加申込み :• http://connpass.com/event/6021/• facebook 上のイベント• ML やフォーラムの案内への返信でも ok
ご清聴ありがとうございました