コード例 #1
0
        public void AddBankAccountReturnDTOWhenSaveSucceed()
        {
            //Arrange
            IBankTransferService transferService = new BankTransferService();

            ITypeAdapter adapter = PrepareTypeAdapter();

            SICustomerRepository customerRepository = new SICustomerRepository();
            customerRepository.GetGuid = (guid) =>
            {
                return new Customer()
                {
                    Id = guid,
                    FirstName = "Jhon",
                    LastName = "El rojo"
                };
            };

            SIBankAccountRepository bankAccountRepository = new SIBankAccountRepository();
            bankAccountRepository.AddBankAccount = (ba) => { };
            bankAccountRepository.UnitOfWorkGet = () =>
            {
                var uow = new SIUnitOfWork();
                uow.Commit = () => { };

                return uow;
            };

            var dto = new BankAccountDTO()
            {
                CustomerId = Guid.NewGuid(),
                BankAccountNumber = "BA"
            };

            IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService, adapter);

            //Act
            var result = bankingService.AddBankAccount(dto);

            //Assert
            Assert.IsNotNull(result);
        }
コード例 #2
0
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/>
        /// </summary>
        /// <param name="bankAccountDTO"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/></returns>
        public BankAccountDTO AddBankAccount(BankAccountDTO bankAccountDTO)
        {
            if (bankAccountDTO != null && bankAccountDTO.CustomerId != Guid.Empty)
            {
                //check if exists the customer for this bank account
                var associatedCustomer = _customerRepository.Get(bankAccountDTO.CustomerId);

                if (associatedCustomer != null) // if the customer exist
                {
                    //Create a new bank account  number
                    var accountNumber = CalculateNewBankAccountNumber();

                    //Create account from factory and set persistent id
                    var account = BankAccountFactory.CreateBankAccount(associatedCustomer, accountNumber);

                    //set the poid
                    account.Id = IdentityGenerator.NewSequentialGuid();

                    //save bank account
                    SaveBankAccount(account);

                    //return dto
                    return _adapter.Adapt<BankAccount, BankAccountDTO>(account);
                }
                else //the customer for this bank account not exist, cannot create a new bank account
                {
                    LoggerFactory.CreateLog().LogWarning(Messages.warning_CannotCreateBankAccountForNonExistingCustomer);
                    return null;
                }
            }
            else // if bank account is null or customer identifier  is empty, cannot create a new bank account
            {
                LoggerFactory.CreateLog().LogWarning(Messages.warning_CannotAddNullBankAccountOrInvalidCustomer);
                return null;
            }
        }
コード例 #3
0
 bool BankAccountHasIdentity(BankAccountDTO bankAccountDTO)
 {
     //return true is bank account dto has identity
     return (bankAccountDTO != null && bankAccountDTO.Id != Guid.Empty);
 }
コード例 #4
0
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/>
        /// </summary>
        /// <param name="fromAccount"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/></param>
        /// <param name="toAccount"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/></param>
        /// <param name="amount"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService"/></param>
        public void PerformBankTransfer(BankAccountDTO fromAccount, BankAccountDTO toAccount, decimal amount)
        {
            //Application-Logic Process:
            // 1º Get Accounts objects from Repositories
            // 2º Start Transaction
            // 3º Call PerformTransfer method in Domain Service
            // 4º If no exceptions, commit the unit of work and complete transaction

            if (BankAccountHasIdentity(fromAccount)
                &&
                BankAccountHasIdentity(toAccount))
            {
                //get the current unit of work
                var unitOfWork = _bankAccountRepository.UnitOfWork;

                var source = _bankAccountRepository.Get(fromAccount.Id);
                var target = _bankAccountRepository.Get(toAccount.Id);

                if (source != null & target != null) // if all accounts exist
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        //perform transfer
                        _transferService.PerformTransfer(amount, source, target);

                        //comit unit of work
                        unitOfWork.Commit();

                        //complete transaction
                        scope.Complete();
                    }
                }
                else
                    LoggerFactory.CreateLog().LogError(Messages.error_CannotPerformTransferInvalidAccounts);
            }
            else
                LoggerFactory.CreateLog().LogError(Messages.error_CannotPerformTransferInvalidAccounts);
        }
コード例 #5
0
 /// <summary>
 /// <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/>
 /// </summary>
 /// <param name="from"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/></param>
 /// <param name="to"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/></param>
 /// <param name="amount"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/></param>
 public void PerformTransfer(BankAccountDTO from, BankAccountDTO to, decimal amount)
 {
     _bankAppService.PerformBankTransfer(from, to, amount);
 }
コード例 #6
0
 /// <summary>
 /// <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/>
 /// </summary>
 /// <param name="newBankAccount"><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/></param>
 /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService"/></returns>
 public BankAccountDTO AddNewBankAccount(BankAccountDTO newBankAccount)
 {
     return _bankAppService.AddBankAccount(newBankAccount);
 }
コード例 #7
0
        public void PerformBankTransfer()
        {
            //Arrange
            var sourceId = new Guid("3481009C-A037-49DB-AE05-44FF6DB67DEC");
            var bankAccountNumberSource = new BankAccountNumber("4444", "5555", "3333333333", "02");

            var source = BankAccountFactory.CreateBankAccount(Guid.NewGuid(), bankAccountNumberSource);
            source.Id = sourceId;
            source.DepositMoney(1000,"initial");

            var sourceBankAccountDTO = new BankAccountDTO()
            {
                Id = sourceId,
                BankAccountNumber = source.Iban
            };

            var targetId = new Guid("8A091975-F783-4730-9E03-831E9A9435C1");
            var bankAccountNumberTarget = new BankAccountNumber("1111", "2222", "3333333333", "01");
            var target = BankAccountFactory.CreateBankAccount(Guid.NewGuid(), bankAccountNumberTarget);
            target.Id = targetId;

            var targetBankAccountDTO = new BankAccountDTO()
            {
                Id = targetId,
                BankAccountNumber = target.Iban
            };

            var accounts = new List<BankAccount>() { source, target };
            var accountsDTO = new List<BankAccountDTO>() { sourceBankAccountDTO, targetBankAccountDTO };

            SIBankAccountRepository bankAccountRepository = new SIBankAccountRepository();
            bankAccountRepository.GetGuid = (guid) =>
            {
                return accounts.Where(a => a.Id == guid).SingleOrDefault();
            };
            bankAccountRepository.UnitOfWorkGet = () =>
            {
                var unitOfWork = new SIUnitOfWork();
                unitOfWork.Commit = () => { };

                return unitOfWork;
            };

            SICustomerRepository customerRepository = new SICustomerRepository();
            IBankTransferService transferService = new BankTransferService();
            ITypeAdapter adapter = PrepareTypeAdapter();

            IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService, adapter);

            //Act

            bankingService.PerformBankTransfer(sourceBankAccountDTO, targetBankAccountDTO, 100M);
        }
コード例 #8
0
        public void AddBankAccountReturnNullWhenCustomerNotExist()
        {
            //Arrange
            SIBankAccountRepository bankAccountRepository = new SIBankAccountRepository();
            SICustomerRepository customerRepository = new SICustomerRepository();
            customerRepository.GetGuid = (guid) => { return null; };

            IBankTransferService transferService = new BankTransferService();
            ITypeAdapter adapter = PrepareTypeAdapter();

            var dto = new BankAccountDTO()
            {
                CustomerId = Guid.NewGuid()
            };

            IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService, adapter);

            //Act
            var result = bankingService.AddBankAccount(dto);

            //Assert

            Assert.IsNull(result);
        }
コード例 #9
0
        public void AddBankAccountReturnNullWhenCustomerIdIsEmpty()
        {
            //Arrange
            SIBankAccountRepository bankAccountRepository = new SIBankAccountRepository();
            SICustomerRepository customerRepository = new SICustomerRepository();
            IBankTransferService transferService = new BankTransferService();
            ITypeAdapter adapter = PrepareTypeAdapter();

            var dto = new BankAccountDTO()
            {
                CustomerId = Guid.Empty
            };

            IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService, adapter);

            //Act
            var result = bankingService.AddBankAccount(dto);

            //Assert

            Assert.IsNull(result);
        }