사용자 계정간에 송금을 처리하는 컨텍스트가 있다고 가정 해보십시오.DCI에서 예외를 처리하는 방법
class Account < ActiveRecord::Base
belongs_to :user
end
class MoneySender < SimpleDelegator
class NotEnoughBalanceError < StandardError ; ; end
def initialize(account)
super(account)
end
def send_money(destination_account, amount)
raise NotEnoughBalanceError unless can_send?(amount)
self.transaction do
self.balance -= amount
destination_account.balance += amount
end
self.balance
end
def can_send?(amount)
self.balance >= amount
end
end
class HandleMoneyTransferContext
def initialize(source, destination, amount)
@source = source
@destination = destination
@amount = amount
end
def transfer
sender = MoneySender.new(@source
sender.send_money(@destination, @amount)
end
end
그리고 송금이
class AccountsController < AplicationController
def transfer
source = Account.find(params[:id])
destination = Account.find(params[:destination_account])
HandleMoneyTransferContext.new(source, destination, params[:amount]).transfer
render 'success_page'
rescue MoneySender::NotEnoughBalanceError => e
flash[:error] = t(accounts.transfer.not_enough_money)
render 'error_page', status: 400
end
end
같은 것이 그래서 제 질문은, 그것은 OK 컨텍스트가 예외를 발생 할 수 있습니다 작업을한다는 것을 처리하는 웹 응용 프로그램과 레일 컨트롤러에 의해 트리거되는? 컨텍스트에서 역할 예외를 catch하고 컨텍스트 예외를 발생시켜야합니까? (컨텍스트 사용자는 어떤 역할이 사용되는지 알 필요가 없습니다), 더 좋은 해결책이 있습니까?
감사