Claude Codeで値オブジェクトを設計する:不変性・等値性・ドメイン表現の型安全化

はじめに 「金額をnumberで扱っていて通貨単位の混在バグが起きた」「メールアドレスをstringで渡し続けていてバリデーションが散らばった」——値オブジェクトでドメイン概念を型として表現し、不正な値が作れない設計をClaude Codeに生成させる。 CLAUDE.mdに値オブジェクト設計ルールを書く ## 値オブジェクト設計ルール ### 特性 - 不変(Immutable): 作成後...

By · · 1 min read
Claude Codeで値オブジェクトを設計する:不変性・等値性・ドメイン表現の型安全化

Source: DEV Community

はじめに 「金額をnumberで扱っていて通貨単位の混在バグが起きた」「メールアドレスをstringで渡し続けていてバリデーションが散らばった」——値オブジェクトでドメイン概念を型として表現し、不正な値が作れない設計をClaude Codeに生成させる。 CLAUDE.mdに値オブジェクト設計ルールを書く ## 値オブジェクト設計ルール ### 特性 - 不変(Immutable): 作成後に変更不可(全フィールドreadonly) - 等値性: IDではなく値で等しいかを判定(equals()メソッド) - 自己検証: コンストラクタでバリデーション(無効な値は作れない) ### 設計 - プリミティブ型(string/number)の代わりに使用 - privateコンストラクタ + static create()でファクトリーメソッド - toValue()でプリミティブに変換(DBへの保存など) ### 適用対象 - 金額・通貨・価格 - メールアドレス・URLなどの文字列 - 住所・座標などの複合値 - 数量・サイズ(単位付き数値) 値オブジェクト実装の生成 値オブジェクトを設計してください。 要件: - 不変・等値性・自己検証 - Moneyクラス(金額+通貨) - EmailAddress - URL - Quantity(単位付き数値) 生成ファイル: src/domain/valueObjects/ 生成される値オブジェクト実装 // src/domain/valueObjects/money.ts — 金額値オブジェクト export type Currency = 'JPY' | 'USD' | 'EUR' | 'GBP'; export class Money { private constructor( private readonly _amount: number, // 最小単位(円/セント) private readonly _currency: Currency ) {} static of(amount: number, currency: Currency): Money { if (!Number.isInteger(amount)) { throw new DomainError(`Money amount must be

Related Posts

Similar Topics

#ai (18)#feat (17)#fix (17)#webdev (14)#javascript (13)#chore (9)#opensource (8)#large language models (4)#modernization (6)#css (6)#backbonejs (6)#es6 (6)#redis (2)#security (5)#nextjs (5)#programming (5)#code generation (4)#api (4)#node (4)#html (4)

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#artificial intelligence (31559)#data science (24017)#ai (16798)#generative ai (15034)#crypto (14995)#machine learning (14680)#bitcoin (14235)#featured (13557)#news & insights (13064)#crypto news (11083)

Around the Network