「ドメイン駆動設計入門」という本を読みました

この本は、会社のM君が読んでいて、この本の話をよくされるので、「私も読んでおくか~」となって読みました。

この本を読んで、ドメイン駆動設計の復習になりましたし、新しい知識もあって大変勉強になりました!⊂(^-^)⊃

忘れてたことも多いしね(笑)

 

この本は、ドメイン駆動についてコードをベースにわかりやすく紹介してくれているのが特徴かなと思います。

前にドメイン駆動と言えばこれ みたいなエリック・エヴァンスという方の本があって、そのレビューを書きましたが

「エリック・エヴァンスのドメイン駆動設計」を読みました

上記の本は、いい本なんだけど、とにかく読みにくいし、長い!

エリック・エヴァンスの本は、考え方、みたいなのについて述べられている部分が多いので

「とにかくコードで見たい」

という方には退屈になるだろうなと思うんですよね。

実際、私も読んでいる間、何度も昼寝しました(笑)。

 

なので、手っ取り早くドメイン駆動を学びたい、コードで紹介してほしいという方にはこっちの「ドメイン駆動設計入門」がおすすめかもしれません。

ただ、「ドメイン駆動設計入門」にも書いてあることですが、私としてはエリック・エヴァンスの「エリック・エヴァンスのドメイン駆動設計」とセットで読んでほしいなと思います。


下記、メモです。

<値オブジェクトとエンティティ>

値オブジェクトは、電話番号とか、名前とか、ユーザーidとか。
ユーザーidが例えば6文字以上、とか名前が性と名からなるとか、そういうのをオブジェクトにしておくと制約をコードにしておけるので作りやすい。不変にしておくのがよい。
エンティティは、ユーザー、とか。ユーザーの年齢が変わっても同じユーザー。
識別子で識別されて同一性が担保される。

<凝集度>
クラスのプロパティが、どのメソッドで使われているか。
すべてのプロパティが全部のメソッドで使われていたらめちゃ高凝集。

<集約>
UserクラスとCircleクラスがあって、たとえばユーザーをサークルに追加する処理、というのを
ユーザーアプリケーションに
circle.members.add(member) としてはいけない。
例えば、サークルに人数制限がある場合、circle.members.add(member)  とするたびに、サークルのメンバーが30人以下かどうか、という確認をcircle.members.add(member)する前に確認するとする。
すると、circle.members.add(member) を呼び出すたびにそれをしないといけないと、プログラム上DRYの原則に反しているし、Circleというクラスにその人数制限の表現ができていないから。
CircleクラスにJoinというメソッドを作ってそこで定義するべき。

<仕様>
仕様をプログラムにするのもいいかも。
前述のCircleで例えると、メンバーを種別で分けることになり、例えばプレミアムユーザーは5人まで、とかルールが複雑になっていくことが考えられる。
その場合
CircleMembersFullSpecipication
というクラスを作って、そこでサークルメンバーに対する複雑な仕様を書いておく。
そうすると、サークルメンバー追加という場合に、CircleMembersFullSpecipicationを呼び出して、仕様にあっていれば追加、とすればよいので楽だしコードが読みやすい。


プログラムの書き方って本当にどうやって書いたって自由なんですよ。

私の好きな言葉に “Code is Poetry” (コードは詩である)というものがあります。

詩はどう書いたって自由です。

が、読むほうにすれば、やはり優劣があります。

いい詩はスッと入ってきますね。

言葉と概念の選び方なんだと思います。

詩人ってセンスが大切みたいな思うかもしれませんが、めちゃくちゃ作品を書いてるんですよね。

松尾芭蕉も確認されただけで1000句も詠んでいたらしいです。

コードもやはり、書いてきた量とプログラマーのスキルというのはある程度比例するなと思うときがあります。

「古池や蛙飛び込む水の音」

ぐらいなプログラムが書けれたら最高ですね⊂(^-^)⊃

 

19 thoughts on 「ドメイン駆動設計入門」という本を読みました

  1. Pingback: PHPerKaigi2023に行ってきた | Summer Snow

  2. fun88 ทางเข าเด ทท อป เป็นที่นิยมในการเดิมพันกีฬาที่หลากหลาย คุณสามารถเลือกเดิมพันในกีฬาที่คุณชื่นชอบได้ นอกจากนี้ยังมีคาสิโนสดที่น่าตื่นเต้นรอคุณอยู่ เช่น บาคาร่า รูเล็ต และสล็อตออนไลน์อีกมากมาย

  3. You really make it seem so easy with your presentation but I to find this matter to be
    really one thing which I think I might never understand.
    It sort of feels too complex and extremely vast
    for me. I’m having a look forward to your next post, I’ll attempt to get the hang of it!
    Najlepsze escape roomy

  4. An intriguing discussion is worth comment. I think that you ought to write more on this subject, it may not be a taboo subject but usually people do not speak about these subjects. To the next! Many thanks.

  5. An interesting discussion is definitely worth comment. I believe that you should publish more on this topic, it may not be a taboo matter but generally folks don’t speak about these topics. To the next! Cheers!

  6. That is a very good tip especially to those fresh to the blogosphere. Brief but very precise info… Many thanks for sharing this one. A must read article!

  7. Good post. I learn something new and challenging on websites I stumbleupon on a daily basis. It’s always interesting to read through content from other writers and use a little something from their websites.

  8. I’m amazed, I must say. Rarely do I encounter a blog that’s both equally educative and entertaining, and let me tell you, you have hit the nail on the head. The problem is something that not enough folks are speaking intelligently about. Now i’m very happy I stumbled across this during my search for something regarding this.

  9. I was more than happy to discover this page. I need to to thank you for your time just for this fantastic read!! I definitely enjoyed every part of it and I have you bookmarked to check out new information on your blog.

  10. You are so awesome! I do not suppose I’ve truly read something like this before. So good to discover somebody with a few original thoughts on this topic. Really.. many thanks for starting this up. This website is one thing that’s needed on the web, someone with a bit of originality.

  11. Aw, this was a really nice post. Spending some time and actual effort to make a great article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.

  12. Greetings! Very helpful advice in this particular article! It is the little changes that will make the largest changes. Thanks a lot for sharing!

  13. Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I am going to return once again since I bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.

  14. Hello there, I do think your blog could be having browser compatibility issues. When I take a look at your site in Safari, it looks fine however, when opening in IE, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Apart from that, wonderful website!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です