private IList <DtoTransaction> ReadTransactionsFromUser() { using (var repository = new RepositoryTransaction()) { return(repository.Read()); } }
public EmpEntity GetEntity(int empID) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetEmpRepository(); return(repository.FindBySpecification(o => o.EmpID == empID).FirstOrDefault()); } }
public AgencyEntity GetEntity(int key) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetAgencyRepository(); return(repository.GetByKey(key)); } }
public IList <CollectionEntity> GetEntities(int?clientID, bool onlyUnWriteOff) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetCollectionRepository(); return(repository.FindBySpecification(CreateSpecExpression(clientID, onlyUnWriteOff)).ToList()); } }
public TeamEntity GetEntity(int TeamID) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetTeamRepository(); return(repository.GetByKey(TeamID)); } }
public AREntity GetByKey(int arId) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetARRepository(); return(repository.GetByKey(arId)); } }
public IList <AgencyEntity> GetEntities() { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetAgencyRepository(); return(repository.FindBySpecification(o => true).ToList()); } }
public void Add(AREntity entity) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetARRepository(); repository.Add(entity); trans.Commit(); } }
public EmpEntity GetEntity(string username, string password) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetEmpRepository(); password = PasswordMD5(password); username = username.ToLower(); return(repository.FindBySpecification(o => o.Username == username && o.Password == password).FirstOrDefault()); } }
public void Merge(EmpEntity entity) { using (RepositoryTransaction trans = new RepositoryTransaction()) { TrimFields(entity); var repository = trans.GetEmpRepository(); if (entity.EmpID <= 0) { if (entity.Username.IsNullOrEmpty() || entity.Password.IsNullOrEmpty()) { entity.Username = null; entity.Password = null; } else { var sameUserameEntities = repository.FindBySpecification(o => o.Username == entity.Username.ToLower()); if (sameUserameEntities.Count() > 0) { throw new ApplicationException("Someone already has that username!Try another please!"); } } repository.Add(entity); } else { var orgEntity = repository.GetByKey(entity.EmpID); orgEntity.EmpEmail = entity.EmpEmail; orgEntity.EmpName = entity.EmpName; if (entity.Username.IsNullOrEmpty()) { orgEntity.Username = null; orgEntity.Password = null; } else { if (orgEntity.Username != entity.Username) { var sameUserameEntities = repository.FindBySpecification(o => o.Username == entity.Username.ToLower()); if (sameUserameEntities.Count() > 0 && sameUserameEntities.Single().EmpID != entity.EmpID) { throw new ApplicationException("Someone already has that username!Try another please!"); } orgEntity.Username = entity.Username; } if (!entity.Password.IsNullOrEmpty()) { orgEntity.Password = entity.Password; } } } trans.Commit(); } }
public BusinessTests() { var dbContext = new EfContext(new DbContextOptionsBuilder <EfContext>().Options); var uow = new EfUnitOfWork(); var logger = new Logger(); var repoCustomer = new RepositoryBase <Customer>(new EfRepository(dbContext)); var repoAccount = new RepositoryBase <Account>(new EfRepository(dbContext)); var repoTransaction = new RepositoryTransaction(new EfRepository(dbContext)); _serviceCustomer = new BusinessCustomer(repoCustomer, uow, logger); _serviceAccount = new BusinessAccount(repoAccount, uow, logger, repoCustomer, repoTransaction); _serviceTransaction = new BusinessBase <Transaction>(repoTransaction, uow, logger); }
public void CommitTest() { // Add a new thing, manually commit. using (RepositoryTransaction rt = EntityRepository.BeginTransaction()) { ThingRepo.Save(new Thing() { Name = "TestThing", Corners = 1 }); rt.Commit(); } // Check thing was saved Assert.AreEqual(1, ThingRepo.GetAll().Count, "Thing should be saved"); }
public void RollbackTest() { // Add a new thing, but rollback xn. using (RepositoryTransaction rt = EntityRepository.BeginTransaction()) { ThingRepo.Save(new Thing() { Name = "TestThing", Corners = 1 }); rt.Rollback(); } // Check thing wasn't saved Assert.AreEqual(0, ThingRepo.GetAll().Count, "Thing should not be saved"); }
public void Merge(AgencyEntity entity) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetAgencyRepository(); if (entity.AgencyID <= 0) { repository.Add(entity); } else { var orgEntity = repository.GetByKey(entity.AgencyID); orgEntity.AgencyName = entity.AgencyName; orgEntity.CurrencyType = entity.CurrencyType; } trans.Commit(); } }
public void Merge(TeamEntity entity) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetTeamRepository(); if (entity.TeamID <= 0) { repository.Add(entity); } else { var orgEntity = repository.GetByKey(entity.TeamID); orgEntity.Depart = entity.Depart; orgEntity.Market = entity.Market; } trans.Commit(); } }
public ActionResult Delete(DtoTransaction dto) { if (RequestIsLoaded()) { dto.Value = decimal.Parse(Request.Form["value"]); using (var repository = new RepositoryTransaction()) { repository.Delete(dto); } return(RedirectToAction("Index")); } else { return(View()); } }
public void Merge(ClientEntity entity) { using (RepositoryTransaction trans = new RepositoryTransaction()) { var repository = trans.GetClientRepository(); if (entity.ClientID <= 0) { repository.Add(entity); } else { var orgEntity = repository.GetByKey(entity.ClientID); orgEntity.ClientGroup = entity.ClientGroup; orgEntity.ClientName = entity.ClientName; orgEntity.ClientType = entity.ClientType; } trans.Commit(); } }
public ActionResult Save(DtoTransaction dto) { if (NewItem(dto)) { dto.Id = Guid.NewGuid(); using (var repository = new RepositoryTransaction()) { repository.Create(dto); } } else { using (var repository = new RepositoryTransaction()) { repository.Update(dto); } } return(RedirectToAction("Index"));; }
public void ThreadSafetyTest() { // Run first thread, start a transaction, add a new entity, but do not commit. Pause thread. // Run a second thread that will add a new entity in its own transaction, and commit. End thread. // Run a third thread that will go get the entities, T1 entity should not be there, but T2 entity should be. End thread. // Resume first thread. Commit transaction. Get all and make sure we now have all entities. // We define T2 and T3 before T1, as T1 runs both of these and waits for them to complete. ManualResetEvent t2wait = new ManualResetEvent(false); Thread t2 = new Thread(() => { try { // Add a new thing in a transaction. using (RepositoryTransaction rt = EntityRepository.BeginTransaction()) { ThingRepo.Save(new Thing() { Name = "T2", Corners = 1 }); rt.Commit(); } } finally { t2wait.Set(); } }); ManualResetEvent t3wait = new ManualResetEvent(false); Thread t3 = new Thread(() => { try { // Do a get. Should get just T2. So 1 thing Assert.AreEqual(1, ThingRepo.GetAll().Count, "Should be 1 thing by now"); } finally { t3wait.Set(); } }); ManualResetEvent t1wait = new ManualResetEvent(false); Thread t1 = new Thread(() => { try { using (RepositoryTransaction rt = EntityRepository.BeginTransaction()) { ThingRepo.Save(new Thing() { Name = "T1", Corners = 1 }); // Before we commit. Run T2. t2.Start(); t2wait.WaitOne(); // And t3 t3.Start(); t3wait.WaitOne(); // Now commit our changes rt.Commit(); } // And now check that we have both new things Assert.AreEqual(2, ThingRepo.GetAll().Count, "Should be 2 things by now"); } finally { t1wait.Set(); } }); // Run the whole thing. // And wait for t1 to exit. t1.Start(); t1wait.WaitOne(); // Double check that we have both new things Assert.AreEqual(2, ThingRepo.GetAll().Count, "Should be 2 things by now"); }
/// <summary> /// Read the list of transactions from file and do them. /// </summary> public static async Task TransactionProcessAsync(CancellationToken cancellationToken = default) { try { string filePath = "\\Data\\transactions.json"; var transactionsDTO = IOHelpers.ReadFromFile <Transaction, TransactionDTO>(filePath); var repositoryTransaction = new RepositoryTransaction(); var transactions = IOHelpers.EntitiesList <Transaction, TransactionDTO>(transactionsDTO, repositoryTransaction.BusinessToDomainObjectPropertyMap()); await repositoryTransaction.UpdateIds(transactions, transactionsDTO); using (var dbContext = new BankAccountContext()) { var repositoryAccountContext = new RepositoryBaseEF <Account>(dbContext); for (int i = 0; i < transactions.Count; i++) { var transactionDone = false; var account = await repositoryAccountContext.FindById(transactions[i].AccountId); repositoryTransaction = new RepositoryTransaction(account); var message = string.Empty; message = string.Format("Ouput {0}:", (i + 1).ToString()); repositoryTransaction.report(message); var validation = true; if (transactions[i].TransactionType == Models.Enums.TransactionTypes.WithdrawCash || transactions[i].TransactionType == Models.Enums.TransactionTypes.WithdrawTrnasfer) { validation = await Validators.Validations.CustomerValidation(transactions[i].AccountId, transactions[i].CustomerId, transactionsDTO[i].AccountNo); } var accountDestination = new Account(); if (validation) { switch (transactions[i].TransactionType) { case Models.Enums.TransactionTypes.DepositCash: repositoryTransaction.DepositCash(transactions[i]); message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; break; case Models.Enums.TransactionTypes.WithdrawCash: message = repositoryTransaction.WithdrawCash(transactions[i]); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; case Models.Enums.TransactionTypes.WithdrawTrnasfer: foreach (Account ac in repositoryAccountContext.Get()) { if (ac.AccountNo.Equals(transactions[i].TransactionAccountNo)) { accountDestination = ac; break; } } message = repositoryTransaction.WithdrawTrnasfer(transactions[i], accountDestination); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD Account Number: {2} Balance: ${3} CAD", account.AccountNo, account.Balance, accountDestination.AccountNo, accountDestination.Balance); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; } } if (transactionDone) { var repositoryTransactionContext = new RepositoryBaseEF <Transaction>(dbContext); await repositoryTransactionContext.Create(transactions[i]).ConfigureAwait(false); await repositoryTransactionContext.SaveAsync().ConfigureAwait(false); await repositoryAccountContext.Update(account).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); if (accountDestination != null && transactions[i].TransactionType == Models.Enums.TransactionTypes.WithdrawTrnasfer) { await repositoryAccountContext.Update(accountDestination).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); } } } } } catch (DbException ex) { Console.WriteLine(ex.Message); } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Read the list of objects from a file. /// </summary> public static async Task <Dictionary <string, decimal> > Transaction(string filePath) { var transactionsDTO = IOHelpers.ReadFromFile <Transaction, TransactionDTO>(filePath, true); var repositoryTransaction = new RepositoryTransaction(); var transactions = IOHelpers.EntitiesList <Transaction, TransactionDTO>(transactionsDTO, repositoryTransaction.BusinessToDomainObjectPropertyMap()); await repositoryTransaction.UpdateIds(transactions, transactionsDTO); Dictionary <string, decimal> balances = new Dictionary <string, decimal>(); using (var dbContext = new BankAccountContext()) { var repositoryAccountContext = new RepositoryBaseEF <Account>(dbContext); for (int i = 0; i < transactions.Count; i++) { var transactionDone = false; var account = await repositoryAccountContext.FindById(transactions[i].AccountId); repositoryTransaction = new RepositoryTransaction(account); var message = string.Empty; message = string.Format("Ouput {0}:", (i + 1).ToString()); repositoryTransaction.report(message); var validation = true; if (transactions[i].TransactionType == TransactionTypes.WithdrawCash || transactions[i].TransactionType == TransactionTypes.WithdrawTrnasfer) { validation = await Validators.Validations.CustomerValidation(transactions[i].AccountId, transactions[i].CustomerId, transactionsDTO[i].AccountNo); } var accountDestination = new Account(); if (validation) { switch (transactions[i].TransactionType) { case TransactionTypes.DepositCash: repositoryTransaction.DepositCash(transactions[i]); message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; break; case TransactionTypes.WithdrawCash: message = repositoryTransaction.WithdrawCash(transactions[i]); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD", account.AccountNo, account.Balance.ToString()); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; case TransactionTypes.WithdrawTrnasfer: foreach (Account ac in repositoryAccountContext.Get()) { if (ac.AccountNo.Equals(transactions[i].TransactionAccountNo)) { accountDestination = ac; break; } } message = repositoryTransaction.WithdrawTrnasfer(transactions[i], accountDestination); if (message.Equals(string.Empty)) { message = string.Format("Account Number: {0} Balance: ${1} CAD Account Number: {2} Balance: ${3} CAD", account.AccountNo, account.Balance, accountDestination.AccountNo, accountDestination.Balance); repositoryTransaction.report(message); transactionDone = true; } else { repositoryTransaction.report(message); transactionDone = false; } break; } } if (transactionDone) { var repositoryTransactionContext = new RepositoryBaseEF <Transaction>(dbContext); await repositoryTransactionContext.Create(transactions[i]).ConfigureAwait(false); await repositoryTransactionContext.SaveAsync().ConfigureAwait(false); await repositoryAccountContext.Update(account).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); if (accountDestination != null && transactions[i].TransactionType == TransactionTypes.WithdrawTrnasfer) { await repositoryAccountContext.Update(accountDestination).ConfigureAwait(false); await repositoryAccountContext.SaveAsync().ConfigureAwait(false); } } if (account != null && account.AccountNo != null && account.AccountNo != string.Empty) { var key = account.AccountNo; if (balances.ContainsKey(key)) { balances[key] = account.Balance; } else { balances.Add(key, account.Balance); } } if (accountDestination != null && accountDestination.AccountNo != null && accountDestination.AccountNo != string.Empty) { var key = accountDestination.AccountNo; if (balances.ContainsKey(key)) { balances[key] = accountDestination.Balance; } else { balances.Add(key, accountDestination.Balance); } } } } return(balances); }