domain-logic-implementation

Enforce domain invariants and lifecycle rules before persistence.

4|Updated May 16, 2026
One-click install
npx skills add https://github.com/machenjie/rd-skills --skill domain-logic-implementation
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: domain-logic-implementation
Source: https://github.com/machenjie/rd-skills/tree/main/src/foundation/capabilities/domain-logic-implementation
Command: npx skills add https://github.com/machenjie/rd-skills --skill domain-logic-implementation

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Domain rules often get duplicated inconsistently across controllers, services, UI, and persistence, which leads to invalid states being saved and bugs surfacing only in edge cases.

Core Features & Use Cases

  • Centralized domain authority: places invariants, calculations, lifecycle rules, and policies near the aggregate, value object, or domain service that owns them.
  • Single-source rule enforcement: ensures each rule has one authority and prevents contradictory behavior across layers.
  • State transition safety: rejects invalid lifecycle transitions before persistence so aggregates remain consistent over time.
  • Transport- and persistence-independent logic: keeps domain operations free from framework/ORM objects and transport concerns.

Quick Start

Use this capability to design a domain implementation contract for a change that adds or modifies an invariant, aggregate behavior, calculation, or lifecycle transition.

Frequently Asked Questions about domain-logic-implementation

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce domain invariants and prevent invalid state transitions in aggregates?

To enforce domain invariants, place lifecycle rules and state transition checks directly near the owning aggregate or domain service to reject invalid changes before persistence. This establishes a single-source rule authority, preventing duplicated inconsistent checks across controllers and services while keeping aggregates consistent.

Why does duplicated domain logic across layers cause bugs in edge cases?

Duplicated domain logic across UI, controllers, and persistence causes bugs because inconsistent rule enforcement allows invalid states to be saved, surfacing only during edge cases. Centralizing invariants within a domain authority ensures each rule has one owner, eliminating contradictory behavior across application layers.

What is the best way to design a domain implementation contract for a new aggregate behavior?

The best way to design a domain implementation contract is to define explicit success and failure outcomes for the new aggregate behavior, ensuring the logic remains free from transport and persistence concerns. Enforce boundary-focused tests to validate that the domain service correctly rejects invalid transitions.

How do I keep domain operations independent from framework and ORM objects?

Keep domain operations independent by placing calculations, policies, and invariants near the owning domain model or value object, strictly avoiding framework or ORM object references inside the domain logic. This transport-agnostic approach ensures the domain service maintains rule authority without external dependencies.

When should I use a domain service instead of an aggregate for rule placement?

Use a domain service for rule placement when an invariant or calculation spans multiple domain models or does not naturally belong to a single aggregate. Centralizing the policy within the domain service ensures state transition safety and prevents duplicated inconsistent checks across layers before persistence.

How do I test invalid lifecycle transitions before persistence in a domain model?

Test invalid lifecycle transitions by writing boundary-focused tests that specify explicit failure outcomes when the domain authority rejects an invalid state change. This validates that the aggregate or value object enforces invariants and prevents invalid data from reaching the persistence layer.