Skip to content

プロジェクトのファイル構成を修正 #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 6, 2023

Conversation

mochi-yu
Copy link
Collaborator

@mochi-yu mochi-yu commented Oct 4, 2023

概要

  • プロジェクトのファイル構成を修正する
  • これにより、特にロジック・データソース・表示の処理を分ける

実装

  • 以下のような一方向のみの参照となった
    • /cmd => /service => /gateway => entity
  • gatewayにてinterfaceを実装したことで、容易にプログラム上でAISourceを変更可能になった

@mochi-yu mochi-yu self-assigned this Oct 4, 2023
@cocoide
Copy link
Owner

cocoide commented Oct 5, 2023

[ディレクトリの命名や構成に関して]
・domain serviceと混同するから serive→usecaseの方がいいかな
・AISourceは直感的な命名ではない (Sourceはどの文脈のSourceだろ)
→ CommitMessageServiceとかでいいんじゃないのかな
・Interfaceをファイル毎に分けるのなら、packgeで分けた方がいい
(元々、開発効率を優先して同じファイルに書いてた)
このPRが参考になるかなと思うんだけど、gateway層は技術的な関心毎をまとめる(DIをする)層だと元々考えていた。
→ domainディレクトリにentityとservice層をおくとより依存関係の方向がわかりやすくなっていいのかもしれない。
gateway→serivceにDIして
依存関係は
usecase→service→entityになるイメージ

case int(entity.WrapServer):
gi = gateway.NewGrpcServeGateway()
default:
gi = gateway.NewGrpcServeGateway()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コンストラクタ関数 (New〇〇~)はcmdとかのエントリポイントで呼んで欲しい
色々なところで呼び出したらレイアードアーキテクチャの意味がなくなりそう

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分の中のレイヤードアーキテクチャーの目的は、プログラムの責務の分離だけどそれで同じですかね?

この変更の意図は、APIの接続先を選択するロジックを、コマンドの表示周りの処理と分離すること。
cmdディレクトリはCUIの表示とか、操作とかに関する処理だけにとどめたい。どのAPIにアクセスするかの選択は表示とかとは無関係で、cmdディレクトリ内のプログラムでは面倒を見ないようにしたい。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コンストラクタ関数は一箇所にまとめて保守性をあげたかったのが目的。
構造体の中に、該当のサービスを入れたら済む話なのかなと思った。

レイヤードアーキテクチャの目的は文脈
次第だと思うけど、リファクタしても影響範囲を出にくくするのと可読性を上げる事だと思う。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

保守性を上げることは、処理をまとめて同じところに書くよりも、各プログラムの責務を明確に分けることで生まれると僕は思うけど。
CLI上での表示をいじりたければ全てcmd\suggest.goを見る。万が一他の部分を変えたとしても、表示以外に影響を及ぼさない。中の処理を変えたければinternal\service\suggest_cmd.goを見る。というのが保守性を高めることじゃないですかね?

「リファクタしても影響範囲を出にくくするのと可読性を上げる事」は、全てのアーキテクチャが考えられている目的で、中でもレイヤードアーキテクチャの目的は、レイヤごとに責務を分けて、他のレイヤの役割を混ぜないことじゃないですかね。

Copy link
Owner

@cocoide cocoide Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コンストラクタ関数をエントリポイントでまとめた方が依存関係が全部わかって保守性が上がるって意味でいいたかった。
今回は、cmdが実質インターフェースだから特殊だけど。
そこはNewContainerとかでDIだけ別パッケージに切り出すとかで対処できそうかも。

コンストラクタ関数をまとめるのは、まとめるのが目的でなくコード規約てきなものだとイメージしてた。

レイヤ責務を分ける目的はどんな事だと考えてる?

}

func (ps fileDiffService) CreateFileDiffStr() (string, error) {
Copy link
Owner

@cocoide cocoide Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この関数は他のUsecase (分割コミットとか)で使って共通化するから
上層のDomain Seriviceに移行したほうがいいかな
(共通化するタイミングで大丈夫なのかなとも思いつつ)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、変数名とかを変えただけだよ。
基本domain serviceはusecaseより下層かと思うけど、上層に移行というのは何を想定してた?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たしかに、下層だな。すまん
クリーンアーキテクチャの図を想像していい間違えた。

return err
}

switch configIndex {
Copy link
Owner

@cocoide cocoide Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

マジックナンバーはハードコーディングしないで
iotaとかでenum化した方が見やすいかも

@mochi-yu
Copy link
Collaborator Author

mochi-yu commented Oct 5, 2023

@cocoide

・domain serviceと混同するから serive→usecaseの方がいいかな

これは変更します。

・AISourceは直感的な命名ではない (Sourceはどの文脈のSourceだろ)

変更しても良いけど、ここはservice(明示的に書くならapplication service)ではないので、するならしたのと併せてCommitMessageRepositoryとかにしたい。

・Interfaceをファイル毎に分けるのなら、packgeで分けた方がいい

ごめんこれが何を言いたいのかよく理解できていないわ。
自分の中でのgatewayディレクトリは、repository層のイメージ。変更が分かりにくくなるかと思って名前変えなかったけど、変えて良いなら丸々repositoryに変更したい。この中には外部リソースにアクセスする処理を書きたい。それ以外のファイル変更情報の変更とかは上層のservice(usecase)層の中で処理したい。

あと、domainService作るならentity層の中かと思うよ。

@cocoide
Copy link
Owner

cocoide commented Oct 5, 2023

@cocoide

・domain serviceと混同するから serive→usecaseの方がいいかな

これは変更します。

・AISourceは直感的な命名ではない (Sourceはどの文脈のSourceだろ)

変更しても良いけど、ここはservice(明示的に書くならapplication service)ではないので、するならしたのと併せてCommitMessageRepositoryとかにしたい。

・Interfaceをファイル毎に分けるのなら、packgeで分けた方がいい

ごめんこれが何を言いたいのかよく理解できていないわ。

自分の中でのgatewayディレクトリは、repository層のイメージ。変更が分かりにくくなるかと思って名前変えなかったけど、変えて良いなら丸々repositoryに変更したい。この中には外部リソースにアクセスする処理を書きたい。それ以外のファイル変更情報の変更とかは上層のservice(usecase)層の中で処理したい。

あと、domainService作るならentity層の中かと思うよ。

クリーンアーキの文脈でたしかにgatewayはrepositoryと言われることはあるからgatewayでない方がいいかもね。 
(infraとかでも、今のインターン先ではgatewayがインフラ層として扱われてたのにバイアイスかかってた)

entityの中にserviceを入れるのはどこがソースだろ?そうした方がいい理由は何だと思う?

domainにまとめたかったのは、そこに具体的な実装がないつまり、そこを上位のレイヤーだと示したいだけで、無駄に層を深くするのはやる必要はないと思う。

仮にgatewayがrepositoryなら、そこに具体的な実装を置くのはどうだと思う?

repositoryはdbアクセスを抽象化する文脈で使われるからクライアント側で使うのは微妙かも。
reactでレポジトリパターンは使わんくない?

@mochi-yu mochi-yu assigned cocoide and unassigned cocoide Oct 11, 2023
@cocoide cocoide merged commit 2e5262f into main Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants