もりもりこちゃん
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # ドメイン駆動設計入門 1章 ~ 3章 ### ドメイン駆動設計とは何か 何らかのシステムを作るときエンジニアは何かを新しく学ぶ。その分野に無知であるので。 例えば物流システムであれば、物流の業務の流れや登場人物 >開発者がソフトウェアを開発するために必要な知識と不要な知識を選り分けるに は何が必要でしょうか。当たり前のことですが、ソフトウェアの利用者を取り巻く 世界について知る必要があります。 >ソフトウェアの利用者にとって重要な知識が何であるのかは、その世界の有り様に左右されるのです。価値あるソフトウェアを構 築するためには利用者の問題を見極め、解決するための最善手を常に考えていく必 要があります。 >ドメイン駆動設計はそういった洞察を繰り返しながら設計を行い、 ソフトウェアの利用者を取り巻く世界と実装を結びつけることを目的としていま す。 ### 2種類に分かれる - ソフトウェアにとって重要な概念を抽出するための「モデリング」 - 概念を実装に 落とし込むための「パターン」 この本ではパターンに焦点を当てて進めています ## ドメインとドメインモデルとドメインオブジェクト ドメインは「領域」という意味。 例えば会計システムなら金銭や帳簿という概念が含まれ、物流システムならトラックやドライバーが概念として含まれている。 >ドメイン駆動設計では、ドメインの概念 をモデリングして得られたモデルをドメインモデルと呼びます >ドメインモデルをソフトウェアで動作するモジュールとして表現したものがドメ インオブジェクトです。 >![](https://i.imgur.com/IjAntxG.png) 実際にコードで表現したものがドメインオブジェクト ## 本書で紹介されるパターン - 知識を表現するパターン - 値オブジェクト - エンティティ - ドメインサービス - アプリケーションを実現するためのパターン - リポジトリ - アプリケーションサービス - ファクトリ - 知識を表現する、より発展的なパターン - 集約 - 仕様 >![](https://i.imgur.com/Fq3woyF.png) ## 値オブジェクト フルネームから姓を取り出す場合 ```csharp= var fullName = "naruse masanobu"; var tokens = fullName.Split(' '); // ["naruse", "masanobu"] ➡ という配列に var lastName = tokens[0]; Console.WriteLine(lastName); // naruse が表示される ``` このコードはこの場合はうまく動いてそうだが、 ```csharp= var fullName = "john smith"; var tokens = fullName.Split(' '); // ["john", "smith"] という配列に var lastName = tokens[0]; Console.WriteLine(lastName); // john が表示される ``` 姓が後ろに記述される文化では動かない 一般的にはクラスで表現する ```csharp= class FullName { public FullName(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } public string FirstName { get; } public string LastName { get; } } ``` >この例からわかることは、システムに最適な値が必ずしもプリミティブな値であ るとは限らないということです。 >システムには必要とされる処理にしたがって、そ のシステムならではの値の表現があるはずです 値オブジェクトとは >- 不変である >- 交換が可能である >- 等価性によって比較される 硬貨がよく例として挙げられます。それぞれに当てはめると #### 不変である ```csharp= var coin = new Coin(100) // 100円玉 coin.changeAmount(500) ``` 硬貨が途中で変化することは現実世界からは予想できず混乱させます #### 交換が可能である ```csharp= var myPocketCoin = new Coin(100) // 100円玉 myPocketCoin = new Coin(500) // 500円玉 ``` 変化はしないが、交換することは可能である #### 価性によって比較される ```csharp= var myPocketCoin1 = new Coin(100) // 100円玉 var myPocketCoin2 = new Coin(500) // 500円玉 Console.WriteLine(myPocketCoin1.amount === myPocketCoin2.amount); ``` 不自然。 もしCoinに`equals()`メソッドが生えていた場合 ```csharp= public override bool Equals(object obj) { return this.amount === object.amount } ``` このように自然に書くことができる。 ```csharp= Console.WriteLine(myPocketCoin1 === myPocketCoin2); ``` これは自然である以外にも、属性が追加されても修正不要というメリットがある ### どこまで値オブジェクトにするか コンテキストによるので一概には言えない ### 値オブジェクトにするモチベーション 利点 >- 表現力を増す ```csharp= var modelNumber = "a20421-100-1"; var modelNumber = new ModelNumber("a20421-100-1") ``` >- 不正な値を存在させない ```csharp= // passwordが文字列の場合、コントローラー内 var password = "hoge" if (password.length < 8) { throw new Exception("異常な値です"); } // passwordが値オブジェクトの場合 var password = new Password("hoge") // この時点でthrowされる ``` >- 誤った代入を防ぐ >- ロジックの散在を防ぐ ## エンティティ 値オブジェクトと対をなすドメインオブジェクト >両者の違いは同一性によって識別されるか否かです >- 可変である >- 同じ属性であっても区別される >- 同一性をもつ 例えば、人間がよく例に出されます #### 可変である 身長や体重は生活のなかで変化します ```csharp= var morifuji = new Person(170) // 身長170cm morifuji.changeHeight(170.5) ``` #### 同じ属性であっても区別される 例えば自分と同姓同名の人がいても、自分とは区別されます(されるべきです)。現実では自明ですが、システムの中では識別子というものを持ちます。 #### 同一性をもつ 身長が変わったとしても変化前と変化後で **識別子**をもつことによって同一性をもちます ### どこまでエンティティにするか ドメインオブジェクトはどういう基準でエンティティまたは値オブジェクトとして定義するか >ライフサイクルが存在し、そこに連続性が 存在するかというのは大きな判断基準になります。 >作成されて生を受け、削除されて死を迎える。まさにユーザはライフサイクルを もち、連続性のある概念です。ユーザはエンティティで間違いなさそうです。 逆に言うと、連続性のないものは値オブジェクトとして定義するとシステムがシンプルに保たれる 結局はこの基準もコンテキストに依存しており、**車のタイヤはドライバーからすると「値オブジェクト」。タイヤ工場からみると「エンティティ」になる**

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully