Ejemplo n.º 1
0
        public void AccountData()
        {
            var dataFixture = new DataFixture();

            dataFixture.Fixture().ConfigureAwait(false).GetAwaiter().GetResult();

            // Initialize the list of objects and save them into database table.
            var filePath    = "\\Data\\accounts.json";
            var accountsDTO = IOHelpers.ReadFromFile <Account, AccountDTO>(filePath, true);

            var repositoryAccount = new RepositoryAccount();
            var accounts          = IOHelpers.EntitiesList <Account, AccountDTO>(accountsDTO, repositoryAccount.BusinessToDomainObjectPropertyMap());

            var accountsNew = new List <Account>();

            using (var dbContext = new BankAccountContext())
            {
                var repository = new RepositoryBaseEF <Account>(dbContext);

                accountsNew = repository.Get();
            }

            dataFixture.Dispose().ConfigureAwait(false).GetAwaiter().GetResult();

            Assert.NotNull(accountsNew);

            var count = accounts.Count;

            Assert.Equal(accounts[count - 1].AccountNo, accountsNew[count - 1].AccountNo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize the list of objects and save them into database table and do the transactions process.
        /// </summary>
        public static async Task SaveDataAsync(CancellationToken cancellationToken = default)
        {
            try
            {
                // Initialize the list of objects and save them into database table.
                string filePath     = "\\Data\\customers.json";
                var    customersDTO = IOHelpers.ReadFromFile <Customer, CustomerDTO>(filePath);

                var repositoryCustomer = new RepositoryCustomer();
                var customers          = IOHelpers.EntitiesList <Customer, CustomerDTO>(customersDTO, repositoryCustomer.BusinessToDomainObjectPropertyMap());

                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <Customer>(dbContext);

                    await repository.CreateEntities(customers).ConfigureAwait(false);

                    await repository.SaveAsync().ConfigureAwait(false);
                }

                filePath = "\\Data\\accounts.json";
                var accountsDTO = IOHelpers.ReadFromFile <Account, AccountDTO>(filePath);

                var repositoryAccount = new RepositoryAccount();
                var accounts          = IOHelpers.EntitiesList <Account, AccountDTO>(accountsDTO, repositoryAccount.BusinessToDomainObjectPropertyMap());

                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <Account>(dbContext);

                    await repository.CreateEntities(accounts).ConfigureAwait(false);

                    await repository.SaveAsync().ConfigureAwait(false);
                }

                if (accountsDTO.Count > 0)
                {
                    await repositoryCustomer.CreateCustomerAccounts(accountsDTO);
                }

                filePath = "\\Data\\phones.json";
                var phonesDTO = IOHelpers.ReadFromFile <PhoneNumber, PhoneNumberDTO>(filePath);

                var repositoryPhone = new RepositoryPhone();
                var phones          = IOHelpers.EntitiesList <PhoneNumber, PhoneNumberDTO>(phonesDTO, repositoryPhone.BusinessToDomainObjectPropertyMap());

                if (phonesDTO.Count > 0 && customersDTO.Count > 0)
                {
                    repositoryPhone.UpdateCustomerIds(customersDTO, phones, phonesDTO);
                }

                using (var dbContext = new BankAccountContext())
                {
                    var repository = new RepositoryBaseEF <PhoneNumber>(dbContext);

                    await repository.CreateEntities(phones).ConfigureAwait(false);

                    await repository.SaveAsync().ConfigureAwait(false);
                }
            }
            catch (DbException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }