RailsのクラスとDDD・クリーンアーキテクチャ概念のマッピング

## はじめに

以前書いた以下記事の内容を表にあらわしてみました。

https://qiita.com/yokoto/items/cd45e7b1d8b11bb9c29a

## Railsとアーキテクチャ

| Rails | DDD | Clean Architecture
|:-----------|------------|------------:|
| [ActionView](https://api.rubyonrails.org/classes/ActionView.html) | UI層 | Frameworks & Drivers(UI) |
| [ActionController](https://api.rubyonrails.org/classes/ActionController.html) | アプリケーション層 | Interface Adapters(Controllers / Presenters) / Application Business Rules(Use Cases / Interactor) |
| [ActiveModel::Model](https://api.rubyonrails.org/classes/ActiveModel/Model.html) | ドメイン層 | Enterprise Business Rules(Entities) |
| [ActiveRecord::Persistence](https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html) | インフラストラクチャ層 | Application Business Rules(Repository) / Interface Adapters(Repository) |


## Railsとドメインモデル

| Rails | DDD | Clean Architecture
|:-----------|------------|------------:|
| [ActiveRecord::ActiveModel](https://api.rubyonrails.org/classes/ActiveModel.html) | Entities | Entities, Repository |
| [ActiveRecord::Aggregations](https://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html) | Value Objects | Entities |
|[Module::Concerning](https://api.rubyonrails.org/classes/Module/Concerning.html) | Modules | Entities |
| サービスオブジェクト / [ActievRecord::Callbacks](https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html) / [ActiveRecord::Validations](https://api.rubyonrails.org/classes/ActiveRecord/Validations.html) | Domain Service  | Entities |
| [ActionController](https://api.rubyonrails.org/classes/ActionController.html) / サービスオブジェクト / フォームオブジェクト / [ActiveRecord::Observer](https://github.com/rails/rails-observers)(-Rails 4.0) / [ActievRecord::Callbacks](https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html) / [ActiveRecord::Validations](https://api.rubyonrails.org/classes/ActiveRecord/Validations.html) | Application Service | Use Cases / Interactor |

## Railsとドメインオブジェクトのライフサイクル

| Rails | DDD | Clean Architecture |
|:-----------|------------|------------:|
| フォームオブジェクト | Aggregates | - |
| - | Factories | - |
| [ActiveRecord::QueryMethods](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html) | Repositories | Application Business Rules(Repository) / Interface Adapters(Repository) |


## 参考

- [パーフェクトRails Part5 エキスパートRails](https://gihyo.jp/book/2020/978-4-297-11462-6)
- [texta.fm](https://anchor.fm/textafm)
- [Ruby on Railsの正体と向き合い方 / What is Ruby on Rails and how to deal with it?](https://speakerdeck.com/yasaichi/what-is-ruby-on-rails-and-how-to-deal-with-it)
- [Realworld Domain Model on Rails](https://speakerdeck.com/joker1007/realworld-domain-model-on-rails)
- [俺が悪かった。素直に間違いを認めるから、もうサービスクラスとか作るのは止めてくれ](https://qiita.com/joker1007/items/25de535cd8bb2857a685)
- [Rails サービスクラス再考 / have a rethink on Rails service class](https://speakerdeck.com/suusan2go/have-a-rethink-on-rails-service-class)
- [Ruby on Rails ガイド](https://railsguides.jp/)
- [Ruby on Rails API](https://api.rubyonrails.org/)
- [DDD難民に捧げる Domain-Driven Designのエッセンス 第2回　DDDの基礎と実践](https://www.ogis-ri.co.jp/otc/hiroba/technical/DDDEssence/chap2.html)
- [「IDDD本から理解するドメイン駆動設計」連載一覧](https://codezine.jp/article/corner/655)
- [クリーンアーキテクチャ完全に理解した](https://gist.github.com/mpppk/609d592f25cab9312654b39f1b357c60)

## 最後に

独断と偏見に基づくため、明らかな誤りや異なるご意見についてお気軽にコメントいただけますと幸いです。