public Account GetById(int id) { string sql = "SELECT Id, Balance FROM Account WHERE Id=@Id"; SqlParameter parameter = new SqlParameter("@Id", id); Account account = new Account(); using (SqlConnection conn = new SqlConnection(_connStr)) { conn.Open(); using(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.Add(parameter); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { account.Id = id; account.Balance = Convert.ToDecimal(reader["Balance"]); } } } return account; }
protected void Page_Init(object sender, EventArgs e) { this._accountService = ObjectFactory.GetInstance<AccountService>(); this.account1 = this._accountService.GetAccountById(1); this.account2 = this._accountService.GetAccountById(2); }
public void Transfer(Account from, Account to, decimal amount) { if (from.Balance > amount) { from.Balance -= amount; to.Balance += amount; this._accountRepository.Save(from); this._accountRepository.Save(to); this._unitOfWork.Commit(); } else { throw new Exception("余额不足"); } }
public void Remove(Account account) { _unitOfWork.RegisterRemoved(account, this); }
public void Add(Account account) { _unitOfWork.RegisterNew(account, this); }
public void Save(Account account) { _unitOfWork.RegisterAmended(account, this); }