CQRS (Command Query Responsibility Segregation)
1. 目的
- ドメインモデルからドメインにとって本質的ではないクエリデータを隔離したい
2. 課題
- データを読み込む処理(クエリ)とデータを更新する処理(コマンド)は処理の性質が異なっている為、同じデータモデルを使うことは非効率
- クエリとコマンドで同じデータモデルを利用するのは手間が大きい
- 画面表示用にデータ変換ラッパーが必要になったりする
- UI側の処理がドメインモデルに依存しているとドメイン層の変更がUI層に影響を与える(それ自体は悪いことではない)
3. 解決策
クエリとコマンドを分離し、別のインターフェイスを与える
- クエリにはクエリ専用のデータモデルを使用し、コマンドにはコマンド専用のデータモデルを使用する
- クエリ用のデータモデルは非正規化され、UIの要求に対応したものになる
- コマンド用のデータモデルは整合性や一貫性の確保に重点をおいたいわゆるドメインモデルというものになる
4. メリット
- クエリ側の実装コストの低下
- ドメインモデルからUIに関する情報を隔離でき、ドメインモデルの
5. デメリット
6. 注意事項
- そもそもシンプルなシステムであればオーバーエンジニアリング
- ご利用は計画的に
参考資料