Class DomainServiceAbstract

Base class for Domain Services in Domain-Driven Design.

Domain Services encapsulate domain logic that:

  • Doesn't naturally fit within an Entity or Value Object
  • Operates on multiple aggregates
  • Represents a significant process or transformation in the domain

Unlike Application Services, Domain Services contain pure domain logic and have no dependencies on infrastructure concerns.

class MoneyTransferService extends DomainService {
readonly name = 'MoneyTransferService';

transfer(
from: BankAccount,
to: BankAccount,
amount: Money
): Result<void, DomainError> {
// Validate transfer
if (!from.canWithdraw(amount)) {
return Result.fail(
new DomainError('INSUFFICIENT_FUNDS', 'Account has insufficient funds')
);
}

if (!from.currency.equals(amount.currency)) {
return Result.fail(
new DomainError('CURRENCY_MISMATCH', 'Currency mismatch')
);
}

// Perform transfer
from.withdraw(amount);
to.deposit(amount);

// Record domain events
from.record({
occurredAt: new Date(),
accountId: from.id.value,
amount: amount.value,
type: 'withdrawal'
});

to.record({
occurredAt: new Date(),
accountId: to.id.value,
amount: amount.value,
type: 'deposit'
});

return Result.ok(undefined);
}
}
class OrderPricingService extends DomainService {
readonly name = 'OrderPricingService';

calculateTotal(order: Order, customer: Customer): Money {
let total = order.subtotal;

// Apply customer discount
if (customer.isPremium()) {
total = total.multiply(0.9); // 10% discount
}

// Add shipping
const shipping = this.calculateShipping(order, customer);
total = total.add(shipping);

// Add taxes
const tax = this.calculateTax(total, customer.address);
total = total.add(tax);

return total;
}

private calculateShipping(order: Order, customer: Customer): Money {
// Shipping logic
}

private calculateTax(amount: Money, address: Address): Money {
// Tax calculation logic
}
}

Constructors

Properties

Constructors

Properties

name: string

Unique name identifying this domain service