https://yoskhdia.hatenablog.com/entry/2016/10/18/152624

Clean ArchitectureにはUseCase層が定義されていますが、このUseCaseが一体どういうものなのか度々わからなくなるので、自分の考えをまとめてみるエントリです。

Clean Architectureについてはこちら

日本語訳:クリーンアーキテクチャ(The Clean Architecture翻訳)

以降、概念を”ユースケース”、実装されるモノを”UseCase”と表記することにします。 (同じっちゃ同じなんですが、指してるものがところどころ変わるので表記分けをしています。) また、Webアプリケーションを想定しています。

Clean Architectureから抜粋します。

Use Cases

The software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system. These use cases orchestrate the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.

We do not expect changes in this layer to affect the entities. We also do not expect this layer to be affected by changes to externalities such as the database, the UI, or any of the common frameworks. This layer is isolated from such concerns.

We do, however, expect that changes to the operation of the application will affect the use-cases and therefore the software in this layer. If the details of a use-case change, then some code in this layer will certainly be affected.

ユースケースはアプリケーション固有のビジネスルールをカプセル化したものです。 エンティティ(ドメインオブジェクト)の持つ(企業全体の)ビジネスルールを使って、それらの調整(データの受け渡し等)を行うことでユースケースの達成を目指します。ユースケースはあくまでエンティティを(一方的に)利用する側であるため、この層での変更(アプリケーションの変更)はエンティティに影響を与えないことが望ましいです。 また、より外層のDBやUIなどからの影響からも隔離されていることが望ましいです。

みたいなことがClean Architectureでは書かれています。

以前、ユースケース難民になってから読んだ書籍「ユースケース駆動開発実践ガイド」では、以下の様なガイドラインが述べられています。

  1. (画面のような)バウンダリクラスの名前を使いなさい。
  2. ドメインクラスの名前を使いなさい。
  3. 「名詞(主語) - 動詞 - 名詞(目的語)」(日本語でなら「名詞(主語) - 名詞(目的語) - 動詞」)という文の構造に従ってユースケースを書きなさい。
  4. オブジェクトモデルの言葉を使ってユースケースを書きなさい。
  5. ユースケースは実行時の振る舞いの仕様であるということを忘れないように。
  6. GUIプロトタイプや画面モックアップを使いなさい。
  7. イベントとその応答の流れとしてユースケースを書き、ユーザとシステムの対話の両側を記述しなさい。
  8. ユースケースは叙述的に書きなさい。
  9. アクターとユースケース図を使ってユースケースを組織化しなさい。