public void Transfer(Account from, Account to, decimal amount)
        {
            if (from.Balance>=amount)
            {
                from.Balance -= amount;
                to.Balance += amount;

                _accountRepository.Save(from);
                _accountRepository.Save(to);
                _unitOfWork.Commit();
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Account a = new Account(1000);
            System.Console.WriteLine("现在张三,存有{0}", a.Balance);
            Account b = new Account(200);
            System.Console.WriteLine("现在李四,存有{0}", b.Balance);
            System.Console.WriteLine("张三准备转500元给李四,转帐开始了......");

            //声明要使用的UnitOfWork
            IUnitOfWork unitOfWork = new UnitOfWork.Infrastructure.UnitOfWork();

            //声明要使用的Repository
            IAccountRepository accountRepository = new AccountRepository(unitOfWork);

            AccountService service = new AccountService(accountRepository, unitOfWork);

            service.Transfer(a, b, 500);
            System.Console.WriteLine("转账结束");
            System.Console.WriteLine("张三当前余额:{0}", a.Balance);
            System.Console.WriteLine("李四当前余额:{0}", b.Balance);

            System.Console.ReadKey();
        }
 public void Save(Account account)
 {
     _unitOfWork.RegisterAmended(account, this);
 }
 public void Remove(Account account)
 {
     _unitOfWork.RegisterRemoved(account, this);
 }
 public void Add(Account account)
 {
     _unitOfWork.RegisterNew(account, this);
 }