public void TestTransactionCommit() { IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)]; Account account = NewAccount(); daoManager.OpenConnection(); Account account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); account2.EmailAddress = "*****@*****.**"; try { daoManager.BeginTransaction(); accountDao.Create(account); accountDao.Update(account2); daoManager.CommitTransaction(); } finally { // Nothing } daoManager.OpenConnection(); account = accountDao.GetAccountById(account.Id); account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); Assert.IsNotNull(account); Assert.AreEqual("*****@*****.**", account2.EmailAddress); }
public void TestCreateAccount() { IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)]; Account account = NewAccount(); try { daoManager.OpenConnection(); accountDao.Create(account); account = accountDao.GetAccountById(1001); } catch (Exception e) { // Ignore Console.WriteLine("TestCreateAccount, error cause : " + e.Message); } finally { daoManager.CloseConnection(); } Assert.IsNotNull(account); Assert.AreEqual("*****@*****.**", account.EmailAddress); }
public ActResult <string> Create(Account account) { var result = new ActResult <string>(); try { //检查登录名和手机号码是否重复 if (accountDao.GetAccountByLoginName(account.Name) != null) { result.Message = "该用户名已存在!"; } else if (accountDao.GetAccountByPhone(account.Phone) != null) { result.Message = "该手机号码已存在!"; } else { accountDao.Create(account); result.Message = "创建成功!"; } } catch (Exception ex) { result.Message = ex.Message; } return(result); }
public void TestUsingTransactionScope() { Account account = NewAccount(); IAccountDao accountDao = daoManager[typeof(IAccountDao)] as IAccountDao; IUserDao userDao = daoManager2[typeof(IUserDao)] as IUserDao; DateTime stamp = DateTime.Now.AddDays(2); User joeCool = null; daoManager.OpenConnection(); accountDao.Create(account); daoManager.CloseConnection(); User newUser = new User(); newUser.Id = "joe_cool"; newUser.UserName = "******"; newUser.Password = "******"; newUser.EmailAddress = "*****@*****.**"; newUser.LastLogon = DateTime.Now; daoManager2.OpenConnection(); userDao.Create(newUser); daoManager2.CloseConnection(); using (TransactionScope tx = new TransactionScope()) { daoManager.OpenConnection(); account = accountDao.GetAccountById(1001); account.FirstName = "TestTransactionScope"; accountDao.Update(account); daoManager.CloseConnection(); daoManager2.OpenConnection(); joeCool = userDao.Load("joe_cool"); joeCool.LastLogon = stamp; daoManager2.CloseConnection(); //tx.Complete(); // not call complte --> RollBack } //---------------------------------------- daoManager.OpenConnection(); account = accountDao.GetAccountById(1001); daoManager.CloseConnection(); Assert.IsNotNull(account); Assert.AreEqual("*****@*****.**", account.EmailAddress); Assert.IsFalse("TestTransactionScope" == account.FirstName); //---------------- daoManager2.OpenConnection(); joeCool = userDao.Load("joe_cool"); daoManager2.CloseConnection(); Assert.IsNotNull(joeCool); Assert.AreEqual("Joseph Cool", joeCool.UserName); Assert.IsFalse(stamp.ToString() == joeCool.LastLogon.ToString()); }
public void TestUsingConnection() { IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)]; using (IDalSession session = daoManager.OpenConnection()) { Account account = NewAccount(); accountDao.Create(account); } // compiler will call Dispose on DaoSession }
public void TestUsingTransaction() { IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)]; using (IDalSession session = daoManager.BeginTransaction()) { Account account = NewAccount(); Account account2 = accountDao.GetAccountById(1); account2.EmailAddress = "*****@*****.**"; accountDao.Create(account); accountDao.Update(account2); session.Complete(); // Commit } // compiler will call Dispose on IDalSession }
public void TestDeleteAccount() { IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)]; Account account = NewAccount(); daoManager.OpenConnection(); accountDao.Create(account); account = accountDao.GetAccountById(1001); Assert.IsNotNull(account); Assert.AreEqual("*****@*****.**", account.EmailAddress); accountDao.Delete(account); account = accountDao.GetAccountById(1001); Assert.IsNull(account); daoManager.CloseConnection(); }
public void TestTransactionRollback() { IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)]; Account account = NewAccount(); daoManager.OpenConnection(); Account account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); account2.EmailAddress = "*****@*****.**"; try { daoManager.BeginTransaction(); accountDao.Create(account); accountDao.Update(account2); throw new Exception("BOOM!"); //daoManager.CommitTransaction(); } catch { daoManager.RollBackTransaction(); } finally { } daoManager.OpenConnection(); account = accountDao.GetAccountById(account.Id); account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); Assert.IsNull(account); Assert.AreEqual("*****@*****.**", account2.EmailAddress); }
public void TestNestedDao() { IAccountDao accountDao = daoManager[typeof(IAccountDao)] as IAccountDao; IUserDao userDao = daoManager2[typeof(IUserDao)] as IUserDao; DateTime stamp = DateTime.Now.AddDays(2); User joeCool = null; User newUser = new User(); Account account = NewAccount(); newUser.Id = "joe_cool"; newUser.UserName = "******"; newUser.Password = "******"; newUser.EmailAddress = "*****@*****.**"; newUser.LastLogon = DateTime.Now; daoManager.OpenConnection(); daoManager2.OpenConnection(); accountDao.Create(account); userDao.Create(newUser); daoManager.CloseConnection(); daoManager2.CloseConnection(); account = null; daoManager.OpenConnection(); account = accountDao.GetAccountById(1001); daoManager.CloseConnection(); Assert.IsNotNull(account); Assert.AreEqual("*****@*****.**", account.EmailAddress); daoManager2.OpenConnection(); joeCool = userDao.Load("joe_cool"); daoManager2.CloseConnection(); Assert.IsNotNull(joeCool); Assert.AreEqual("Joseph Cool", joeCool.UserName); }